I'm trying to send the DOM as a message from the contentscript to the backgroundjs, but it doesn't seem like anything is sending. In my extension, I'm adding a button to a link, then on click, it will get a preview of the website.
I've read through dozens of other links that seem to have the same problem, and I've tried most of those solutions but it still silently fails.
manifest.json
{
"name": "PreviewMe",
"version": "0.0.1",
"manifest_version": 2,
"description": "Test",
"permissions": [
"<all_urls>"
],
"background": {
"scripts": ["js/background.js"],
"persistent": true
},
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"js/contentscript.js"
]
}
]
}
contentscript.js
chrome.extension.sendMessage({}, function(response) {
var readyStateCheckInterval = setInterval(function() {
if (document.readyState === "complete") {
clearInterval(readyStateCheckInterval);
// do stuff to initialize
}
}, 10);
});
// send dom over to background.js
chrome.runtime.sendMessage({dom: document.body.innertHTML});
background.js
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
console.log('HELLO WORLD');
});
I put the hello world console log to test, but even that isn't showing up.
Thanks.
EDIT: I thought my code was silently failing but turns out it's just logged to a separate console. Didn't know there was a separate console for background pages.