In my content script, I am using this approach to be able to access web page's variables:
function exec(fn) {
var script = document.createElement('script');
script.setAttribute("type", "application/javascript");
script.textContent = '(' + fn + ')();';
document.body.appendChild(script); //run the script
document.body.removeChild(script); //clean up
}
exec( function(){
var test = websiteVar
chrome.runtime.sendMessage("extensionID", {test: test}, function(response) {
});
});
Where it says extensionID, if I hardcode it, it works. However, I can't use chrome.runtime.id
as it is injected in the page. I think I read they don't work in the same context.
Is there a way to get the extension id programatically?