User:Erutuon/scripts/templatePreview.js

/* * Saves and retrieves the name of the last page that was previewed with the * "Preview page with this template" feature on the current template or module * page, or puts in the name of the documentation page. * * So if you're editing a module and previewing a testcases page, you only have * to type in the name of the testcases page once, not every time you load the * edit page. */ (function templatePreviewIIFE { "use strict";

$.getScript("//en.wiktionary.org/w/index.php?title=User:Erutuon/scripts/storageUtils.js&action=raw&ctype=text/javascript",	function {		var StorageWrapper = MyStorage.StorageWrapper;		if (!(StorageWrapper && StorageWrapper.prototype.storageAvailable)) {			console.log("Storage is not functioning, so TemplatePreview.js will not run.");			return;		}		function TemplatePreviewStorage(key) {			this.key = key;		}		TemplatePreviewStorage.prototype = Object.create(StorageWrapper.prototype, { rawStorage: { get: function { return StorageWrapper.prototype.get.call(this, this.key); },				set: function (value) { return StorageWrapper.prototype.set.call(this, this.key, value); },			},			// Get and save storage in JSON format. storage: { get: function { try { const previewPageNames = this.rawStorage; if (previewPageNames) { const previewPageNamesObject = JSON.parse(previewPageNames); // possible error if (previewPageNamesObject) return previewPageNamesObject; }					} catch (e) { console.log("Error parsing storage:", e); }					return {}; },				set: function (previewPageNamesObject) { try { const previewPageNames = JSON.stringify(previewPageNamesObject); // possible error this.rawStorage = previewPageNames; } catch (e) { console.log("Error stringifying object: ", e); }				},			},			get: { value: function (templateName) { return this.storage[templateName]; },			},			set: { value: function (templateName, previewPageName) { const storage = this.storage; storage[templateName] = previewPageName; this.storage = storage; },			},		});		const getDefaultPreviewPage = function (pageName) {			pageName = pageName.replace(/_/g, " ");			return (!/\/documentation$/.test(pageName))				? pageName + "/documentation"				: pageName;		};		function doIt {			const button = $("#wpTemplateSandboxPreview input");			const box = $("#wpTemplateSandboxPage input");			if (!(box.length > 0 && button.length > 0))				return;			const storage = new TemplatePreviewStorage("previewPage");			window.templatePreviewStorage = storage;			const pageName = mw.config.get("wgPageName");			if (box.val === "") {				const pageToPreview = storage.get(pageName) || getDefaultPreviewPage(pageName);				box.val(pageToPreview);			}			button.click(function { storage.set(pageName, box.val); });		}		$(doIt);	});

});