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

Using Javascript to click on a given button AND have it open in a new window

本文关键字:在新窗口中打开 按钮 Javascript 单击 使用      更新时间:2023-09-26

当这个页面加载时,我想模拟在新窗口(或新标签页)中打开时自动单击send me this thing按钮

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

为什么?

这是针对 Fluid.app 上的Greasekit的,类似于Greasemonkey。

该按钮是一个链接,该页面使用 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);
}