【问题标题】:Using Javascript to click on a given button AND have it open in a new window使用 Javascript 单击给定按钮并在新窗口中打开它
【发布时间】:2013-10-25 01:27:58
【问题描述】:

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

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

为了什么?

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

【问题讨论】:

  • 您是否可以控制将侦听器附加到按钮的过程?

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


【解决方案1】:

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

【讨论】:

  • 谢谢,我实际上能够毫无问题地使用您提供的 3 行代码(换句话说,我不需要做注入的事情)
  • 这很有趣,如果可以的话,看看 Fluid+Greasekit 是自动包含 jQuery(好)还是在页面范围内自动运行脚本(非常坏)。
  • 令人困惑的是,我刚刚又试了一次,而您的答案中没有出现 两个 选项。 (当我尝试它并且有效时我喝醉了吗?)
  • 鉴于您的其他 Q 的答案有效,并且鉴于我在您的测试页面上测试了这个脚本,这个答案将适用于您 - 至少对于测试页面。如果实际页面不同(现在),除非您向我们提供使用它的方法,否则由您决定。就它现在“不起作用”而言,这还不够好。我们需要详细信息和错误消息。错误控制台说什么?您是否调整或禁用了弹出窗口阻止程序?
猜你喜欢
  • 1970-01-01
  • 2015-09-09
  • 1970-01-01
  • 2015-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多