The problem is that Youtube updates the page dynamically, so the content script wasn't always being run after page content is changed.
You need to detect if page url has been changed.
There is two ways for detect content changing.
Solution
- Use chrome.webNavigation.onHistoryStateUpdated event to detect that the content is changed.
You need to set permissions for webNavigation in manifest.json:
"permissions": [
*"tabs", "webNavigation"*
]
background.js
chrome.webNavigation.onHistoryStateUpdated.addListener(function(details) {
//Send message to content Script -> Page was changed
//or execute parser from here
// chrome.tabs.executeScript
});
content.js
// do pars your content
- Use Mutation Observers: at your content script.
The MutationObserver interface provides the ability to watch for changes being made to the DOM tree.
// Select the node that will be observed for mutations
var targetNode = document.getElementById('some-id');
// Options for the observer (which mutations to observe)
var config = { attributes: true, childList: true, subtree: true };
// Callback function to execute when mutations are observed
var callback = function(mutationsList) {
for(var mutation of mutationsList) {
if (mutation.type == 'childList') {
// do something with content
}
else if (mutation.type == 'subtree') {
// do something with content
}
}
};
// Create an observer instance linked to the callback function
var observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
observer.observe(targetNode, config);
// Later, you can stop observing
observer.disconnect();