I want to get innerHTML of a current video on youtube. The code below doesn't work on youtube, because changing subpages doesn't cause reload of whole page (when it does, the code works well). After every video change the innerHTML from first visited video is printed in console. The only way to get the current one is to click f5. How can I rewrite the code to make it work?
manifest.json
...
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["index.js"]
}
],
"permissions": ["activeTab", "scripting", "tabCapture", "storage", "tabs"],
...
background.js
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
const regex = /.*youtube.com\/watch\?.*/gm;
if (changeInfo.status === "complete" && !!tab.url.match(regex)) {
chrome.tabs.sendMessage(tabId, {
message: "NEW_VIDEO"
});
}
});
index.js
chrome.runtime.onMessage.addListener((request) => {
if (request.message === "NEW_VIDEO") {
console.log(document.documentElement.innerHTML);
}
});