0

The problem

This content script fires on Twitch's "choose a channel" page, but when you choose the channel, it doesn't fire. How do I make it fire on the channels page when redirected from "choose a channel" page? Thanks.

https://i.stack.imgur.com/ykZmP.png

https://i.stack.imgur.com/vhWnP.png


The code

manifest.json

{
   "manifest_version": 2,
   "name": "stuff",
   "version": "1.0",
   "description": "desc",

   "content_scripts": [ 
       {
          "js": [ "script.js" ],
          "matches": [ "http://*.twitch.tv/*" ],
          "run_at": "document_end"
       }
   ]
}

script.js

function init()
{
    alert("Script executed");
}

init();
Mark Doe
  • 11
  • 3
  • possible duplicate of [Chrome extension is not loading on browser navigation at YouTube](http://stackoverflow.com/questions/18397962/chrome-extension-is-not-loading-on-browser-navigation-at-youtube) – abraham Nov 24 '14 at 20:53
  • Completely different questions. – Mark Doe Nov 24 '14 at 20:59
  • It's the same reason. Just different URLs. – abraham Nov 24 '14 at 20:59
  • I've checked it with chrome.webNavigation.onHistoryStateUpdated and window.onpopstate. Twitch doesn't use history api. – Mark Doe Nov 24 '14 at 21:08
  • `window.onpopstate` won't fire since, well, it's not being popped, only pushed. Please include the code you tried with `onHistoryStateUpdated`. – Xan Nov 25 '14 at 12:44
  • added `"webNavigation"` to permissions in *manifest* and added the following code to *bg.js* `chrome.webNavigation.onHistoryStateUpdated.addListener(function(details) { console.log('executed'); });` – Mark Doe Nov 25 '14 at 14:12

1 Answers1

0

Alright, I have solved the problem. I'm answering my own question in case other people would need this. I've used a script called waitForKeyElements.js which was originally made for Greasemonkey, but apparently it can also be used in the chrome extensions. I hope this will help someone.

Question closed.

The script: https://gist.github.com/BrockA/2625891#file-waitforkeyelements-js

Mark Doe
  • 11
  • 3
  • Yes, but _which_ element? – Xan Nov 27 '14 at 20:35
  • Also, a consider using `MutationObserver`s for performance reasons. – Xan Nov 27 '14 at 20:36
  • In this particular case that can be any element that you encounter on the channel's page, but not on "choose the channel" page. This question is actually the simplified version of an actual problem I had. I had a problem with a chat window, so in my problem's case I had to watch the ".textarea-contain" element. And nice job there Xan, you could have told me about mutation observers before... – Mark Doe Nov 27 '14 at 21:54