0

I want to run some javascript code on every youtube page. But with this code it only runs if I re-upload the page. And if I just click link on youtube that goes to another youtube link, it does not work. What needs to be changed?

manifest.json

{
    "manifest_version" : 2,
    "name": "Remove ads",
    "version": "0.1.0",
    "description": "Remove ads and suggestions on youtube",

    "content_scripts": [{
       "matches": ["https://www.youtube.com/*"],
       "js": ["content.js"]
    }],
    "web_accessible_resources": ["script.js"]
}

content.js

var s = document.createElement('script');
s.src = chrome.extension.getURL('script.js');
s.onload = function() {
    this.remove();
};
(document.head || document.documentElement).appendChild(s);

script.js

alert("hello");
  • Youtube is a so-called AJAX-navigated site so you need to use events e.g. [How to detect page navigation on Youtube and modify HTML before page is rendered?](//stackoverflow.com/a/34100952) – wOxxOm Apr 19 '17 at 20:46
  • That worked. Thank you! – ValeraKundas Apr 19 '17 at 21:09

0 Answers0