microjungle JsonML to DocumentFragment

Based on https://github.com/deepsweet/microjungle

Tính đến 09-12-2016. Xem phiên bản mới nhất.

Script này sẽ không được không được cài đặt trực tiếp. Nó là một thư viện cho các script khác để bao gồm các chỉ thị meta // @require https://update.greasyfork.org/scripts/25520/162315/microjungle%20JsonML%20to%20DocumentFragment.js

 // ==UserScript==
// @name        microjungle JsonML to DocumentFragment
// @description Based on https://github.com/deepsweet/microjungle
// @namespace   https://greasyfork.org/en/users/447-spooky-donkey
// @ecmaVersion 6
// @version     2
// @grant       none
// ==/UserScript==

function MicrojungleJsonMLtoDocumentFragment() {
	return function frag(template, target = document.createDocumentFragment()) {
		if (!Array.isArray(template)) {
			return target;
		}
		const stringOrFinite = a => typeof a === "string" ||
			typeof a === "number" && isFinite(a);
		function createElement(item, s = item[1]) {
			const elem = document.createElement(item.shift());
			if (!!s && s.constructor === Object) {
				let attrList = item.shift();
				Object.keys(attrList).forEach(function (attrName) {
					const attrValue = attrList[attrName];
					if (stringOrFinite(attrValue)) {
						elem.setAttribute(attrName, attrValue);
					}
				});
			}
			target.appendChild(frag(item, elem));
		}
		template.forEach(function (item) {
			if (stringOrFinite(item)) {
				target.appendChild(document.createTextNode(item));
			} else if (item) {
				if (typeof item[0] === "string") {
					createElement(item);
				} else if (item.nodeType) {
					target.appendChild(item);
				} else {
					frag(item, target);
				}
			}
		});
		return target;
	};
}