How to access chrome.runtime.id inside injected files (web_accessible_resources)?

281 views
Skip to first unread message

Xaoo BBX

unread,
May 2, 2023, 2:40:49 PM5/2/23
to Chromium Extensions
Hello there,

is there an easy way to access extension id inside injected files? Tried some things including inline js but nothing is working. I don't want to hardcode that - I'd rather want to get that dynamically. 

Any help appreciated.

Thanks.

Cuyler Stuwe

unread,
May 2, 2023, 3:30:26 PM5/2/23
to Chromium Extensions, Xaoo BBX
Needs a bit more detail because the generic problem is theoretically simple, and it's not immediately obvious how your circumstance is making this difficult.

Xaoo BBX

unread,
May 2, 2023, 4:11:39 PM5/2/23
to Chromium Extensions, Cuyler Stuwe
Actually I came up with an idea and it worked. I will share this so anyone can use that. Not sure how bulletproof it is thou.

This is snippet from my content.js

var script = document.createElement('script');
script.src = chrome.runtime.getURL('injected.js');
script.id = chrome.runtime.id;
script.classList.add('rmd-block-extension');

(document.head || document.documentElement).appendChild(script);

This is snipper from my injected.js file

var extensionSrc = document.querySelector('script.rmd-block-extension');

if (extensionSrc) {
extensionID = extensionSrc.id;
extensionSrc.remove();
}

and this is how I can access extension id. 

If you have better idea let me know. 
Cheers.

Jackie Han

unread,
May 2, 2023, 11:57:43 PM5/2/23
to Xaoo BBX, Chromium Extensions, Cuyler Stuwe
Hi Xaoo, 

Your answer is a general way to pass parameters from content script (ISOLATED world) to MAIN world script.
In this way, you can further simplify your code by using document.currentScript, for example:

// your injected.js




--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/515649f6-b1e7-4285-8636-d3f1aaf814c5n%40chromium.org.

Xaoo BBX

unread,
May 3, 2023, 6:12:32 AM5/3/23
to Chromium Extensions, Jackie Han, Chromium Extensions, Cuyler Stuwe, Xaoo BBX
Tried this approach but in my case document.currentScript is always null. Not sure why though.

Jackie Han

unread,
May 3, 2023, 6:25:59 AM5/3/23
to Xaoo BBX, Chromium Extensions, Cuyler Stuwe
MDN docs say:
It's important to note that this will not reference the <script> element if the code in the script is being called as a callback or event handler; it will only reference the element while it's initially being processed.

That means you can't use it in a callback or async way. For example, you can put it in the first line of your code.
 

Jackie Han

unread,
May 3, 2023, 6:29:43 AM5/3/23
to Xaoo, Chromium Extensions
You're welcome

On Wed, May 3, 2023 at 6:27 PM Xaoo <xao...@gmail.com> wrote:
Yeah, I just figured it out. It works if it's in the first line of the injecged.js file. Thanks!
Reply all
Reply to author
Forward
0 new messages