microjungle JsonML to DocumentFragment

Based on https://github.com/deepsweet/microjungle by Kir Belevich

Dieses Skript sollte nicht direkt installiert werden. Es handelt sich hier um eine Bibliothek für andere Skripte, welche über folgenden Befehl in den Metadaten eines Skriptes eingebunden wird // @require https://update.greasyfork.org/scripts/25520/241821/microjungle%20JsonML%20to%20DocumentFragment.js

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

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

/*exported MicrojungleJsonMLtoDocumentFragment*/
const MicrojungleJsonMLtoDocumentFragment = function () {
	"use strict";
	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) {
				const attrList = item.shift();
				Object.entries(attrList).forEach(([attrName, attrValue]) => {
					if (stringOrFinite(attrValue)) {
						elem.setAttribute(attrName, attrValue);
					}
				});
			}
			target.appendChild(frag(item, elem));
		}
		template.forEach(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;
	};
};