0

I am making an extension for one website and on specific pages there is a button that executes logic that I need to trigger from extension. Lets say page has button <button onclick="page_action('1')">Click me</button>. page_action function is located in separate file that is hosted in CDN, for example https://website.cdn.com/my_script.js and this my_script.js is defined on websites page (head tag).

So my question is how and if its even possible to manually execute page_action from within chrome extension ?

Finding and clicking button element isn't an option cause in some cases this button is hidden.

Tried different variations and one error that I stumbled was related to CSP. This code is even throwing error:

"content_security_policy": {
    "extension_pages": "script-src 'self' https://website.cdn.com; object-src 'self'"
  }
6matko
  • 74
  • 1
  • 8
  • Send a click event: document.querySelector('button').dispatchEvent(new MouseEvent('click', {bubbles: true})) – wOxxOm Oct 25 '21 at 17:13
  • @wOxxOm It would work if the button would be present on page but there are special cases when that button is not rendered and thus no item to dispatch event. – 6matko Oct 25 '21 at 17:50
  • Ah, it seems like `page_action` is a global function in which case you can do it in a script element, [examples](https://stackoverflow.com/a/9517879). – wOxxOm Oct 25 '21 at 19:02
  • Thanks for the idea @wOxxOm but Method 2/3 give me following error: `Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self'". 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.` I'm not sure its issue on my side or its blocked by the content page itself. – 6matko Oct 27 '21 at 18:31
  • As the answer says, only method 1 is compatible with ManifestV3. – wOxxOm Oct 27 '21 at 18:51
  • Yeah. Missed that part, my bad. Now experimenting with Method 1. – 6matko Oct 27 '21 at 18:53

0 Answers0