Greasemonkey 脚本停止工作并出现错误:unsafeWindow.document.watch 不是函数

发布于 2025-01-06 08:33:05 字数 438 浏览 0 评论 0 原文

我对这一切都很陌生,但这个脚本曾经在 Firefox 上运行,但最近停止了。它将 Gmail 收件箱中的未读邮件计数放在窗口/选项卡标题的开头。

unsafeWindow.document.watch('title',
function(prop, oldval, newval) {
 if (matches = newval.match(/Inbox \((\d+)\)/)) {
    names = newval.match(/\w+/)
    newval = '(' + matches[1] + ') unread - ' + names[0] + ' Inbox';
 }
 return (newval);
});

运行时,错误控制台显示“unsafeWindow.document.watch 不是函数”。我尝试在谷歌和这里搜索,但无法弄清楚这一点。任何帮助将不胜感激!

I'm very new to all of this, but this script used to work on firefox and recently stopped. It puts the unread count on Gmail's inbox to the beginning of the window/tab title.

unsafeWindow.document.watch('title',
function(prop, oldval, newval) {
 if (matches = newval.match(/Inbox \((\d+)\)/)) {
    names = newval.match(/\w+/)
    newval = '(' + matches[1] + ') unread - ' + names[0] + ' Inbox';
 }
 return (newval);
});

When this runs, the error console shows "unsafeWindow.document.watch is not a function". I tried searching on Google and here, but could not figure this out. Any help would be greatly appreciated!

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

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

发布评论

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

评论(1

殊姿 2025-01-13 08:33:05

看起来 Greasemonkey 的沙箱(XPCNativeWrapper)已经改变了。这似乎是一个可能的错误,但目前我没有看到任何未解决的问题。

此外,watch() 是非标准的(可能会消失),并且根据 文档不是意味着要使用除了临时调试

同时,您可以通过将代码注入页面范围来使该代码再次工作,如下所示:

function AddTitleWatch () {
    document.watch ('title', function (prop, oldval, newval) {
        var matches, names;
        if (matches = newval.match (/Inbox \((\d+)\)/ ) ) {
            names   = newval.match (/\w+/)
            newval  = '(' + matches[1] + ') unread - ' + names[0] + ' Inbox';
        }
        return (newval);
    } );
}

function addJS_Node (text, s_URL, funcToRun) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    scriptNode.type                         = "text/javascript";
    if (text)       scriptNode.textContent  = text;
    if (s_URL)      scriptNode.src          = s_URL;
    if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';

    var targ    = D.getElementsByTagName('head')[0] || D.body || D.documentElement;
    targ.appendChild (scriptNode);
}

addJS_Node (null, null, AddTitleWatch);

但更智能、更长期、更健壮、可移植的解决方案是重构代码以使用间隔计时器。 ...

setInterval (RefactorTitle, 200);

function RefactorTitle () {
    var oldTitle    = RefactorTitle.oldTitle  ||  "";
    var docTitle    = document.title;

    if (docTitle != oldTitle ) {
        var matches, names;
        if (matches     = docTitle.match (/Inbox \((\d+)\)/ ) ) {
            names       = docTitle.match (/\w+/);
            docTitle    = '(' + matches[1] + ') unread - ' + names[0] + ' Inbox';
        }
        document.title          = docTitle;
        RefactorTitle.oldTitle  = docTitle;
    }
}

It looks like Greasemonkey's sandbox (XPCNativeWrapper) has changed. This seems like a possible bug, but I don't see any open issues at the moment.

Also, watch() is non-standard (may go away) and according to the documentation is not meant to be used except for temporary debugging.

In the meantime, you can get that code working again by injecting it into the page scope, like so:

function AddTitleWatch () {
    document.watch ('title', function (prop, oldval, newval) {
        var matches, names;
        if (matches = newval.match (/Inbox \((\d+)\)/ ) ) {
            names   = newval.match (/\w+/)
            newval  = '(' + matches[1] + ') unread - ' + names[0] + ' Inbox';
        }
        return (newval);
    } );
}

function addJS_Node (text, s_URL, funcToRun) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    scriptNode.type                         = "text/javascript";
    if (text)       scriptNode.textContent  = text;
    if (s_URL)      scriptNode.src          = s_URL;
    if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';

    var targ    = D.getElementsByTagName('head')[0] || D.body || D.documentElement;
    targ.appendChild (scriptNode);
}

addJS_Node (null, null, AddTitleWatch);

But the smarter, long-term, more robust, portable solution is to refactor the code to use an interval timer. ...

setInterval (RefactorTitle, 200);

function RefactorTitle () {
    var oldTitle    = RefactorTitle.oldTitle  ||  "";
    var docTitle    = document.title;

    if (docTitle != oldTitle ) {
        var matches, names;
        if (matches     = docTitle.match (/Inbox \((\d+)\)/ ) ) {
            names       = docTitle.match (/\w+/);
            docTitle    = '(' + matches[1] + ') unread - ' + names[0] + ' Inbox';
        }
        document.title          = docTitle;
        RefactorTitle.oldTitle  = docTitle;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文