addEventListener in content script not working addEventListener in content script not working google-chrome google-chrome

addEventListener in content script not working


Content scripts do not run in the scope of the current page. The event handler has to be injected via another <script> tag, as described in this answer: Building a Chrome Extension with Youtube Events:

var actualCode = 'function onytplayerStateChange() {'               + '    console.log("The state of the player has changed");'               + '}';var script = document.createElement('script');script.textContent = actualCode;(document.head||document.documentElement).appendChild(script);script.parentNode.removeChild(script);

PS. The DOM is available to the content script, so binding the event handler does work.