I would like to take some js variable using a injected script into the DOM. To do so I have two files the one that inject the script into the DOM and the one that send over the value.
getPageSource.js
var s = document.createElement('script');
s.src = chrome.extension.getURL('script.js');
(document.head||document.documentElement).appendChild(s);
s.onload = function() {
s.parentNode.removeChild(s);
};
function getTag(){
document.addEventListener('ITAG_connectExtension', function(e) {
return e.detail;
});}
chrome.extension.sendMessage({
action: "getSource",
source: getTag()
});
script.js
var tagType = {};
tagType = itsClickPI;
setTimeout(function() {
document.dispatchEvent(new CustomEvent('ITAG_connectExtension', {
detail: tagType
}));
}, 0);
However the request.source in the popup.js is undefined.
popup.js
chrome.extension.onMessage.addListener(function(request, sender) {
if (request.action == "getSource") {
message.innerText = request.source;
}
});
Could you give me some lights on here?
Thanks in advance.