Triggering a click event from content script - chrome extension

后端 未结 3 897
小蘑菇
小蘑菇 2020-12-14 23:49

In my chrome extension\'s content script, I click on certain links/buttons on webpages from certain sites. To do that, I use following code in the content script (I embed jQ

3条回答
  •  不思量自难忘°
    2020-12-15 00:14

    Due to browser extension sand-boxing, and basic jQuery functionality, you cannot trigger a non-jQuery click event with trigger or click.

    You can however call the raw DOM element click method, which will act exactly as if the element was clicked with the mouse. Just use [0] to access the DOM element:

    $(css_selector)[0].click();
    

    Although you would seldom need to, you can trigger all matching buttons using the same code in an each. As the this in an each is the DOM element it is quite simple:

    $(css_selector).each(function(){
        this.click();
    });
    

提交回复
热议问题