Javascript Greasemonkey使用jQuery,如何编写Greasemonkey脚本来修改页面DOM元素?

Javascript Greasemonkey使用jQuery,如何编写Greasemonkey脚本来修改页面DOM元素?,javascript,jquery,dom,greasemonkey,Javascript,Jquery,Dom,Greasemonkey,在greasemonkey wiki页面中,有一个使用jQuery和greasemonkey的示例。下面列出了代码,页面的位置为 此示例脚本直接修改DOM。我是否需要用jQuery函数围绕代码,如下所示: // ==UserScript== // @name jQuery Example // @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js // ==/UserScri

在greasemonkey wiki页面中,有一个使用jQuery和greasemonkey的示例。下面列出了代码,页面的位置为

此示例脚本直接修改DOM。我是否需要用
jQuery函数
围绕代码,如下所示:

// ==UserScript==
// @name          jQuery Example
// @require       http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==

// Append some text to the element with id someText using the jQuery library.
jQuery(function(){
    $("#someText").append(" more text.");   
})
我问这个问题是因为我的greasemonkey代码不是每次刷新页面时都执行的。对这个问题有什么想法吗?
谢谢。

不,在
jQuery()
调用中包装脚本代码,或者使用
$(document.ready()
都没有意义

Greasemonkey在
DOMContentLoaded
事件(与jQuery包装器或ready事件相同)和GM版本的jQuery加载后自动激发

还要注意,wiki页面已经过时。GM现在可以很好地使用jQuery1.6.2

您没有显示相关代码,也没有链接到目标页面,但“Greasemonkey代码不是每次刷新页面时都执行”的最可能原因是:

  • GM脚本中存在错误

  • 目标内容通过AJAX单独加载。
    您可以使用此模式中的代码来解决以下问题:

    //--- This handles both page-load delays, and AJAX changes.
    setInterval (function() { checkForTweetbox (); }, 500);
    
    function checkForTweetbox () {
        var tweetbox = document.querySelector ('div.tweet-box textarea');
        if (tweetbox) {
            if (! tweetbox.weHaveProcessed) {
                tweetbox.weHaveProcessed    = true;
                alert ('New tweet-box found!');
            }
        }
    }
    

  • 不,在
    jQuery()
    调用中包装脚本代码和使用
    $(document.ready()
    都没有意义

    Greasemonkey在
    DOMContentLoaded
    事件(与jQuery包装器或ready事件相同)和GM版本的jQuery加载后自动激发

    还要注意,wiki页面已经过时。GM现在可以很好地使用jQuery1.6.2

    您没有显示相关代码,也没有链接到目标页面,但“Greasemonkey代码不是每次刷新页面时都执行”的最可能原因是:

  • GM脚本中存在错误

  • 目标内容通过AJAX单独加载。
    您可以使用此模式中的代码来解决以下问题:

    //--- This handles both page-load delays, and AJAX changes.
    setInterval (function() { checkForTweetbox (); }, 500);
    
    function checkForTweetbox () {
        var tweetbox = document.querySelector ('div.tweet-box textarea');
        if (tweetbox) {
            if (! tweetbox.weHaveProcessed) {
                tweetbox.weHaveProcessed    = true;
                alert ('New tweet-box found!');
            }
        }
    }
    

  • 有时要从Greasemonkey脚本访问DOM,您可能需要使用unsafeWindow.jQueryWhen为“有时”?有时要从Greasemonkey脚本访问DOM,您可能需要使用unsafeWindow.jQueryWhen为“有时”?\2很有帮助。这通常是知道AJAX调用何时提供了元素的最佳方法吗?或者有没有一种方法可以设置已插入的回拨(vs轮询)?我仍然认为自己是一个新手,使用JavaScript和jQuery重新排列页面上的一堆HTML,其中一些在页面加载后加载。@ MealPoLink是一种非常健壮和易于使用的技术。尤其是通过
    waitForKeyElements()
    之类的实用程序执行此操作时。为了获得更好的性能,可以使用
    MutationObs‌​服务器
    s.#2很有帮助。这通常是知道AJAX调用何时提供了元素的最佳方法吗?或者有没有一种方法可以设置已插入的回拨(vs轮询)?我仍然认为自己是一个新手,使用JavaScript和jQuery重新排列页面上的一堆HTML,其中一些在页面加载后加载。@ MealPoLink是一种非常健壮和易于使用的技术。尤其是通过
    waitForKeyElements()
    之类的实用程序执行此操作时。为了获得更好的性能,可以使用
    MutationObs‌​服务器
    s。