Last active
January 1, 2021 09:30
-
-
Save razetime/053c80f62b8b820b69721b18cdfc27eb to your computer and use it in GitHub Desktop.
Userscript for executing apl code in chat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name APL chat | |
// @version 2 | |
// @grant GM_xmlhttpRequest | |
// @grant GM_listValues | |
// @match https://chat.stackexchange.com/* | |
// @run-at document-start | |
// @require https://gitlab.com/n9n/apl/-/raw/master/apl.js | |
// ==/UserScript== | |
// thanks to @cvzi for making it work correctly! | |
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver; | |
var observer = new MutationObserver(function(mutations, observer) { | |
// fired when a mutation occurs | |
console.log(mutations, observer); | |
let codes = document.getElementsByTagName("code"); | |
for (let elem of codes) { | |
if(elem.innerText && !('interpreted' in elem.dataset) && elem.innerText[0] == '⋄') { | |
elem.dataset.interpreted = true; // see https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes#JavaScript_access | |
let result = '' | |
let color = 'red' | |
// Catch apl error and show it in orange color | |
try { | |
result = apl.fmt(apl(elem.innerText)).toString() | |
} catch(e) { | |
result = e.toString() | |
color = 'orange' | |
} | |
let tmp = document.createElement("div"); | |
tmp.innerHTML = "<pre style=\"color:"+color+"\">"+result.replace("\n","<br>")+"</pre>"; | |
let parent = elem.parentElement; | |
parent.appendChild(tmp.firstChild); | |
//console.log(result); | |
} | |
} | |
}); | |
window.addEventListener('DOMContentLoaded', (event) => { | |
alert = function() {}; // Prevents ⎕← and ⍞← from trggering alerts | |
window.alert = function(){}; | |
observer.observe(document, { | |
subtree: true, | |
ChildList:true, | |
attributes: true | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Chrome version: