Sundar R
Sundar R

Reputation: 14695

jQuery .ready not getting called from within Greasemonkey script

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.

Upvotes: 1

Views: 355

Answers (1)

Brock Adams
Brock Adams

Reputation: 93473

The alert doesn't fire for two reasons:

  1. The browser is redirected from www.ted.com to www.youtube.com right away -- long before the ready event can fire on ted.com. The script is not set to fire on youtube.com at all.

    If you want the script to fire on both domains, adjust the @include (or @match) directives and then use checks like:

    if (location.hostname == "www.ted.com") {
        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;
    }
    

    to alter the script's behavior, per domain.

  2. Because @grant none is used, the GM script's jQuery conflicts with code on both domains. You will see error messages on Firefox's Error Console (CtrlShiftJ). The solution for that is to restore the sandbox by changing the @grant to @grant GM_addStyle.

Upvotes: 2

Related Questions