Javascript 如何在页面加载时让 Chrome 扩展程序自动点击按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11387615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:41:15  来源:igfitidea点击:

How can I make a Chrome extension automatically click a button when a page loads?

javascriptbuttongoogle-chrome-extensioncontent-script

提问by Cogneti Base

I'm trying to create a Google Chrome extension that presses a button on page load. I've been able to trigger the button using VBscript for Internet Explorer by using this code:

我正在尝试创建一个在页面加载时按下按钮的 Google Chrome 扩展程序。通过使用以下代码,我已经能够使用 VBscript for Internet Explorer 触发按钮:

IE.Navigate("javascript:changeIframe();")

But, now I need to do it in Google Chrome via an extension. However, the button does not have an id:

但是,现在我需要通过扩展程序在 Google Chrome 中执行此操作。但是,按钮没有 id:

<input type="button" value="Start!" onclick="javascript:changeIframe();">

and here's what I've tried so far and nothing seems to work:

这是我迄今为止尝试过的,但似乎没有任何效果:

window.onload="javascript:changeIframe()";
javascript:changeIframe();
document.getElementById('').click();
document.getElementById('').submit();

It seems that .clickand .submitis not defined in Google-Chrome's javascript?

似乎Google-Chrome 的 javascript 中没有定义.click.submit定义?

The weird thing is that when I use this script:

奇怪的是,当我使用这个脚本时:

javascript:changeIframe();

in Chrome's javascript console, it presses the button automatically and everything works fine. But, when I put the same script in my .js file it does not work.

在 Chrome 的 javascript 控制台中,它会自动按下按钮,一切正常。但是,当我将相同的脚本放入我的 .js 文件时,它不起作用。

I just need to know what I have to put inside my .js in order to press the button automatically on page load.

我只需要知道我必须在 .js 中放入什么,以便在页面加载时自动按下按钮。

回答by Brock Adams

The traditional way to do this is to injectthe code1:

传统的方法是注入代码1

var scriptNode          = document.createElement ('script');
scriptNode.textContent  = 'changeIframe ();';

document.body.appendChild (scriptNode);


You won't normally have to listen for onloadeither, as content scripts will fire roughly at that point by default.


您通常不必侦听onload这两种情况,因为默认情况下,内容脚本将在那时大致触发。





1The extension JS operates in an "Isolated World" and cannot interact with the page's javascript without some tricks that are not needed here.

1扩展 JS 在“孤立的世界”中运行,如果没有一些这里不需要的技巧,就无法与页面的 javascript 交互。

回答by PAEz

Great that you got a solution, but thought Id show you another way of simulating a click.
With this way you dont have to inject code or know the function that the onclick will run, just get the button and simulate a click....

很高兴您找到了解决方案,但我认为 Id 向您展示了另一种模拟点击的方式。
通过这种方式,您不必注入代码或知道 onclick 将运行的功能,只需获取按钮并模拟点击....

// https://developer.mozilla.org/en/DOM/element.dispatchEvent
function simulateClick(obj) {
  var evt = document.createEvent("MouseEvents");
  evt.initMouseEvent("click", true, true, window,
    0, 0, 0, 0, 0, false, false, false, false, 0, null);
  var canceled = !obj.dispatchEvent(evt);      
/*

  if(canceled) {
    // A handler called preventDefault
    alert("canceled");
  } else {
    // None of the handlers called preventDefault
    alert("not canceled");
  }
  */
}

var what = document.querySelector('input[type="button"][value="Start!"]');
simulateClick(what);