Can’t trigger click with jQuery in a Chrome extension

The root cause of the problem is that extension content scripts execute in an isolated world. One of the reasons for this is so that your code does not conflict with the page’s code: for instance, you can use a different version of jQuery.

So, your content script has its own copy of jQuery. The way jQuery’s .click() works is by maintaining a list of event handlers that are triggered by the click..

..and you may see the problem already. The content script’s copy of jQuery is not aware of the page’s copy list of handlers, and cannot trigger them.

That, by the way, explains why it works when you put it in the console – by default, console executes in the page’s context and triggers the page’s copy of jQuery.

There are ways to overcome this, but the most straightforward for your task is to emit a proper DOM event, that will be caught by the page’s code. See this question for an example.

Leave a Comment

tech