EDIT: I managed to resolve my issue this way (not sure how bulletproof it is though)
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);
Snippet from my injected.js
(function (XMLHttpRequest) {
var XHR = XMLHttpRequest.prototype;
var extensionID = '';
XHR.send = function (postData) {
this.addEventListener('load', function () {
var extensionSrc = document.querySelector('script.rmd-block-extension');
if (extensionSrc) {
extensionID = extensionSrc.id;
extensionSrc.remove();
}
} catch (err) {
}
}
}
});
};
})(XMLHttpRequest);
And it worked. I do have my extension ID inside my injected.js file :)
Alternative (better?) version:
content.js
var script = document.createElement('script');
script.src = chrome.runtime.getURL('injected.js');
script.id = chrome.runtime.id;
(document.head || document.documentElement).appendChild(script);
injected.js
var extensionID = '';
var script = document.currentScript;
if (script) {
extensionID = script.id;
script.remove();
}
Initial question
I tried literally everything including this long essay: Access variables and functions defined in page context using a content script but nothing is working..
I use method 1:
var script = document.createElement('script');
script.src = chrome.runtime.getURL('injected.js');
script.onload = function () {
this.remove();
};
(document.head || document.documentElement).appendChild(script);
I need to access my extension id in my web_accessible_resources
files because I need to send some messages there. I can hardcode that but thats not the point. I want to use chrome.runtime.id
result there.
Is there an easy way to do that? Inline scripting is not working (policy error - tried with sha etc but without any success, unless im doing something wrong.. No idea).
Any help appreciated.
Thanks.