I'm trying to write a simple Greasemonkey script, but as a beginner in Javascript and a complete newbie to Greasemonkey, I keep encountering issues. Here's my code so far:
// ==UserScript==
// @name TEDToYoutube
// @include http://www.ted.com/talks/*.html
// @exclude http://www.ted.com/talks/*.html?*
// @version 1
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
// @run-at document-start
// @grant none
// @namespace abiteasier.in
// ==/UserScript==
var url = window.location.href;
var talk_name = url.split("/").pop();
talk_name = talk_name.substring(0, talk_name.lastIndexOf('.html'));
var youtube_search_url = 'http://www.youtube.com/user/TEDtalksDirector/search?query=' + talk_name;
window.location.href = youtube_search_url;
$(document).ready( function() {
alert("called");
var a = $("li.channels-browse-content-list-item");
alert(a.length());
} );
As you might have inferred, the script is to redirect from a TED talk page to its corresponding Youtube video. I've got the search working and the search page loads fine, but the .ready function never seems to fire. Is it a problem due to the @run-at above, does Greasemonkey apply that only to the original TED page or to every page we visit from the script? Or is the problem elsewhere in the script?
Update: Okay, I thought about this and the most reasonable logic I can think for this is that once the URL changes, GM stops executing this script. I'll try to verify this in the GM docs, please post an answer or a comment if you are knowledgeable in that area.
Update 2: I fixed the issue by including the YouTube page in @include, the code is on http://userscripts.org/scripts/show/174390 if anyone's curious.