Tips to adapt GreaseMonkey script for Chrome TamperMonkey?

editoi February 20   Kehitys [?]

I wonder if some of you might have some tips to make GM scripts work on Chrome — or even how to get started in order to debug.

Last week I uploaded my simple Reverso Enhancer Spanish script, which works perfectly in PaleMoon, but on Chrome with TamperMonkey nothing happens.

TamperMonkey is not showing any errors, so I'm not even sure how to start debugging, apart from using a bunch of alert

Thanks in advance for any thoughts.

Viestejä yhteensä

  • For historical compatibility with the old scripts @grant none in Tampermonkey runs the code in the page context - not inside the userscript sandbox - so your global functions and global variables are mixed with the page and may interfere, the opposite can also occur if the page runs some script after yours and defines a property on window with the same name as one of your vars/functions. If that's not your goal, remove @grant none.

    As for debugging, you can use devtools to properly debug the code, set breakpoints, step through the code line by line, inspect the values, etc.

  • Thank you for the precious tips, @wOxxOm!

    Looks like the script is choking early, on this line:
    var this_url = content.document.location
    TamperMonkey reports that content is not defined.

    Is that a namespace problem? Does that mean that some 'vanilla' language needs to be used to check whether the script is running in Chrome, and if so to declare a few variables differently?

  • editoi February 20 [?]

    changed that line to
    var this_url = window.location.toString()
    and… it looks like the basic functionality works.

    Will probably have to tweak some things, but that's a great start.
    Thank you again.

  • `content` isn't a standard thing, probably something ancient.
  • Okay, great to know, thank you! Removed it from all my scripts.

Kirjaudu sisään tai Rekisteröidy kommentoidaksesi.