Greasy Fork

Shined up real nice.

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

After trying this script, you can ask a question about it, review it, or report it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// ==UserScript==
// @name         GOG.com - Updated Thread Count in Title
// @namespace    ssokolow.com
// @version      10
// @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
//
// @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 under Greasemonkey
// @compatible   chrome   Tested periodically with Tampermonkey
//
// @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 faved_replies = 0;
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;

    // Subtract faved replies here so the count for threads still works
    // properly if session bugs prevent the dropdown from displaying
    return Math.max(total - faved_replies, 0);
};

/// 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;
            faved_replies = category.find('.item:not(.visited) .reply').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);
        Tinycon.setBubble(0);
    }, 'I');
});