Sleazy Fork is available in English.

議論 » 開発

Tips to adapt GreaseMonkey script for Chrome TamperMonkey?

§
投稿日: 2019/02/20
編集日: 2019/02/20

Tips to adapt GreaseMonkey script for Chrome TamperMonkey?

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.

wOxxOmMod
§
投稿日: 2019/02/20

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.

§
投稿日: 2019/02/20

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?

§
投稿日: 2019/02/20
編集日: 2019/02/20

UPDATE 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.

wOxxOmMod
§
投稿日: 2019/02/21

content isn't a standard thing, probably something ancient.

§
投稿日: 2019/02/21

Okay, great to know, thank you! Removed it from all my scripts.

返信を投稿

返信を投稿するにはログインしてください。