简体   繁体   English

使用Greasemonkey可以正常运行脚本,但不能使用Tampermonkey。 是因为jQuery $ .get吗?

[英]Script ok with Greasemonkey but does not work with Tampermonkey. Is it because of jQuery $.get?

I made a script for Greasemonkey on Firefox, it works perfectly, but nothing happens on Chrome with Tampermonkey. 我在Firefox上为Greasemonkey编写了一个脚本,它可以完美运行,但是在带有Tampermonkey的Chrome上什么也没有发生。

I know that Chrome restricts the use of jQuery. 我知道Chrome限制了jQuery的使用。

I especially found this interesting post : How can I use jQuery in Greasemonkey scripts in Google Chrome? 我特别发现了这个有趣的帖子: 如何在Google Chrome浏览器的Greasemonkey脚本中使用jQuery?

I tried the solutions but I still can not get my script to run on Google Chrome. 我尝试了解决方案,但仍无法使我的脚本在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? 唯一可能是问题的是$ .get,不是吗?

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. 我尝试了不同的解决方案,要求在执行脚本之前先加载jQuery,我尝试使用建议的模板,但它肯定无法正常工作,我不知道为什么。

If you want to use jQuery version that is embedded on this website, you need to refer to it with unsafeWindow . 如果要使用此网站上嵌入的jQuery版本,则需要使用unsafeWindow进行引用。 In other words: you need to define $ as unsafeWindow.$ at the beginning of your userscript. 换句话说:您需要在用户脚本的开头将$定义为unsafeWindow.$

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++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mark.js 在 Greasemonkey/Tampermonkey 用户脚本中工作吗? - Does mark.js work in a Greasemonkey/Tampermonkey user script? 某些按键事件在Tampermonkey上不起作用。 - Some keypress events doesn't work on Tampermonkey. 脚本可以在Greasemonkey中运行,但是在Tampermonkey中什么也没有发生? - Script works in Greasemonkey, but nothing happens in Tampermonkey? 如何使用Greasemonkey / Tampermonkey脚本更改类CSS? - How to change a class CSS with a Greasemonkey/Tampermonkey script? 为什么以下jquery代码在tampermonkey中不起作用 - why the following jquery code does not work in tampermonkey 为什么隐藏侧边栏的Greasemonkey JQuery脚本在Stack Exchange网站上有效,而在Stack Overflow上无效? - Why does my Greasemonkey JQuery script that hides the sidebar work on Stack Exchange sites but not Stack Overflow? 如何设置脚本只运行一次? Greasemonkey/Tampermonkey - How to set script only run one times? Greasemonkey/Tampermonkey 如何使用Tampermonkey / Greasemonkey脚本设置持久性全局超时? - How to set a persistent global timeout using a Tampermonkey/Greasemonkey script? 为什么此Greasemonkey脚本不能与此jQuery插件一起使用? - Why doesn't this Greasemonkey script work with this jQuery plugin? 尝试将 jquery 加载到 tampermonkey 脚本中 - Trying to load jquery into tampermonkey script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM