使用Javascript单击给定的按钮并在新窗口中打开它 [英] Using Javascript to click on a given button AND have it open in a new window

查看:105
本文介绍了使用Javascript单击给定的按钮并在新窗口中打开它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When this page loads, I want to simulate automatically clicking the send me this thing button whilst having it open in a new window (or new tab)

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

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

This is for Greasekit on Fluid.app, which is similar to Greasemonkey.

推荐答案

该Button是一个链接,并且该页面使用jQuery.这样您就可以得到如下按钮:

That Button is a link and that page uses jQuery. So you can get the button like:

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

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

Then modify the link to open in a new tab/window.
Then click the link.

代码是:

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

,请注意,现代弹出窗口阻止程序将阻止此操作,我不知道您的Fluid版本中嵌入的Webkit.告诉您的弹出窗口阻止程序允许这些特定的弹出窗口.

BUT, beware that modern popup blockers will block this, I don't know about the webkit embedded in your version of Fluid. Tell your popup blocker to allow these particular popups.

而且,因为这是Greasekit,所以您需要注入上面的代码,因此完整的用户脚本将类似于:

Also, because this is Greasekit, you need to inject the above code, so the complete userscript will be something like:

// ==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单击给定的按钮并在新窗口中打开它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆