User:Suffusion of Yellow/wikilint.js

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(` Show  my   all   no  problems. `				);

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		}); } });