简体   繁体   中英

Greasemonkey script to automatically select, and click, a specific button with non-English characters in the value/selector?

I am trying to get a Greasemonkey script to click a specific button every XY seconds. The button I would like to click has this HTML:

<input value="CSV保存" onclick="getCSVData()" type="submit">

I searched for a solution and found:

setInterval(click, 5000);

function click()
{
 $("#button id").click();
}

But what is my "button id" here?

The script basically should download a CSV file every 5 minutes .

Edit: The latest code I tried:

// ==UserScript==
// @name        autoclick
// @namespace   yy
// @description yy
// @include     http://...
// @version     1
// ==/UserScript==

setInterval(click, 1000);

function click()
{
 $("[value = 'CSV保存']").click();
}

Also I used notepad++ with "Big5(traditonal)" coding, because of the Chinese ideographs. (perhaps a mistake?)

Thanks again!

Several things:

  1. The language encoding might be a factor, more below.

  2. jQuery .click() fails to work in a wide variety of Greasemonkey-script scenarios. Use MouseEvents as in this answer .

  3. Be alert for AJAX. Is that <input> added or modified dynamically? If so, use waitForKeyElements as in the previously linked answer .

  4. Since the input actually triggers a javascript function, rather than trying to click, it usually is enough just to call the function directly. Like so:

     unsafeWindow.getCSVData(); 

    See, also, "Generate Click Events" in the GM docs .

  5. Rather that rely on problematic characters like CSV保存 for your jQuery selector, use more of the page's surrounding structure (which your question should show). For example, maybe:

     var targSubmit = $("form.foo div.bar input[type=submit]:eq(2)"); 

    Link to the target page and/or provide the actual HTML for help choosing selectors.

  6. As mentioned in other answers, that is the wrong interval for 5 minutes. Use 5 x 60 x 1000, or 300000 .

  7. Don't use common or keywords as function or global variable names. click is too generic and function click(){... could potentially override window.click depending on your script's injection.

  8. Don't forget the @grant directive .


Putting it all together, this should work:

// ==UserScript==
// @name        autoclick
// @namespace   yy
// @description yy
// @include     http://...
// @version     1
// @grant       none
// ==/UserScript==

setInterval (clickSpecialSubmit, 5 * 60 * 1000); // 5 minutes

function clickSpecialSubmit () {
    unsafeWindow.getCSVData ();
}


Or possibly replace clickSpecialSubmit with something like:

function clickSpecialSubmit () {
    // THIS NEXT LINE MUST BE TUNED TO MATCH YOUR ACTUAL PAGE!
    var targSubmit  = $("form.foo div.bar input[type=submit]:eq(2)");

    var clickEvent  = document.createEvent ('MouseEvents');
    clickEvent.initEvent ('click', true, true);
    targSubmit[0].dispatchEvent (clickEvent);
}




I can't get a Greasemonkey script to work using Big5(traditonal) and containing those characters. Firefox reports "illegal character" in the error console.

It could just be my test system, but note that Greasemonkey scripts must be valid UTF-8. From the source code :

error.scriptCharset=Error reading script: All Greasemonkey scripts MUST be encoded with UTF-8.

The setInterval call is definitely the bread-and-butter of your infrastructure here. It sets up a function to be called every n milliseconds repeatedly, so this is exactly what you'd want to do (though you want to pass in 300000 for the duration - 5000 is every five seconds ).

The example you've given uses jQuery, and assumes that the HTML element in question will have a convenient id attribute identifying it, which your tag doesn't. So, depending on the layout of the page and what unique and stable ways there are to identify the element you're interested in, you'll need to use an appropriate selector to point to it.

Maybe $("[value = 'CSV保存']") depending on whether this is unique enough, and doesn't change over time.

Once you have it selected, the .click() call will click it as expected. And with this running in an interval, this will happen every 5 minutes (or whatever timeout you specified)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM