GOG.com - Updated Thread Count in Title

Display a count of updated threads in the GOG forum list page title and icon and reload once an hour so a pinned tab can be used as an update notifier

Ajankohdalta 30.11.2015. Katso uusin versio.

// ==UserScript==
// @name         GOG.com - Updated Thread Count in Title
// @namespace    ssokolow.com
// @version      6
// @description  Display a count of updated threads in the GOG forum list page title and icon and reload once an hour so a pinned 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); }, 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($('title').text() + ' (' + unviewed_count + ')');

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