3

I am trying to make the simples chrome extension work in youtube (content script), but it doesn't seem to work correctly.

Oddly enough it works some of the times, and some it simple doesn't.

My final goal is to make it work only in the video URLs but I got stuck before that.

I originally tried:

"matches": [ "http://www.youtube.com/*" ]

Without any success at all, which I found odd, so I guess that's my first question, since in another extension I made, it worked perfectly for http://twitter.com/

I read the code to some youtube extensions, and one used:

"matches": [ "*://*.youtube.com/*" ]

The weird thing is that this only works sometimes, and I can't reproduce/understand when. It definitely not works when I reload the page, but in some URLs it just works.

The actual script is bare bones script for debugging:

$(window).load(function() {
    alert(2);
});

So I'm pretty confused about this inconsistent behaviour.

Edit:

This url's don's seem to work:

This do:

Edit 2:

Entire manifest:

{
  "name": "__MSG_extname__",
  "version": "0.1",
  "default_locale": "en",
  "manifest_version": 2,
  "icons": {
    "16": "images/icon16.png",
    "48": "images/icon48.png",
    "128": "images/icon128.png" },
  "content_scripts":[
    {
    "matches": [ "*://*.youtube.com/*" ],
    "js": ["jquery.min.js", "script.js"]
    }
  ],
  "web_accessible_resources": ["images/down-arrow.png"]
}
Trufa
  • 39,971
  • 43
  • 126
  • 190

1 Answers1

1

You need to post or link to your entire manifest.json, but the most likely problem is $(window).load( ....
By default, Chrome content scripts fire at unpredictable times which could be either before or after window.onload fires!

So, add "run_at": "document_end" to your manifest to guarantee that your script will run at a predictable time, and before window.onload.

~~~
Next, it is possible that other extensions are blocking that alert. Check and disable your other extensions as/if needed.

~~~
Finally, it may be that YouTube loads some pages via AJAX -- although I can't find a case where it does at the moment. AJAX-loaded pages will not refire your content script. If you find a case where AJAX loads a "new" page, (1) comment here with the recipe, (2) You can use AJAX-aware techniques to refire code as necessary. (Note that waitForKeyElements.js would be added via the manifest rather than a @require directive.)

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295