admin管理员组文章数量:1336188
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!
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!
Share edited Oct 18, 2012 at 19:33 Brock Adams 93.7k23 gold badges241 silver badges305 bronze badges asked Oct 18, 2012 at 9:46 Stefan HubertStefan Hubert 351 silver badge7 bronze badges2 Answers
Reset to default 4Several things:
The language encoding might be a factor, more below.
jQuery
.click()
fails to work in a wide variety of Greasemonkey-script scenarios. UseMouseEvents
as in this answer.Be alert for AJAX. Is that
<input>
added or modified dynamically? If so, usewaitForKeyElements
as in the previously linked answer.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.
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.
As mentioned in other answers, that is the wrong interval for 5 minutes. Use 5 x 60 x 1000, or
300000
.Don't use mon or keywords as function or global variable names.
click
is too generic andfunction click(){...
could potentially overridewindow.click
depending on your script's injection.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)
本文标签:
版权声明:本文标题:javascript - Greasemonkey script to automatically select, and click, a specific button with non-English characters in the values 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742401165a2467910.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论