动态加载的 jQuery 与 GreaseMonkey 在页面上不一致(刷新似乎可以修复它)...不知道为什么

发布于 2024-08-28 17:35:46 字数 2624 浏览 3 评论 0原文

我希望在我访问的每个站点上都有一个自定义页面分析页脚...因此我使用了一种方法将 JQuery 附加到 unsafeWindow。

然后我在页面上创建一个浮动页脚。我希望能够调用菜单中的命令,进行一些处理,然后将结果放在页脚中。不幸的是,它有时有效,有时无效。

printOutput 函数中至少应发生两个警报。有时它只触发一个,然后它(崩溃?)没有错误?在其他页面上,两个警报都会触发并找到该元素,但不会添加额外的文本。 (例如 www.linode.com)

刷新页面,然后再次运行 printOutput 命令似乎总是有效。

有谁知道怎么回事吗???

用户脚本可以安装在: http://www.captionwizard.com/test/page_analysis.user.js

// ==UserScript==
// @name           page_analysis
// @namespace      markspace
// @description    Page Analysis
// @include        http://*/*
// ==/UserScript==
(function() 
{
    // Add jQuery
    var GM_JQ = document.createElement('script');
    GM_JQ.src = 'http://code.jquery.com/jquery-1.4.2.min.js';
    GM_JQ.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(GM_JQ);

    var jqueryActive = false;

    //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() 
    {
        jqueryActive = true;
        setupOutputFooter();
    }

    /******************************* Analysis FOOTER Functions ******************************/ 
    function printOutput(someText)
    {
        alert('printing output');

        if($('div.analysis_footer').length)
        {
           alert('is here - appending');
           $('div.analysis_footer').append('<br>' + someText);         
        }
        else
        {
            alert('not here - trying again');
            setupOutputFooter();
            $('div.analysis_footer').append('<br>' + someText);            
        }
    }


    GM_registerMenuCommand("Test Output", testOutput, "k", "control", "k" );


    function testOutput()
    {
        printOutput('testing this');
    }           

    function setupOutputFooter()
    {    
        $('<div class="analysis_footer">Page Analysis Footer:</div>').appendTo('body');
        $('div.analysis_footer').css('position','fixed').css('bottom', '0px').css('background-color','#F8F8F8');
        $('div.analysis_footer').css('width','100%').css('color','#3B3B3B').css('font-size', '0.8em');
        $('div.analysis_footer').css('font-family', '"Myriad",Verdana,Arial,Helvetica,sans-serif').css('padding', '5px');
        $('div.analysis_footer').css('border-top', '1px solid black').css('text-align', 'left');
    }      


}());   

I want a custom page analysis footer on every site I visit... so I've used a method to attach JQuery to unsafeWindow.

I then create a floating footer on the page. I want to be able to call commands in a menu, do some processing, then put the results in the footer. Unfortunately it sometimes works, sometimes it doesn't.

At least two alerts should happen in the printOutput function. Sometimes it only fires one, then it (crashes?) without error? On other pages, both alerts fire and it finds the element, but it doesn't add the extra text. (e.g. www.linode.com)

Refreshing the page, then running the printOutput command again seems to always work.

Does anyone know what's going on???

The userscript can be installed at:
http://www.captionwizard.com/test/page_analysis.user.js

// ==UserScript==
// @name           page_analysis
// @namespace      markspace
// @description    Page Analysis
// @include        http://*/*
// ==/UserScript==
(function() 
{
    // Add jQuery
    var GM_JQ = document.createElement('script');
    GM_JQ.src = 'http://code.jquery.com/jquery-1.4.2.min.js';
    GM_JQ.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(GM_JQ);

    var jqueryActive = false;

    //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() 
    {
        jqueryActive = true;
        setupOutputFooter();
    }

    /******************************* Analysis FOOTER Functions ******************************/ 
    function printOutput(someText)
    {
        alert('printing output');

        if($('div.analysis_footer').length)
        {
           alert('is here - appending');
           $('div.analysis_footer').append('<br>' + someText);         
        }
        else
        {
            alert('not here - trying again');
            setupOutputFooter();
            $('div.analysis_footer').append('<br>' + someText);            
        }
    }


    GM_registerMenuCommand("Test Output", testOutput, "k", "control", "k" );


    function testOutput()
    {
        printOutput('testing this');
    }           

    function setupOutputFooter()
    {    
        $('<div class="analysis_footer">Page Analysis Footer:</div>').appendTo('body');
        $('div.analysis_footer').css('position','fixed').css('bottom', '0px').css('background-color','#F8F8F8');
        $('div.analysis_footer').css('width','100%').css('color','#3B3B3B').css('font-size', '0.8em');
        $('div.analysis_footer').css('font-family', '"Myriad",Verdana,Arial,Helvetica,sans-serif').css('padding', '5px');
        $('div.analysis_footer').css('border-top', '1px solid black').css('text-align', 'left');
    }      


}());   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

邮友 2024-09-04 17:35:46

可能只是 code.jquery.com 有时无法(或至少非常慢)加载 jQuery 脚本。你所做的看起来不错。您还可以使用 @require 将 jQuery 与用户脚本一起打包(因此不需要为每个页面加载请求 jquery):

// ==UserScript==
// @name          jQuery Example
// @require       http://code.jquery.com/jquery-1.4.1.min.js
// ==/UserScript==

(function() {
    // This will be executed onLoad
    // Append some text to the element with id #someText using the jQuery library.
    $("#someText").append(" more text.");
}());

阅读本文以获取更多信息

It could just be that code.jquery.com sometimes fails (or at least is very slow) to load the jQuery script. What you've done looks fine. You can also package jQuery with the userscript, with @require (thus jquery doesn't need to be requested for every page load):

// ==UserScript==
// @name          jQuery Example
// @require       http://code.jquery.com/jquery-1.4.1.min.js
// ==/UserScript==

(function() {
    // This will be executed onLoad
    // Append some text to the element with id #someText using the jQuery library.
    $("#someText").append(" more text.");
}());

read this for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文