I've been working on an extension for Chrome.
In order to work, it must pass a global window variable set by the website to the extension's main script using a CustomEvent.
This usually works fine when the page is loaded in an active tab, but whenever it's loaded as a non-focused tab, or when the execution of the extension script is otherwise delayed, the event object received by the listener has a null detail attribute.
Just to be clear, the entire detail object of the event is null, not just the needed variable. I've made a workaround for this by attaching the needed variable to the document as a JSON string, but I'd like to understand what's wrong with the event.
Relevant parts of code are below.
manifest.json:
"content_scripts": [ {
"js": [ "main.js", "jquery-3.3.1.js"],
"matches": [ "*://*.site.com/*"],
"run_at": "document_end"
} ],
"permissions": [ "declarativeContent", "activeTab", "storage", "*://*.site.com/*" ],
"web_accessible_resources": [ "injectedScript.js" ]
main.js:
$(function() {
document.addEventListener('globalPasser', function(response) {
if(response.detail){
// store variables
}
}, true);
var passGlobals = document.createElement('script');
passGlobals.src = chrome.extension.getURL('injectedScript.js');
(document.head||document.documentElement).appendChild(passGlobals);
passGlobals.onload = function() {
passGlobals.remove();
};
}
injectedScript.js:
var newEvent = new CustomEvent('globalPasser', {detail:{variable: neededGlobalVariable}});
document.dispatchEvent(newEvent);