Delgan
Delgan

Reputation: 19627

Script doesn't work without TamperMonkey

I'm working on a user script, and I noticed that it worked when it was set up with TamperMonkey, but not when it was manually added to the Chrome extensions.

I wish I knew what it was due? What TamperMonkey has more to run my script?

// ==UserScript==
// @name        Rainbow DDB
// @namespace   Rainbow DDB
// @description Change la couleur du "!" lorsqu'une DDB est en cours.
// @include     http://www.jeuxvideo.com/forums/3-*
// @include     http://www.jeuxvideo.com/forums/1-*
// ==/UserScript==

$ = unsafeWindow.$;
var dates = document.querySelectorAll(".date");
i=0;

function ddb(j) {
url = dates[j].getElementsByTagName("a")[0].href;
   $.get(
   url, 
   function(data) {
   if (data.indexOf("Signalement déjà fait") >= 0) {
   dates[j].querySelector("a img").src = "http://image.noelshack.com/fichiers/2013/17/1367080939-14agd2.png";
   } else if (data.indexOf("Vous êtes à l'origine") >= 0) {
   dates[j].querySelector("a img").src = "http://image.noelshack.com/fichiers/2013/17/1367081255-14aig2.png";
   } else if (data.indexOf("effectué un boost") >= 0) {
   dates[j].querySelector("a img").src = "http://image.noelshack.com/fichiers/2013/17/1367073914-149xe2.png";
   } else if (data.indexOf("Autosignalement déjà effectué") >= 0) {
   dates[j].querySelector("a img").src = "http://image.noelshack.com/fichiers/2013/17/1367082905-14atu2.png";
   }
   });
}


while (i<dates.length) {
   ddb(i);
   i++;
  }

As you can see it is a very simple script. Is $.get a problem ? I tried without the $ = unsafeWindow.$; but it does not work neither.

Upvotes: 0

Views: 1368

Answers (1)

Brock Adams
Brock Adams

Reputation: 93483

Tampermonkey supports a proper unsafeWindow, while Chrome userscripts have a stripped-down, mostly useless, object by that name. See "Why is window (and unsafeWindow) not the same from a userscript as from a tag?".

I recommend just supporting Tampermonkey and not bothering with straight Chrome userscripts. This offers several advantages:

  1. Almost complete support for the powerful Greasemonkey API.

  2. This means that almost all Greasemonkey scripts will work, as-is, in Tampermonkey and there are a lot of pre-built scripts out there.

  3. It's vastly easier to install, create, edit, update and maintain Tampermonkey scripts. Especially, now that Chrome has its "Install from our $tore, or else!" policy for userscripts and full-fledged extensions.

  4. Automatic and easy synching between machines if you use Chrome's synching features.



If you insist on supporting naked Chrome, you will have to inject the code. Like so:

// ==UserScript==
// @name        Rainbow DDB
// @namespace   Rainbow DDB
// @description Change la couleur du "!" lorsqu'une DDB est en cours.
// @include     http://www.jeuxvideo.com/forums/3-*
// @include     http://www.jeuxvideo.com/forums/1-*
// ==/UserScript==

function GM_scriptMain ($) {
    var dates   = document.querySelectorAll (".date");
    var urlBase = "http://image.noelshack.com/fichiers/2013/17/";

    function ddb (j) {
        var url = dates[j].getElementsByTagName("a")[0].href;
        $.get (
            url, function (data) {
            if (data.indexOf("Signalement déjà fait") >= 0) {
                dates[j].querySelector("a img").src = urlBase + "1367080939-14agd2.png";
            } else if (data.indexOf("Vous êtes à l'origine") >= 0) {
                dates[j].querySelector("a img").src = urlBase + "1367081255-14aig2.png";
            } else if (data.indexOf("effectué un boost") >= 0) {
                dates[j].querySelector("a img").src = urlBase + "1367073914-149xe2.png";
            } else if (data.indexOf("Autosignalement déjà effectué") >= 0) {
                dates[j].querySelector("a img").src = urlBase + "1367082905-14atu2.png";
            }
        } );
    }

    for (var k = 0, L = dates.length;  k < L;  ++k) {
        ddb (k);
    }
}

withPages_jQuery (GM_scriptMain);

function withPages_jQuery (NAMED_FunctionToRun) {
    //--- Use named functions for clarity and debugging...
    var funcText        = NAMED_FunctionToRun.toString ();
    var funcName        = funcText.replace (/^function\s+(\w+)\s*\((.|\n|\r)+$/, "$1");
    var script          = document.createElement ("script");
    script.textContent  = funcText + "\n\n";
    script.textContent += 'jQuery(document).ready(function() {'+funcName+'(jQuery);});';
    document.body.appendChild (script);
};

Upvotes: 2

Related Questions