4

I made a script for Greasemonkey on Firefox, it works perfectly, but nothing happens on Chrome with Tampermonkey.

I know that Chrome restricts the use of jQuery.

I especially found this interesting post : How can I use jQuery in Greasemonkey scripts in Google Chrome?

I tried the solutions but I still can not get my script to run on Google Chrome. I do not really see what's wrong with my script, because it is really short. What could be the problem?

This is the script I'm trying to run (I shortened it but I am obliged to leave much since I do not know where is the problem):

// ==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-*
// @version     1
// ==/UserScript==

dates = document.getElementsByClassName("date");
i=0;

function ddb(j) {
    url = dates[j].getElementsByTagName("a")[0].href;
    $.get(url, function(data) {
        if (data.contains("Signalement déjà fait")) {
            document.getElementsByClassName("date")[j].getElementsByTagName("a")[0].getElementsByTagName("img")[0].src = "http://image.noelshack.com/fichiers/2013/17/1367080939-14agd2.png";
        }
    });
}

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

The only thing that can be a problem is $.get, is not it?

I tried different solutions, ask loading jQuery before executing my script, I tried with the proposed template but it definitely did not work, and I do not see why.

Community
  • 1
  • 1
Delgan
  • 18,571
  • 11
  • 90
  • 141

1 Answers1

6

If you want to use jQuery version that is embedded on this website, you need to refer to it with unsafeWindow. In other words: you need to define $ as unsafeWindow.$ at the beginning of your userscript.

Here's fixed code:

// ==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-*
// @version     1
// ==/UserScript==


$ = unsafeWindow.$;
dates = document.getElementsByClassName("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) {
            document.getElementsByClassName("date")[j].getElementsByTagName("a")[0].getElementsByTagName("img")[0].src = "http://image.noelshack.com/fichiers/2013/17/1367080939-14agd2.png";
        }
    });
}


while (i<dates.length) {
    ddb(i);
    i++;
}
ghost
  • 769
  • 5
  • 11
  • Thank you for Thee interested in my problem. Unfortunately, I had already tried this solution and it did not work. I tried again, the alert () is displayed correctly but the rest, no. – Delgan Apr 27 '13 at 21:44
  • But `$.get` function works fine and this was your problem about. So what exactly do you mean? What is not displayed correctly? Also please give me example url where I can fully test this script. – ghost Apr 27 '13 at 21:47
  • Sorry, it is a french wbesite: http://www.jeuxvideo.com/forums/1-51-42148021-1-0-1-0-qui-utilise-firefox-ici.htm You have to be connected, so, try with these ID: nickname = "Vlalprolo" and password="kourkis87" If the script work fine, you should see the icone "!" at the right of the first post blue, not red. – Delgan Apr 27 '13 at 21:50
  • 2
    I couldn't login with these data, but I managed to find what was causing a problem. It is about [`contains()`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/contains) method you used. As you can see on MDN page, it is only supported in Firefox. You can replace it with `indexOf()` method. I edited my answer, you can try new code. – ghost Apr 27 '13 at 22:06
  • Yes, I'm stupid, I forgot that this nickname was banned ... That said, change contains for IndexOf is working very well, thank you! (and if you want to be sure, use the identifier "Uneamiegentille" with the password kourkis87 again) All this research about the incompatibility between jQuery and Chrome ... I was really not looking in the right direction. – Delgan Apr 27 '13 at 22:18
  • Sometimes the simplest things are the hardest to find. I'm glad I could help you :) – ghost Apr 27 '13 at 22:24
  • @Pacerier, I don't know how is that today but the point was to give you the full control over the the website scripts' functions, variables etc. I don't remember if this could be done just by `window` instead of `unsafeWindow` but probably not. Detailed description is available on wiki http://wiki.greasespot.net/UnsafeWindow – ghost Apr 19 '15 at 17:18