简体   繁体   English

单击油脂猴子中的按钮

[英]click on button in greasemonkey

<a class="js-open-panel action-button" target="video-download" href="#">
        <em><span class="icon-16x16 icon-download"></span> Download</em></a>

xpath using firebug /html/body/div[2]/div[2]/section[3]/a[4] 使用Firebug的xpath / html / body / div [2] / div [2] / section [3] / a [4]

i want to click this button but no thing worked all the """ getElementBy """ 我想单击此按钮,但所有“”“ getElementBy”“”都无济于事

i read a book big size by searching but nothing worked 我通过搜索阅读了一本大书,但没有任何效果

/*
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);
*/

As long as the button is not loaded by AJAX, the following should work: 只要按钮不是由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);


Note that querySelector() works off of CSS selectors (which Firebug will also show) versus the XPath. 请注意, querySelector()与CSS选择器(Firebug也将显示)相对于XPath均有效。



From the comments, it sounds like AJAX is not loading the button (but it may be used to activate it). 从注释中,听起来好像AJAX没有加载按钮(但是可以用来激活它)。

Use this complete script to start. 使用此完整脚本开始。 Change nothing except the @include directive. 除了@include指令,什么都@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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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