使用Javascript单击给定按钮并在新窗口中打开它

时间:2013-10-25 01:27:58

标签: javascript greasemonkey greasekit fluid-mac-app-engine

加载this page时,我想模拟自动点击send me this thing按钮,同时在新窗口(或新标签页)中打开它

  • 在这样的任何页面上只会有一个send me this thing按钮

为什么?

这适用于Fluid.app上的Greasekit,类似于Greasemonkey。

1 个答案:

答案 0 :(得分:2)

该Button是一个链接,该页面使用jQuery。所以你可以得到如下按钮:

var buyItBtn = $("a:contains('Send me this thing')");

然后在新标签/窗口中修改要打开的链接 然后单击链接。

代码是:

//-- Note  that :contains() is case-sensitive.
var buyItBtn = $("a:contains('Send me this thing')");
buyItBtn.attr ("target", "_blank");
buyItBtn[0].click ();

但是,请注意现代弹出窗口阻止程序会阻止它,我不知道您的Fluid版本中嵌入的webkit。告诉你的弹出窗口拦截器允许这些特定的弹出窗口。

另外,因为这是Greasekit,你需要注入上面的代码,所以完整的用户脚本将是这样的:

// ==UserScript==
// @name        _Amazon-like store, buy things in a new window
// @include     https://dl.dropboxusercontent.com/u/5546881/*
// @grant       GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
function GM_main () {
    //-- Note  that :contains() is case-sensitive.
    var buyItBtn = $("a:contains('Send me this thing')");
    buyItBtn.attr ("target", "_blank");
    buyItBtn[0].click ();
}

addJS_Node (null, null, GM_main);

function addJS_Node (text, s_URL, funcToRun, runOnLoad) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    if (runOnLoad) {
        scriptNode.addEventListener ("load", runOnLoad, false);
    }
    scriptNode.type                         = "text/javascript";
    if (text)       scriptNode.textContent  = text;
    if (s_URL)      scriptNode.src          = s_URL;
    if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';

    var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
    targ.appendChild (scriptNode);
}