GOG.com - Updated Thread Count in Title

Display a count of updated threads (and notifications) in the GOG favicon (and page title) and reload hourly so a browser tab can be used as an update notifier

От 30.11.2015. Виж последната версия.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey, Greasemonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Userscripts.

За да инсталирате скрипта, трябва да инсталирате разширение като Tampermonkey.

За да инсталирате този скрипт, трябва да имате инсталиран скриптов мениджър.

(Вече имам скриптов мениджър, искам да го инсталирам!)

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

(Вече имам инсталиран мениджър на стиловете, искам да го инсталирам!)

// ==UserScript==
// @name         GOG.com - Updated Thread Count in Title
// @namespace    ssokolow.com
// @version      8
// @description  Display a count of updated threads (and notifications) in the GOG favicon (and page title) and reload hourly so a browser tab can be used as an update notifier
// @contributionURL http://tinyurl.com/kfgayrh
//
// @match        *://www.gog.com/forum
//
// @require      https://cdnjs.cloudflare.com/ajax/libs/tinycon/0.6.3/tinycon.min.js
// @require      https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
//
// @compatible   firefox  Developed and dogfooded on Firefox
// @incompatible chrome   This script itself works but Tinycon.js fails to set the favicon for unknown reasons
//
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_registerMenuCommand
// ==/UserScript==

// Set up hourly reload before anything that unexpected markup could break
setTimeout(function() { window.location.reload(true); },
           GM_getValue('check_interval', 3600 * 1000));

// Initialize with values indicating a soft error
var bubble_bg = '#FF8000';
var unviewed_count = '!';
var notification_counts = null;
var needs_persist = false;
var old_counts = JSON.parse(GM_getValue('notification_counts', '{}'));

/// Save a set of counts as 'stuck' so they'll get ignored next time
var mark_stuck_values = function(counts) {
    GM_setValue('notification_counts', JSON.stringify(counts));
}

/// Mark a value in old_counts to be cleared
var reset_stuck_value = function(idx) {
    old_counts[idx] = 0;
    needs_persist = true;
}

/// Retrieve GOG notification counts with "mark as read" support
var get_notification_counts = function() {
    // Parse the counts and omit the total to avoid double-counting
    var counts = [], total = 0;
    $(".top-nav__item-count").slice(1).each(function() {
        counts.push(Number(this.textContent));
    });

    // Implement "Mark as Read" to work around GOG's delayed dismissal
    for (var i = 0, len = counts.length; i < len; i++) {
        if (old_counts && old_counts[i] && counts[i] >= old_counts[i]) {
            // If the number went up, keep suppressing stuff marked as stuck
            total += counts[i] - old_counts[i];
        } else {
            total += counts[i];
            reset_stuck_value(i);
        }
    }

    // Only call GM_setValue once for performance reasons
    if (needs_persist) {
        mark_stuck_values(old_counts);
        needs_persist = false;
    }

    // Make accessible to the "mark as read" callback
    notification_counts = counts;
    return total;
};

/// Retrieve unviewed count for favourite forum topics
var get_unviewed_count = function() {
    var category = $(".topics .text:contains('My favourite topics')");
    if (category.length) { // If not some kind of "server overloaded" page...
        category = category.parents('h2').next('.category');

        // Use an empty list of favourite topics to detect being logged out
        if (category.find('.item:not(.message)').length) {
            bubble_bg = '#9CC824';
            unviewed_count = category.find('.item:not(.visited) .name a').length;
        } else {
            bubble_bg = '#ff0000';
            unviewed_count = 'X';
        }
    }
}

$(document).ready(function() {
    get_unviewed_count();
    if (unviewed_count !== 'X') {
        unviewed_count += get_notification_counts();
    }

    Tinycon.setOptions({
        width: 7,
        height: 9,
        font: '10px arial',
        colour: '#ffffff',
        background: bubble_bg,
        fallback: false
    });
    Tinycon.setBubble(unviewed_count);
    $('title').text('[' + unviewed_count + '] ' + $('title').text());

    GM_registerMenuCommand("Ignore Stuck Notification Count", function() {
        mark_stuck_values(notification_counts);
    }, 'I');
});