// ==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.1.1 // @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 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.toObject = function () { var ret = {}; ret.text = this.__name__; ret.children = []; ret.state = { opened: true }; var files = []; for (var key in this.__childNode__.values()) { 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]; ret.children.push({ text: file.__name__ + "\t" + file.__size__ }); } files = []; } return ret; }; return TreeNode; }()); (function() { 'use strict'; var newCSS = GM_getResourceText ("customCSS"); GM_addStyle (newCSS); $('head').append(''); var list = $('.file_list > ul li'); var data = new TreeNode($('.topic-title > h3').text()); $(list).each(function (index) { var text = $(this).text(); text = text.trim('\t').replace(/\t+/i, '\t').split('\t'); var nodes = text[0].split('/'); var size = text[1]; data.insert(nodes, size); }); $('.file_list').jstree({ core: { data: data.toObject() } }); })();