1

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.

Sundar R
  • 13,776
  • 6
  • 49
  • 76

1 Answers1

2

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.

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Hi, thanks for the reply, as mentioned in the Update I guessed on the reason 1 here and changed the script according to that, it started working then (http://userscripts.org/scripts/show/174390). The conflict mentioned in 2 doesn't seem to occur, perhaps because I'm requiring the latest version of jQuery, same as TED (not sure what version, if any, YouTube uses). – Sundar R Jul 28 '13 at 06:21
  • No. The conflict definitely occurs. You are requesting a different version of jQ (1.10.2) from TED (1.7.1), your copy of jQ is getting superseded by the page's, and it's **pure dumb luck** that your script didn't break anything noticeable (yet) on either site. – Brock Adams Jul 28 '13 at 07:33
  • I've uploaded a new version with a @grant request to enable the sandbox, but I'd like to know: since this jQuery is getting superseded by theirs as you mention, how could the script break something on their site? I understand how the script itself may have problems when their version of jQuery doesn't have something that the script expects, but how would the site's own javascript get affected? – Sundar R Jul 28 '13 at 09:19
  • Yes, in this case, because you use `document-start` your script is more likely to break than the page. But, you still have a race condition and the possibility of the script's jQuery loading on top of, or after, page scripts. In this case it would cause intermittent faults (if any), the very worst to troubleshoot. And what for? The fix costs nothing and it's a good habit to be in for when you try to reuse the code in different circumstances. – Brock Adams Jul 28 '13 at 09:54