Inject and Run the script to webpage from chrome extension

514 views
Skip to first unread message

Ali Raza

unread,
Oct 19, 2022, 12:48:33 PM10/19/22
to Chromium Extensions
Hello team.
We are working on our extension's migration from MV2 to MV3.

In MV2, we were injecting a script in all related webpages as
```
const scriptString = 'function runAtStart () { console.log('our code...'); }';
const script = document.createElement('script'); 
script.textContent = `Our code here.....`
(document.body || document.head || document.documentElement).appendChild(script);
```

According to MV3 policy, we can only inject script tags with src attributes.
```
const s = document.createElement('script');
s.setAttribute('src', 'chrome-extension://<id_of_the_extension>/js/payload.js');
document.documentElement.appendChild(s);
```
Also I have tried this via
*chrome.scripting.registerContentScript*

. and now it is shown to the webpage, but unfortunately it is not calling the methods which I have written into it.
```
await chrome.scripting.registerContentScripts([
{
  id: `tabId-${tab.id}`,
  matches: ['https://*/*'],
  js: ['js/payload.js'],
  runAt: 'document_end',
  world: 'MAIN',
}], (e) => console.log(e));
```

And in the payload.js file, I have just written 
``` console.log("TEST"); ```

In both scenarios, it is adding script tag with src in webpage DOM but not executing it.
Is there anything I am missing.
Below are the screenshots of webpage behaviour.


Screenshot 2022-10-19 at 9.47.22 PM.pngScreenshot 2022-10-19 at 9.47.47 PM.pngScreenshot 2022-10-19 at 9.48.19 PM.png

Ali Raza

unread,
Oct 19, 2022, 12:50:34 PM10/19/22
to chromium-...@chromium.org, chromium...@chromium.org

Stefan Van Damme

unread,
Oct 19, 2022, 2:02:52 PM10/19/22
to Chromium Extensions, 14bes...@seecs.edu.pk, chromium...@chromium.org
Hi there,

If you want the resource file to be accessed by web pages, add web_accessible_resources in your manifest file:

Other suggestion:
Have you tried this example, where you can place your JavaScript code in the "changeBackgroundColor()" function:

const color = "blue"
function changeBackgroundColor() {
     document.body.style.backgroundColor = color;
     console.log("hello");
}
const tabId = getTabId();
chrome.scripting.executeScript(
{
     target: {tabId: tabId},
     func: changeBackgroundColor,
},
() => { ... });

Thanks,
Stefan vd

Ali Raza

unread,
Oct 19, 2022, 4:43:39 PM10/19/22
to Chromium Extensions, stefa...@gmail.com, chromium...@chromium.org
Thanks a lot, Stefan vd

It is working fine with your suggested method `executeScript`.
Now my actual task is to add a new property in window object i.e. window.peerConnection.

I am adding it through payload.js file and printing out it. It prints the value but in the console tab when I type window.peerConnection, it shows undefined.

Can you please help me out here as well.
Below are the attached screenshots for better understanding and demo.

Screenshot 2022-10-20 at 1.42.43 AM.pngScreenshot 2022-10-20 at 1.43.09 AM.png

Stefan Van Damme

unread,
Oct 20, 2022, 3:25:12 AM10/20/22
to Chromium Extensions, 14bes...@seecs.edu.pk, Stefan Van Damme, chromium...@chromium.org
Hi there,

It is great to hear that my suggestion works for you.

I was researching your issue about "undefined," and found a Chrome bug report about that too:

Thanks,
Stefan vd

Reply all
Reply to author
Forward
0 new messages