How to match YouTube url in chrome extension?
I am trying to make a simplified chrome extension in youtube (content script) but it doesn't work correctly.
Oddly enough, it works for a while and some just don't.
My end goal is to make it only work in video urls, but I am stuck before that.
I originally tried:
"matches": [ "http://www.youtube.com/*" ]
Without any success, which I found strange, so my guess is my first question, since in another extension I did, it worked fine for http://twitter.com/
I read the code for some youtube extensions and one was using:
"matches": [ "*://*.youtube.com/*" ]
The weird thing is that this only works sometimes and I can't reproduce / understand when. It definitely doesn't work when I reload the page, but in some urls it just works.
The actual script is a bare bones script for debugging:
$(window).load(function() {
alert(2);
});
So I am rather confused by this inconsistent behavior.
Edit:
This url seems to work:
It:
Edit 2:
The whole 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"]
}
source to share
You need to send a message or a link to your entire manifest.json
, but the most likely problem is $(window).load( ...
.
By default, Chrome content scripts run at unpredictable times, which may be before or after they window.onload
fire!
So add "run_at": "document_end"
to your manifest to ensure your script runs at predictable times and before window.onload
.
~~~
Further, it is possible that other extensions are blocking this warning . Check and disable other extensions if needed.
~~~
Finally, it could be that YouTube is loading some pages via AJAX - although I can't find a case where it works now. AJAX loaded pages will not restore your script content. If you find a case where AJAX loads a "new" page, (1) comment out the recipe here, (2) you can use AJAX-enabled methods to restore the code as needed . (Note that this waitForKeyElements.js
will be added via a manifest, not a directive @require
.)
source to share