Last active
May 18, 2021 00:23
-
-
Save jared-hughes/f4d2b604f8f2ccacd23453097c9d83fb to your computer and use it in GitHub Desktop.
Custom function in Desmos, including custom autoOperatorNames. Warning: currently incompatible with other scripts that manipulate the worker, such as desmos god mode.
This file contains 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 Custom Desmos Function | |
// @namespace github.com/jared-hughes | |
// @match *://www.desmos.com/calculator* | |
// @description Custom function in Desmos, including custom autoOperatorNames. Warning: currently incompatible with other scripts that manipulate the worker, such as desmos god mode. | |
// @grant none | |
// @version 0.1.1 | |
// @run-at document-start | |
// @author fireflame241 (Jared Hughes) | |
// ==/UserScript== | |
/* To change the function, ctrl+F for "squiggle" and edit */ | |
// the function definition itself | |
function squiggle(x) { | |
return 8*Math.sin(x) + 3*Math.sin(4*x) | |
} | |
window.Worker = new Proxy(Worker, { | |
construct(target, args) { | |
if (args[0].startsWith("blob:")) { | |
const xhr = new XMLHttpRequest | |
xhr.open("GET", args[0], false) | |
xhr.send() | |
const hooked = xhr.responseText | |
// Define BuiltIn.squiggle to be the above function | |
.replace(/Object.defineProperty\((?<exports>.),"sinh"/g, ` | |
Object.defineProperty($<exports>, "squiggle", { | |
enumerable: true, | |
get: function() { | |
return ${squiggle.toString()} | |
} | |
}), $&`) | |
// Mark squiggle as a builtin that calls BuiltIn.squiggle(x) | |
.replace(/sinh:(?<func>.)\("BuiltIn","sinh"/, `squiggle:$<func>("BuiltIn","squiggle"), $&`) | |
args[0] = URL.createObjectURL(new Blob([hooked])) | |
} | |
return new target(...args) | |
} | |
}) | |
function applyNames() { | |
/* see https://github.com/jared-hughes/DesThree/blob/master/src/View.js#L66 for more info, including forcing a rerender */ | |
const fields = document.querySelectorAll('.dcg-mq-editable-field') | |
fields.forEach(field => { | |
const opt = field._mqMathFieldInstance.__controller.root.cursor.options; | |
// allow squiggle to be written without manipulating LaTeX in the console | |
opt.autoOperatorNames.squiggle = 'squiggle' | |
}) | |
} | |
function initialRerender() { | |
window.Calc.getExpressions() | |
.filter(e => e.type === 'expression') | |
.forEach(e => { | |
const selector = `.dcg-expressionitem[expr-id='${e.id}'] .dcg-mq-editable-field` | |
const mqField = document.querySelector(selector) | |
mqField._mqMathFieldInstance.__controller.renderLatexMath(e.latex) | |
}) | |
} | |
function init() { | |
window.Calc.controller.dispatcher.register(e => { | |
if ( | |
['tick', 'new-expression', 'new-expression-at-end'].includes(e.type) || | |
(e.type === 'on-special-key-pressed' && e.key === 'Enter') | |
) { | |
// update the math field of new expression elements | |
applyNames() | |
} | |
}) | |
applyNames() | |
initialRerender() | |
} | |
const interval = setInterval(() => { | |
if (window.Calc && window.Calc.controller) { | |
clearInterval(interval) | |
init() | |
} | |
}, 50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment