last day (15 days later) » 

4:05 PM
0
A: Content script doesn't get injected on every page load

XanThe URL is probably changed through history manipulation or URL fragment change, but does not actually reload the page. If it's a URL fragment change, you can use window.onhashchange event. If the URL changes completely but page does not reload, it probably is achieved with history.pushState or...

 
Is there a good way to tell if it's a fragment change? It wouldn't surprise me if it is since everything is the same except for the last fragment which is the title. However, the Chrome spinner was spinning so I assumed it was loading a new page. I'll take a look at your solutions and see if I can get anything work. Thanks.
 
Xan
See if window.onhashchange is triggered when it happens, e.g. set window.onhashchange = function(e){console.dir(e);}
 
judgeja linked to this possible solution (stackoverflow.com/questions/20865581/…) which seems to get me closer. Every time I click one of the related videos, I get an alert that the content script has run, but it still is pulling from the old, original DOM. Interesting.
 
Xan
It is possible that page's JavaScript updates the history state before updating the DOM. Then you are in a race condition. Try delaying re-injection by some time.
 
Currently my .onHistoryStateUpdated() method looks like this for injecting the content script: jsfiddle.net/uu4tbnrw I'm not a Javascript developer by any means, and after some googling I'm not sure what would be the best way to delay the injection. What is the best practice for doing that?
 
Xan
4:05 PM
1) You probably don't need to reinject jQuery, 2) setTimeout(function(){/*...*/},500) will delay by half a second (asynchronously). This is not a "best practice" solution, only means of debugging what's going on.
 
Changed to: jsfiddle.net/uu4tbnrw/1 The delay is definitely there but for some weird reason it's still grabbing the old URL. No idea how or why. Hmm.
 
Xan
I'm not sure I can help further.
 
No problem, thanks for all the help. If it helps, I set a break point on the content script and when the page is initially loaded, it hits the break point.
 
Xan
If you manually execute $('meta[itemprop=contentURL]').attr('content'); after the transition, does it give the correct URL?
 
When I click another video from the suggetsions, it delays, and hits the content script but the break point is not hit
It must somehow be using and 'old' version of the content script?
 
Xan
4:07 PM
I assume that the break point is not hit, because a freshly injected copy is a "different" script
And your breakpoint does not apply.
 
Good point. When I manually execute the above jQuery, it still goes back to the old link. Even when the page has stopped loading and starts playing the new video
 
Xan
Yeah, you need to find another way to extract the URL.
 
Right clicking and viewing the source directly tells me that there should be a new URL there. This is crazy.
 
Xan
By the way, fix your code snippet in the question (url vs videoUrl in content script)
For now, I'm afraid I'm going offline. Good luck resolving the jQuery mystery. Maybe it's worth a separate (non-chrome-extension-specific) question.
 
I'm not sure what you mean. In the original question in the getUrl() function?
No problem man, thanks for helping me get a bit farther.
 
Xan
4:11 PM
Yes, that's what I meant
videoUrl is undefined in your snippet
 
ah, good catch. Thanks. Take it easy
 

last day (15 days later) »