I have found several quite well up-voted questions here on Stack Overflow, though I can't actually find a working solution.
I've been trying to follow the answer here: Chrome Extension - Get DOM content, but I still get a completely blank console (despite reloading the extension)!
manifest.json:
{
"manifest_version": 2,
"name": "Test Extension",
"version": "0.0",
"background": {
"persistent": false,
"scripts": ["background.js"]
},
"content_scripts": [{
"matches": ["https://*/*"],
"js": ["content.js"]
}],
"browser_action": {
"default_title": "Test Extension",
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": ["activeTab"]
}
background.js:
// A function to use as callback
function logDOM(domContent) {
console.log('I received the following DOM content:\n' + domContent);
}
// When the browser-action button is clicked...
chrome.browserAction.onClicked.addListener(function (tab) {
console.log(tab.url);
chrome.tabs.sendMessage(tab.id, {text: 'report_back'}, logDOM);
});
content.js:
// Listen for messages
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
// If the received message has the expected format...
if (msg.text === 'report_back') {
// Call the specified callback, passing
// the web-page's DOM content as argument
sendResponse(document.all[0].outerHTML);
}
});
Am I doing something wrong?