I'm trying to create a Chrome extension, so that I can read it's content when a page is loaded.
document.addEventListener("DOMContentLoaded", function(){
function modifyDOM() {
return "<html>\n".concat(String(document.body.innerHTML),"\n</html>");
}
chrome.tabs.executeScript({
code: '(' + modifyDOM + ')();'
}, (results) => {
let dat=String(results[0]);
console.log(dat);
});
});
But it gives me error saying :
Unchecked runtime.lastError: Cannot access a chrome:// URL
_generated_background_page.html:1 Error handling response: TypeError: Cannot read property '0' of undefined
But the code works fine, when I put the code inside :
chrome.browserAction.onClicked.addListener(function(tab) {
//this works only when I click on that extension icon
...
});
How can I resolve this ?
My code works when I click the button, but I wanted to check for page content change, as I couldn't find any API for that, I tried to do at-least when the page loads.