2017-01-08 33 views
0

this userscript的目的是,當您訪問/review頁堆棧交易所網站的,它增加了接近頂端桌面通知鏈接,如果您訪問頁面,如果有主持人標誌或帖子要查看,它會生成桌面通知。腳本定期重新加載。Userscript工作在Chrome/Tampermoney,但不能在Firefox/Greasemonkey的

這可以在任何我在多個OS-es(OSX,Linux,Windows)中嘗試的Chrome/Tampermonkey中使用。在Firefox/Greasemonkey中,它在OSX中不起作用(沒有出現桌面通知)。如果我沒有記錯,它在Linux中工作。當我訪問該頁面時,Web開發者控制檯不顯示任何內容。 userscript當然是安裝並啓用的。

我可能會錯過什麼?如何調試呢?

// ==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); 
     } 
    } 
} 

回答

1

GreaseMonkey's source,該GM_notification API尚未實現。

問題#1194是相關問題。 (我不認爲它會很快加)

作爲一個臨時的填充工具,你可以在該文件的開頭:

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

這使得標籤看起來像:

Tab title font is bold and favicon has flash

但是不會產生通知。您現在還應該有document.title中的信息,因爲不會有任何實際的通知(如方括號中的通知數量)。

沒有太多的事情可以做到這一點,當用戶做其他事情時不僅會帶來不便,如window.open竊取焦點(但大多數嘗試可能會被阻止)到新打開的選項卡。

對此的長期解決方案是將其作爲擴展,因爲Firefox和Chrome擴展都支持通知。

+0

如果一個未定義的'GM_notification'是問題,腳本只會在該行被執行時崩潰,否?目前該腳本根本不工作。例如,它不添加**桌面通知**鏈接。 – janos

+0

看來我有一個更基本的問題......如果我用一行'document.title ='hello''替換整個腳本(在標題下面),即使這樣也行不通。所以我想我以某種方式錯誤地安裝了腳本。在我連接的GitHub頁面上,點擊Greasemonkey提供的'[Raw]'按鈕來安裝它。這就是我安裝它的方式。當我訪問一個評論頁面時,Greasemonkey菜單顯示它是活動的腳本。我可能會錯過什麼? – janos

+0

@janos進入Firefox的'about:addons',你會看到左側欄上的用戶腳本標籤。從那裏檢查腳本是否已安裝,然後檢查是否已啓用,然後轉到選項 - >編輯此腳本。從那裏,檢查它是否安裝了正確的腳本。 – Artyer

相關問題