Này người xa lạ!

Còn chờ gì nữa mà không mau đăng nhập hoặc đăng ký để cùng tham gia thảo luận với cộng đồng!

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,

Bình luận

  • 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.
  • you can write logs everywhere in your scripts to get better traces of what if being done in them:

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

    More info: http://wiki.greasespot.net/Greasemonkey_Manual:Other_Useful_Tools#JavaScript_Console

    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

Đăng nhập hoặc Đăng ký để gửi bình luận.