Jump to content

User:Suffusion of Yellow/wikilint.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
if (mw.config.get('wgAction') == "edit" || mw.config.get('wgAction') == "submit") $(async function() {
	'use strict';

	mw.loader.load("https://en.wikipedia.org/w/index.php?title=User:Suffusion_of_Yellow/cm-lint.css&action=raw&ctype=text/css", "text/css");

	let worker = new Worker(URL.createObjectURL(new Blob(["importScripts('https://en.wikipedia.org/w/index.php?title=User:Suffusion_of_Yellow/wikilint-worker.js&action=raw&ctype=text/javascript')"], {type : "text/javascript" })));

	let checks = await fetch("https://en.wikipedia.org/w/index.php?title=User:Suffusion_of_Yellow/wikilint-checks.json&action=raw")
		.then(response => response.json());

	worker.postMessage({
		action: "setup",
		checks: checks,
		oldText: $("#wpTextbox1").val()
	});

	let lintConfig = {
		getAnnotations: getAnnotations,
		async: true
	};

	let haveLinter = false;

	window.setInterval(async () => {
		let $cm = $(".CodeMirror");

		if ($cm.length > 0 && $cm[0].CodeMirror && !$cm[0].CodeMirrorActivated) {
			let cm = $cm[0].CodeMirror;

			$cm[0].CodeMirrorActivated = true;

			if (!haveLinter) {
				await mw.loader.getScript("https://en.wikipedia.org/w/index.php?title=User:Suffusion_of_Yellow/cm-lint.js&action=raw&ctype=text/javascript");
				haveLinter = true;
			}

			cm.setOption("gutters", ["CodeMirror-lint-markers"]);
			cm.setOption("lint", lintConfig);

			if (!$(".wikilint-show").length)
				$(".editButtons").append(`
<div style="float:right;">Show <select class="wikilint-show">
  <option value="mine">my</option>
  <option value="all">all</option>
  <option value="none">no</option>
</select> problems.</div>`
				);

			let saved = mw.storage.get('wikilint-show');

			if (saved == "mine" || saved == "all" || saved == "none")
				$('.wikilint-show').val(saved);

			$('.wikilint-show').on("change", event => {
				mw.storage.set('wikilint-show', event.target.value);
				cm.doc.setValue(cm.doc.getValue()); // Force refresh
			});
		}
	}, 1000);

	let callbacks = {};
	let callbackId = 0;

	worker.onmessage = event => {
		if (callbacks[event.data.id]) {
			let show = $('.wikilint-show').val();
			let annotations = [];

			if (show != "none") {
				for(let annotation of event.data.annotations) {
					if (annotation.added || show == "all") {
						annotation.from = CodeMirror.Pos(annotation.from[0], annotation.from[1]);
						annotation.to = CodeMirror.Pos(annotation.to[0], annotation.to[1]);
						annotations.push(annotation);
					}
				}
			}

			callbacks[event.data.id](annotations);

			delete callbacks[event.data.id];
		}
	}

	function getAnnotations(text, callback) {
		callbacks[++callbackId] = callback;

		worker.postMessage({
			action: "getAnnotations",
			text: text,
			id: callbackId
		});
	}
});