简体   繁体   English

为什么我的Greasemonkey脚本无法在Chrome中运行?

[英]Why don't my Greasemonkey scripts work in Chrome?

My Greasemonkey scripts all work in Firefox 3.6, but in Chrome 6 nothing special happens when I load a page that is supposed to trigger them. 我的Greasemonkey脚本都可以在Firefox 3.6中使用,但是在Chrome 6中,当我加载应该触发它们的页面时,没有什么特别的事情发生。 Here 's an example script (pasted below) that highlights top comments on Hacker News. 是一个示例脚本(粘贴在下面),突出显示了有关Hacker News的热门评论。 Can anyone identify what I'm doing wrong? 谁能识别我在做什么错? When I click on the user.js file and install it in Chrome, the installation succeeds. 当我单击user.js文件并将其安装在Chrome中时,安装成功。

// ==UserScript==
// @name           Hacker News highlight
// @namespace      http://news.ycombinator.com
// @description    highlights popular comments
// @include        http://news.ycombinator.com/item*

// ==/UserScript==

var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://jquery.com/src/jquery-latest.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);

// Check if jQuery's loaded
function GM_wait() {
    if(typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(GM_wait,100); }
else { $ = unsafeWindow.jQuery; letsJQuery(); }
}

GM_wait();

function letsJQuery() { 

    var maxScore = 0;
    var secBest = 0;
    var min = 0;

    var numComments = 0;
    $(".comhead > span").each(function() {
        numComments = numComments + 1;
        var score = parseInt($(this).text().split(' ')[0]);
        if(score > maxScore) {
            maxScore = score;
        }
        else if(score > secBest) {
            secBest = score;
        }
    });

    min = maxScore - secBest;

    $(".comhead > span").each(function() {
        var score = parseInt($(this).text().split(' ')[0]);

        if(min!=0 && score >= min + 1) {
            $(this).css({'background-color':'#B2D7FB', 'padding':'4px 4px 4px 4px','-moz-border-radius':'3px', '-webkit-border-radius':'3x'});      
        }
    });
}

I found the solution here . 我在这里找到了解决方案。 My scripts now work. 我的脚本现在可以工作了。

I have found it easiest to access page resources by injecting my code into the DOM : 我发现通过将代码注入DOM来访问页面资源是最容易

// ==UserScript==
// @name           Hacker News highlight
// @namespace      http://news.ycombinator.com
// @description    highlights popular comments
// @include        http://news.ycombinator.com/item*
// @run-at         document-end
// ==/UserScript==

function letsJQuery() {
   // your stuff
}

var jQuery = document.createElement("script"),
    inject = document.createElement("script");

jQuery.setAttribute("type", "text/javascript");
jQuery.setAttribute("src", "http://code.jquery.com/jquery-latest.js");

inject.setAttribute("type", "text/javascript");
inject.appendChild(document.createTextNode("(" + letsJQuery + ")()"));

document.body.appendChild(jQuery);
document.body.appendChild(inject);

The @run-at ensures that the script loads immediately after DOMDocumentReady , just as in Greasemonkey, and I changed your jQuery URL to point to their CDN. @run-at确保脚本在DOMDocumentReady之后立即加载,就像在Greasemonkey中一样,并且我将您的jQuery URL更改为指向其CDN。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM