microjungle JsonML to DocumentFragment

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

目前為 2016-12-08 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/25520/162139/microjungle%20JsonML%20to%20DocumentFragment.js

// ==UserScript==
// @name        microjungle JsonML to DocumentFragment
// @description Based on https://github.com/deepsweet/microjungle
// @namespace   https://gf.qytechs.cn/en/users/447-spooky-donkey
// @version     1
// @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;
	};
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址