1

I am a JavaScript noob, pardon if my question is too silly or ill stated.

I am trying to figure out how to write a GreaseMonkey script that would detect whether there is a monaco editor on a webpage, and if there is one, then it would try to retrieve its contents. It seems to be easy to do as long as I have an instance of Monaco editor, but I am struggling to figure out how to find it from within GreaseMonkey script.

Basically, here is an example page with Monaco: https://app.coderpad.io/sandbox

From what I understand, a line like this is executed somewhere to initialize Monaco:

let editor = monaco.editor.create.create(...);

And then the reference to this editor could either be dropped, or not saved in any standard place. At least I wasn't able to find it in document.* or window.*. I only found global variable window.MonacoEnvironment, but it doesn't appear to contain any useful references to editor object itself.

I also read source code of Monaco and found monaco.editor.getEditors() function. But I am JavaScript noob and can't figure out why it doesn't work - in Chrome console:

monaco.editor.getEditors()
Uncaught ReferenceError: monaco is not defined

Is there a way to import monaco namespace to make this expression to work?

In general, how can I find reference to editor object(s) from within GreaseMonkey script, that would work reliably on any webpage using reasonably recent version of Monaco? Or is this even possible?

Thanks in advance for any help!

  • 99% chance this is impossible. Larger apps are usually bundled up and wrapped up in an IIFE, so the only way to do that is to manually alter the JS. – code Aug 14 '22 at 23:50
  • 1
    @code Not impossible. (1) https://stackoverflow.com/questions/11192875/is-it-possible-to-gain-access-to-the-closure-of-a-function (2) Sometimes editors like these are explicitly added as a property of an element - if you can access the element, you can access the editor. Monaco may work the same way (or maybe not). – CertainPerformance Aug 14 '22 at 23:52
  • 1
    At first glance, I don't see the editor being accessible outside, so I'd use the technique described https://stackoverflow.com/questions/11192875/is-it-possible-to-gain-access-to-the-closure-of-a-function to overwrite the code to add a line `window.theEditor = editor` or something like that – CertainPerformance Aug 14 '22 at 23:58
  • @CertainPerformance, This trick might work if given code is present inside html page. However in my case javascript is loaded from another URL via `` - can your method be extended to this case? – Robert Thyssel Aug 16 '22 at 00:56
  • Yes it can - use a MutationObserver to watch for the attachment of the script to the document, and then you can replace the script's `src` with a blank one, and set its `.textContent` to your altered script text. More generally, if this is only for *you* personally, you can also use Chrome Local Overrides instead. https://stackoverflow.com/a/59518023 – CertainPerformance Aug 16 '22 at 01:37

0 Answers0