点击按钮在油脂猴子

click on button in greasemonkey

本文关键字:油脂 猴子 按钮      更新时间:2023-09-26
<a class="js-open-panel action-button" target="video-download" href="#">
        <em><span class="icon-16x16 icon-download"></span> Download</em></a>

xpath使用firebug/html/身体/div [2]/div[2]/[3]/[4]节

我想点击这个按钮,但没有东西工作所有的"" " getElementBy ""

我通过搜索读了一本大号的书,但是没有效果

/*
function clickc(x)
{
var el = document.getElementsByTagName('em')[7];
var evt = document.createEvent("MouseEvents");
evt.initEvent("click", true, true);
el.dispatchEvent(evt);
}
setTimeout (clickc , 1);
*/
/*
document.getElementsByClassName("js-open-panel action-button").click();
*/
/*
function clickc(x)
{
var x = document.getElementsByTagName('em')[7].click();
click(x);
}
setTimeout (clickc , 1);
*/

只要这个按钮不是由AJAX加载的,下面的代码应该可以工作:

var dwnldBttn   = document.querySelector (
    "a.js-open-panel.action-button[target='video-download']"
);
var clickEvent  = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
dwnldBttn.dispatchEvent (clickEvent);


注意,querySelector()使用CSS选择器(Firebug也会显示)而不是XPath。



从注释中,听起来像是AJAX没有加载按钮(但它可能被用来激活它)。

使用这个完整的脚本开始。除了@include指令,什么都不改变。

// ==UserScript==
// @name     _YOUR_SCRIPT_NAME
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
setTimeout (clickDownloadButton, 1111);
function clickDownloadButton () {
    var dwnldBttn   = document.querySelector (
        "a.js-open-panel.action-button[target='video-download']"
    );
    var clickEvent  = document.createEvent ('MouseEvents');
    clickEvent.initEvent ('click', true, true);
    dwnldBttn.dispatchEvent (clickEvent);
}