Sleazy Fork is available in English.

Дискусии » Разработка

Need help to run my script in Greasmonkey

§
Публикуван на: 13.07.2018

Need help to run my script in Greasmonkey

Hi Everyone,

I'm trying to run the following script on a webpage via Greasmonkey:

var button = document.querySelector(".Whatever Button"); setInterval(function(){ button.click() },200)) . Basically this script should keep clicking a button on a webpage.

The script works just fine from Mozilla developer console. However, it does nothing on a webpage via Greasmonkey. Please help me to figure out why.

Thank you,

§
Публикуван на: 01.08.2018

It's possible the element you're trying to find doesn't exist yet. Add an 'if' statement to check if it exists...

var button = document.querySelector(".Whatever Button");
if (button) {
  setInterval(function() {
    button.click();
  }, 200);
} else {
  console.warn(".Whatever Button doesn't exist");
}

A lot of web sites these days dynamically generate their HTML code, so you may need to wait for it to appear. A few options I can think of...

  • Run the script when the site is "idle" (after all resource and scripts have loaded and run), by adding this to your metadata block: // @run-at document-idle
  • Use setTimeout() to run after a few seconds, or perhaps use setInverval() to check if the element exists, and then when it's found, do whatever you need to do.
  • And lastly, the best method, the one I prefer using myself, is to use a MutationObserver to have the browser tell you when the element has been added.
§
Публикуван на: 24.11.2018

you can write logs everywhere in your scripts to get better traces of what if being done in them:

GMlog("Hello, World!"); http://wiki.greasespot.net/GMlog

More info: http://wiki.greasespot.net/GreasemonkeyManual:OtherUsefulTools#JavaScriptConsole

Another tip: Take a look at the whole greasemonkey wiki. They have a lot of good stuff in there: http://wiki.greasespot.net/Main_Page

Публикувайте отговор

Влезте, за да публикувате отговор.