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

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

this page加载,我想模拟自动点击 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,你需要注入(inject)上面的代码,所以完整的用户脚本应该是这样的:

// ==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);
}

关于javascript - 使用 Javascript 单击给定按钮并在新窗口中打开它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19579613/

相关文章:

javascript - 我怎样才能改进这个 Javascript 代码来改进它,以便它只适用于来自图像的链接(而不是来自文本的链接)

javascript - 谷歌街景中一个像素离地面的高度/海拔

javascript - 尝试从 javascript 访问服务器端变量

javascript - 编写跨浏览器 Greasemonkey 用户脚本的任何技巧?

javascript - 使用 GM xmlhttpRequest 而不是 iframe 来显示来自外部页面的相关信息

JavaScript (Greasekit) 单击单选按钮框并提交表单

javascript - 将 Canvas 元素绑定(bind)到新元素

php - 在不提交的情况下保存和加载表单中的数据

javascript - 如何在循环中打开弹出窗口并相应地执行回调以返回状态? (Greasemonkey 脚本)

javascript - 我如何确定一个网站是否在 javascript 中出现 404,然后将浏览器重定向到其他地方