Greasy Fork

Shined up real nice.

Mark owned ScummVM Games

A simple aid for collecting ScummVM-supported games

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// ==UserScript==
// @name        Mark owned ScummVM Games
// @namespace   ssokolow.com
// @description A simple aid for collecting ScummVM-supported games
// @license MIT
// @version     5
// @require     https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
//
// @match       *://scummvm.org/compatibility
// @match       *://scummvm.org/compatibility/*
// @match       *://www.scummvm.org/compatibility
// @match       *://www.scummvm.org/compatibility/*
//
// @compatible  firefox Developed and dogfooded under Greasemonkey.
// @compatible  chrome  Tested periodically under Tampermonkey.
//
// @grant       GM_setValue
// @grant       GM_getValue
// @grant       GM_deleteValue
// @grant       GM_listValues
// ==/UserScript==

const OWNED_OPACITY = 0.3;
var owned_games = GM_listValues();
GM_deleteValue('__SHOW_OWNED');
var hide_owned = GM_getValue('__HIDE_OWNED', false);

/// Code shared between initial setup and the click handler
var mark_owned = function(node, state) {
    var row = $(node).closest('tr');
    if (state) {
        row.css('opacity', OWNED_OPACITY).addClass('owned');
    } else {
        row.css('opacity', 1.0).removeClass('owned');
    }
};

/// click() handler for the per-game toggle button
var toggleOwnership = function(e) {
    //e.preventDefault();

    var $this = $(this);
    var game_id = $this.data('game_id');

    // Toggle based on what's displayed so that it will always act as the
    // user expects, regardless of changes since last reload
    if ($this.text() == '+') {
        $this.text('-');
        mark_owned($this, true);
        GM_setValue(game_id, true);
    } else {
        $this.text('+');
        mark_owned($this, false);
        GM_deleteValue(game_id);
    }
};

/// click() handler for the whole-table hide/show button
var toggleVisible = function(e) {
    $('tr.owned')[hide_owned ? 'show' : 'hide']();
    $(this).text(hide_owned ? '-' : '+');
    GM_setValue('__HIDE_OWNED', hide_owned = !hide_owned);
};

/// Shared code to generate a toggle button
var makeButton = function(initial_state, label) {
    return $('<div>', {title: label})
        .text(initial_state ? '+' : '-')
        .css({
            background: '#c0c0c0',
            borderRadius: 3,
            color: 'black',
            display: 'inline-block',
            marginRight: 5,
            paddingLeft: 2,
            paddingRight: 2,
            textAlign: 'center',
            width: '1em',
            cursor: 'pointer',
        })
};

// Per-entry code
$('.content a').each(function() {
    var $this = $(this);

    // Extract the game ID for use in record-keeping
    var url = $(this).attr('href').split('/');
    var game_id = url[url.length-1] ? url[url.length-1] : url[url.length-2];

    // Craft a button to toggle ownership status
    var $td = $this.parents('td');
    var togglebutton = makeButton(owned_games.indexOf(game_id) == -1,
                                  "Toggle Owned")
        .css('visibility', 'hidden')
        .data('game_id', game_id)
        .click(toggleOwnership)
        .prependTo($td);

    // On-hover display in a manner which won't reposition the game title
    $td.hover(function() { togglebutton.css('visibility', 'visible'); },
              function() { togglebutton.css('visibility', 'hidden'); });

    // TODO: Profile alternatives like an x|y|z regexp or a popping iteration
    if (owned_games.indexOf(game_id) !== -1){ mark_owned($this, true); }
});

// Global toggle-button code
var $g_button = makeButton(hide_owned, "Show/Hide Owned Games")
    .click(toggleVisible)
    .attr('id', 'gm_visible_toggle')
    .css({
        'position': 'relative',
        'margin-right': '-100%',
        'top': 14,
        'left': 0,
    }).prependTo('#content .content');

$g_button.data('pos_orig', $g_button.css('position'))
         .data('top_orig', $g_button.css('top'))
         .data('left_orig', $g_button.css('left'));

// Source: http://www.sitepoint.com/css-position-sticky-introduction-polyfills/
// TODO: Fix this for Firefox's new asynchronous panning
//  https://developer.mozilla.org/en-US/docs/Mozilla/Performance/Scroll-linked_effects
var btnOffset = $g_button.offset();
window.addEventListener('scroll', function() {
    if (window.pageYOffset >= btnOffset.top) {
        $g_button
            .css({
                'position': 'fixed',
                'top': 5,
                'left': btnOffset.left,
            });
    } else {
        $g_button.css({
            'position': $g_button.data('pos_orig'),
            'top': $g_button.data('top_orig'),
            'left': $g_button.data('left_orig'),
        });
    }
});

if (hide_owned) { $('tr.owned').hide(); }

var add_print_styles = function() {
    var print_stylesheet = "@media print { body, #container { " +
            " min-width: 0 !important; background: none !important; }\n" +
        " #header, #menu, #footer, #legal, " +
        " .tentacle, .intro, .rbtop, .rbbot, .colorKeyTable, " +
        " #gm_visible_toggle { " +
            " display: none !important; }\n" +
        " #container { width: inherit !important; }\n" +
        " #content, .rbcontent, .rbwrapper, .box { " +
            "margin: 0 !important; padding: 0 !important; " +
            "border: none !important }\n" +
        "}";

    $('<style media="print"></style>').html(print_stylesheet).appendTo('head');
};

// Inject a better print stylesheet to make "thrifting TODO" printouts easier
add_print_styles();