【问题标题】:Userscript works in Chrome/Tampermoney but not in Firefox/GreasemonkeyUserscript 在 Chrome/Tampermoney 中有效,但在 Firefox/Greasemonkey 中无效
【发布时间】:2017-01-09 04:54:21
【问题描述】:

this userscript 的用途是,当您访问 Stack Exchange 网站的 /review 页面时,它会在顶部附近添加一个 Desktop Notifications 链接,如果您访问该页面,它如果有版主标志或帖子要查看,则生成桌面通知。脚本会定期重新加载。

这在我在多个操作系统(OSX、Linux、Windows)中尝试过的任何 Chrome/Tampermonkey 中都可以开箱即用。在 Firefox/Greasemonkey 中,它在 OSX 中不起作用(桌面通知 不会出现)。如果我没记错的话,它确实在 Linux 中工作。当我访问该页面时,Web 开发者控制台 没有显示任何内容。用户脚本当然已安装并启用。

我会错过什么?如何调试?

// ==UserScript==
// @name Moderator Flag Notification
// @author Simon Forsberg
// @description Shows a desktop notification when there are flags or review items to be handled.
// @namespace https://github.com/Zomis/SE-Scripts
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_notification
// @match *://*.stackexchange.com/review*
// @match *://*.stackoverflow.com/review*
// @match *://*.superuser.com/review*
// @match *://*.serverfault.com/review*
// @match *://*.askubuntu.com/review*
// @match *://*.stackapps.com/review*
// @match *://*.mathoverflow.net/review*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// ==/UserScript==

if (window.location.href.indexOf('/desktop-notifications') === -1) {
    $('.tools-rev h1').append('<span class="lsep">|</span><a href="/review/desktop-notifications">Desktop Notifications</a></h1>');
} else {
    var KEY_NEXT = 'NextReload';
    var DELAY = 60 * 1000;
    var currentTime = Date.now ? Date.now() : new Date().getTime();
    var lastTime = GM_getValue(KEY_NEXT, 0);
    var nextTime = currentTime + DELAY;
    GM_setValue(KEY_NEXT, nextTime);

    var timeDiff = Math.abs(lastTime - currentTime);
    setTimeout(function(){ window.location.reload(); }, DELAY);

    $('.subheader h1').html('Desktop Notifications');
    $('.leftcol').html('Keep your browser open on this page and it will be automatically reloaded and alert you when something wants your attention.').removeClass('leftcol');
    $('.rightcol').remove();

    var title = document.title.split(' - '); // keep the site name
    document.title = 'Desktop Notifications - ' + title[1];

    // a way to detect that the script is being executed because of an automatic script reload, not by the user.
    if (timeDiff <= DELAY * 2) {
        var notifications = [];

        var topbarFlag = $('.topbar-links .mod-only .icon-flag .flag-count').html();
        var topbarFlagCount = parseInt(topbarFlag);
        if (topbarFlagCount > 0) {
            notifications.push(topbarFlagCount + ' Flags');
        }

        var reviewItems = $('.icon-tools-flag span');
        var reviewCount = 0;
        if (reviewItems.length > 0) {
            reviewCount = parseInt(reviewItems.html());
            if (reviewCount > 0) {
                notifications.push(reviewCount + ' Review Items');
            }
        }

        if (notifications.length > 0) {
            var details = {
                title: document.title,
                text: notifications.join('\n'),
                timeout: 0
            };
            GM_notification(details, null);
        }
    }
}

【问题讨论】:

    标签: javascript macos firefox


    【解决方案1】:

    根据GreaseMonkey's sourceGM_notification API 尚未实现。

    问题#1194 是相关问题。 (我认为它不会很快添加)

    作为一个临时的 polyfill,你可以在文件的开头有:

    if (!GM_notification) {
        GM_notification = function(details) {
            alert(details.title + '\n' + details.text);
        }
    }
    

    这使得标签看起来像:

    但不会产生通知。您现在应该在document.title 中有信息,因为不会有任何实际通知(例如方括号中的通知数量)。

    当用户在做其他事情时,您无能为力,这不仅会给用户带来不便,例如 window.open 将焦点(但大多数尝试可能会被阻止)转移到新打开的标签。

    对此的长期解决方案是将其制成扩展程序,因为 Firefox 和 Chrome 扩展程序都支持通知。

    【讨论】:

    • 如果未定义的GM_notification 是问题所在,那么只有在执行该行时脚本才会崩溃,不是吗?目前脚本根本不起作用。例如,它不会添加 Desktop Notifications 链接。
    • 看来我有一个更根本的问题......如果我用一行document.title = 'hello'; 替换整个脚本(在标题下方),即使这样也行不通。所以我想我以某种方式错误地安装了脚本。在我链接的 GitHub 页面上,当我单击 [Raw] 按钮时,Greasemonkey 提供安装它。我就是这样安装的。当我访问评论页面时,Greasemonkey 菜单指示它处于活动状态的脚本。我可能会错过什么?
    • @janos 在 Firefox 中转到 about:addons,您应该会在左侧栏看到一个用户脚本选项卡。从那里,检查脚本是否已安装,然后检查它是否已启用,然后转到选项-> 编辑此用户脚本。从那里,检查它是否安装了正确的脚本。
    • 用户脚本选项卡在那里。该脚本已列出并启用。我猜你的意思不是选项,而是首选项。这就是我在之前的评论中提到的编辑脚本的方式。编辑后,我尝试禁用并重新启用,以防万一,但它没有。我尝试了简单的命令,例如我提到的document.title = '...'console.log('hello')。但是这些都没有执行,所以脚本似乎根本没有运行。
    猜你喜欢
    • 2016-01-02
    • 2016-02-19
    • 2022-01-23
    • 1970-01-01
    • 2020-04-04
    • 2011-03-16
    • 2015-06-11
    • 2018-07-05
    • 2014-08-18
    相关资源
    最近更新 更多