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

标签 javascript firefox greasemonkey

我对这一切都很陌生,但这个脚本曾经在 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 不是函数”。我尝试在 Google 和此处搜索,但无法弄清楚。任何帮助将不胜感激!

最佳答案

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

此外,watch() 是非标准的(可能会消失)并且根据 the documentationnot meant to be used except for temporary debugging .

与此同时,您可以通过将代码注入(inject)页面范围来使该代码再次运行,如下所示:

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

关于javascript - Greasemonkey 脚本停止工作并出现错误 : unsafeWindow. document.watch 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9301760/

相关文章:

javascript - 如何消除水平滚动条?

javascript - 用于创建类似界面的思维导图的 JS 库

Firefox 无法打开 "libgtk-3.so.0"。如何规避?

css - 输入类型=范围在 Firefox 中不会拉伸(stretch)

jquery - 如何在firefox中使用JQuery访问parent.document元素?

mouseevent - 如何让 greasemonkey 在新标签页中一个接一个地打开很多链接?

javascript - 使用 Jest 测试 API 端点及其响应

javascript - 如何在滚动时将 jQuery 阴影函数应用于具有相同类名的多个元素

javascript - JavaScript 中的新页面/转发

javascript - 如何使用 GM_xmlhttpRequest 提交到基本的外部表单页面?