Skip to content

Instantly share code, notes, and snippets.

@myfonj
Last active July 24, 2023 22:09
Show Gist options
  • Save myfonj/045d42da5179cc93b3e90eaff0545d4c to your computer and use it in GitHub Desktop.
Save myfonj/045d42da5179cc93b3e90eaff0545d4c to your computer and use it in GitHub Desktop.
Bookmarklet using window.prompt as a REPL interface for current page. Keywordable.
javascript: (() => {
let doPrompt = true;
let promtLabel = '';
let promptValue = '%s';
if (promptValue == '\u0025s') {
promtLabel = 'Eval:';
promptValue = '/* Expression like */ 1 + 1';
} else {
doPrompt = false;
}
do {
try {
if (doPrompt) {
promptValue = prompt(promtLabel, promptValue);
if (promptValue === null) {
break;
}
} else {
doPrompt = true;
}
let result = eval(promptValue);
promtLabel = promptValue + '\n=\n' + result;
promptValue = promptValue/*.replace(/ \/\/= .*?$/, '') + ' //= ' + result;*/;
} catch (e) {
promtLabel = e;
}
} while (true);
})()
@myfonj
Copy link
Author

myfonj commented Jul 13, 2023

  • For "I don't feel like launching devtools just to execute simple onleliner, like document.forms[0].method = 'GET'" or such.
  • OK (enter) evaluates input (prompt).
  • Next prompt displays previous input and result (or error) and prefills the result into input.
  • Cancel (escape) exits.
  • (!) won't work for pages with strict CSP (like GitHub), as any other bookmarklet (https://bugzilla.mozilla.org/show_bug.cgi?id=866522).
  • "Keywordable" means if you set a keyword like ! then you can do ! 1 + 1 (enter) in URL bar and get the result. Again, only if page context permits execution and eval. So you can do ! document.forms[0].method = 'get' (enter)(escape), or continue further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment