0

I'm building a chrome extension that has to click on a button on the webpage.

I can click the button from the console like this.

document.querySelector('#btn').click();

But, Where I try to do the same from content script, I get the following error.

Refused to run the JavaScript URL because it violates the following Content Security Policy directive: "script-src 'self' 'wasm-unsafe-eval'". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution. Note that hashes do not apply to event handlers, style attributes and javascript: navigations unless the 'unsafe-hashes' keyword is present.

The button element is selected correctly, but clicks doesn't work.

I also tried this one:

let clickEvent = new MouseEvent("click", {
  bubbles: true,
  cancelable: true,
  clientX: 150,
  clientY: 150,
});

document.querySelector('#btn').dispatchEvent(clickEvent);

But, got the same error. While this also works form console, but not from extension content_sript.

What is the possible solution for this? Thanks in advance.

  • It's a [bug in Chrome](https://crbug.com/1299742). The workaround is to run this code in [page context](/a/9517879). – wOxxOm Aug 22 '22 at 19:51
  • Thanks @wOxxOm! For others, simply run the content script in "MAIN" world. (You can change in the manifest) – Ghulam Hassan Jul 29 '23 at 05:10

0 Answers0