I want to send data from content.js to popup.js, The content.js is just grabbing the document title and then passing it to the popup.js.
So then popup.js will change the popup.html DOM.
manifest.json:
{
"browser_action": {
"default_icon": {
"64": "icons/icon64.png"
},
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"content.js"
],
"run_at": "document_end"
}
],
"permissions": [
"tabs",
"activeTab",
"*://*/*"
]
}
popup.html:
<html>
<body>
<span class="info">TAB TITLE</span>
<script src="popup.js"></script>
</body>
</html>
content.js:
console.log('CONTENT IS RUNNING')
var getTitle = function() {
return document.title
}
chrome.runtime.sendMessage(getTitle());
popup.js:
console.log('POPUP IS RUNNING')
chrome.runtime.onMessage.addListener(
function(response, sender, sendResponse) {
var title = response;
return title;
}
);
document.querySelector('.info').innerHTML = title; // error: title is not defind
In popup.js the response parameter is not giving the document title.