简体   繁体   English

单击由 Chrome 扩展程序添加的网页上的按钮

[英]Clicking button on webpage that was added by a Chrome Extension

I am using a chrome extension that dynamically adds a new button to a webpage.我正在使用一个 chrome 扩展程序,它可以向网页动态添加一个新按钮。 The extension works fine, but requires a human to click the added button on the webpage to start.该扩展程序工作正常,但需要人工单击网页上添加的按钮才能启动。 I am trying to create an extension that click the button automatically.我正在尝试创建一个自动单击按钮的扩展程序。

I have tried the code below, but it doesn't seem to work.我已经尝试了下面的代码,但它似乎不起作用。 It interacts with the webpage around it, but not the added button.它与周围的网页交互,但不与添加的按钮交互。

*To be clear, I am not trying to click a button inside an extension popup. *明确地说,我不是要单击扩展弹出窗口中的按钮。 This extension adds a button to the pages HTML as the page loads.当页面加载时,此扩展程序向页面 HTML 中添加一个按钮。

$("#button-id").click()

document.getElementsByClassName("class-name")[0].click()

document.getElementById("button-id").click()

If I call如果我打电话

document.getElementById("button-id")

it returns:它返回:

<button class=​"button-class" tabindex=​"0" title=​"Button Title" id=​"button-id">​</button>​

so the console sees the button, but the click function is not triggering anything.所以控制台看到按钮,但点击功能没有触发任何东西。

Based on the native behavior of the target element you would like to achieve you can or cannot use it programmatically created events.根据您想要实现的目标元素的本机行为,您可以或不能使用它以编程方式创建的事件。 In example if you would like to trigger a click - so simply create a mouse event and dispatch it on target node:例如,如果您想触发点击 - 所以只需创建一个鼠标事件并在目标节点上调度它:

const clickEvent = new MouseEvent('click');
const targetNode = document.getElementById('xxx');
targetNode.dispatch(clickEvent);

Some of the native behavior cannot be triggered with such manually created events.某些本机行为无法通过此类手动创建的事件触发。 Eg native select cannot be opened as the event dispatched on it should have isTrusted field set to true, while with manual creation it will be set to false with property descriptor configurable set to false as well.例如,本机选择无法打开,因为在其上调度的事件应该将isTrusted字段设置为 true,而在手动创建时,它将被设置为 false,并且属性描述符可configurable设置为 false。

My guess is your JS file is being called before the extensions file has run meaning the button has not yet been added to the DOM when your code runs.我的猜测是您的 JS 文件在扩展文件运行之前被调用,这意味着当您的代码运行时按钮尚未添加到 DOM。

If you're able to call your code after the other extension has been run that might be your solution.如果您能够在其他扩展程序运行后调用您的代码,这可能是您的解决方案。 Otherwise try this hacky workaround..否则试试这个 hacky 解决方法..

var buttonTrigger = setInterval( function() {
    var buttonElement = document.getElementById( 'button-id' );
    if ( buttonElement ) {

        var eventType = 'click'; // click | mousedown | mouseup | mouseover ..
        var eventObject = document.createEvent( 'MouseEvents' );
            eventObject.initEvent( eventType, true, false );

        buttonElement.dispatchEvent( eventObject );

        clearInterval( buttonTrigger );
    }
}, 100 );

This will loop every 100ms until the button is found and will trigger the click event.这将每 100 毫秒循环一次,直到找到按钮并触发点击事件。

Working example: https://jsfiddle.net/zkyfv1wj/工作示例: https : //jsfiddle.net/zkyfv1wj/

Another possibility is the extension isn't listening to the click event but the mousedown or mouseup events.另一种可能性是扩展程序不侦听 click 事件,而是侦听mousedownmouseup事件。 To trigger these just change the eventType variable.要触发这些只需更改eventType变量。

¯\\_(ツ)_/¯ ¯\\_(ツ)_/¯

I think you should change run_at field to document_end in manifest.json like below.我认为你应该在manifest.json中将 run_at字段更改为document_end ,如下所示。

  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["jquery.min.js", "your.js"],
      "run_at": "document_end"
    }
  ],

I hope this is helpful for you.我希望这对你有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM