// ==UserScript==
// @name dmhy tree view
// @namespace https://greasyfork.org/zh-CN/scripts/26430-dmhy-tree-view
// @license GPL version 3
// @encoding utf-8
// @version 0.2
// @date 2017/01/11
// @modified 2017/01/11
// @description convert plain file list into a tree view
// @author TautCony
// @require http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js
// @require http://cdn.bootcss.com/jstree/3.3.3/jstree.min.js
// @resource customCSS http://cdn.bootcss.com/jstree/3.3.3/themes/default/style.min.css
// @match https://share.dmhy.org/topics/view/*
// @grant GM_addStyle
// @grant GM_getResourceText
// @downloadURL none
// ==/UserScript==
var icons = {
unknown: "https://share.dmhy.org/images/icon/unknown.gif",
audio: 'https://share.dmhy.org/images/icon/mp3.gif',
video: 'https://share.dmhy.org/images/icon/mp4.gif',
image: 'https://share.dmhy.org/images/icon/jpg.gif',
text: 'https://share.dmhy.org/images/icon/txt.gif',
rar: 'https://share.dmhy.org/images/icon/rar.gif'
};
var Dictionary = (function () {
function Dictionary() {
this.__data__ = {};
}
Dictionary.prototype.add = function (key, value) {
if (key in this.__data__)
return;
this.__data__[key] = value;
};
Dictionary.prototype.clear = function () {
this.__data__ = {};
};
Dictionary.prototype.containsKey = function (key) {
return key in this.__data__;
};
Dictionary.prototype.get = function (key) {
return this.__data__[key];
};
Dictionary.prototype.size = function () {
return Object.keys(this.__data__).length;
};
Dictionary.prototype.values = function () {
return this.__data__;
};
return Dictionary;
}());
var TreeNode = (function () {
function TreeNode(node) {
this.__name__ = node;
this.__childNode__ = new Dictionary();
}
TreeNode.prototype.add = function (key, value) {
this.__childNode__.add(key, value);
return this.__childNode__.get(key);
};
TreeNode.prototype.insert = function (path, size) {
var currentNode = this;
for (var _i = 0, path_1 = path; _i < path_1.length; _i++) {
var node = path_1[_i];
var next = currentNode.__childNode__.get(node);
if (!currentNode.__childNode__.containsKey(node)) {
next = currentNode.add(node, new TreeNode(node));
next.__pareneNode__ = currentNode;
}
currentNode = next;
}
currentNode.__size__ = size;
return currentNode;
};
TreeNode.prototype.getIcon = function (ext) {
var icon = icons.unknown;
if (ext == "mkv" || ext == "mka" || ext == "mp4")
icon = icons.video;
else if (ext == "mp3" || ext == "aac" || ext == "flac")
icon = icons.audio;
else if (ext == "txt" || ext == "log" || ext == "cue")
icon = icons.text;
else if (ext == "rar" || ext == "zip" || ext == "7z")
icon = icons.rar;
else if (ext == "jpg" || ext == "jpeg" || ext == "bmp" || ext == "webp")
icon = icons.image;
return icon;
};
TreeNode.prototype.toObject = function () {
var ret = {};
ret.text = this.__name__;
ret.children = [];
ret.state = { opened: true };
for (var key in this.__childNode__.values()) {
var files = [];
var value = this.__childNode__.get(key);
if (value.__childNode__.size() === 0) {
files.push(value);
}
else {
var tmp = {};
tmp.text = value.__name__;
tmp.children = [];
var inner = value.toObject();
for (var _i = 0, _a = inner.children; _i < _a.length; _i++) {
var innerNode = _a[_i];
tmp.children.push(innerNode);
}
ret.children.push(tmp);
}
for (var _b = 0, files_1 = files; _b < files_1.length; _b++) {
var file = files_1[_b];
var ext = file.__name__.substr(file.__name__.lastIndexOf('.') + 1).toLowerCase();
ret.children.push({
icon: this.getIcon(ext),
text: file.__name__ + "\t" + file.__size__
});
}
}
return ret;
};
return TreeNode;
}());
var GM_getResourceText = GM_getResourceText;
var GM_addStyle = GM_addStyle;
(function () {
'use strict';
var newCSS = GM_getResourceText("customCSS");
GM_addStyle(newCSS);
$('head').append('');
$('#tabs-1').append('');
$('.file_list').css('width', '100%');
$('.file_list').css('max-height', '600px');
var data = new TreeNode($('.topic-title > h3').text());
$('.file_list > ul li').each(function (index) {
var text = $(this).text();
var line = text.trim().replace(/\t+/i, '\t').split('\t');
var nodes = line[0].split('/');
var size = line[1];
data.insert(nodes, size);
});
$('.file_list').jstree({
core: { data: data.toObject() },
plugins: ["search"]
});
var id = 0;
$('#search_input').keyup(function () {
if (id) {
clearTimeout(id);
}
id = setTimeout(function () {
$('.file_list').jstree(true).search($('#search_input').val());
}, 250);
});
})();