-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathItchioShowCategories.user.js
More file actions
289 lines (258 loc) · 9.77 KB
/
ItchioShowCategories.user.js
File metadata and controls
289 lines (258 loc) · 9.77 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// ==UserScript==
// @name Itchio Show Categories
// @version 0.0.3
// @author Dillon Regimbal
// @namespace https://dillonr.com
// @description Displays tag categories of games on itch.io
// @match *://itch.io/*
// @match *://*.itch.io/*
// @run-at document-idle
// @grant GM_addStyle
// @grant GM_xmlhttpRequest
// @grant GM_getValue
// @grant GM_setValue
// @icon https://itch.io/favicon.ico
// @noframes
// ==/UserScript==
// Since 2020-06-12
// https://greasyfork.org/en/users/420789-dillon-regimbal
// https://greasyfork.org/en/scripts/405228-itchio-show-categories
// https://github.com/dregimbal/UserScripts/blob/master/ItchioShowCategories.user.js
(function () {
'use strict'
if (window !== window.parent) {
// https://developer.mozilla.org/en-US/docs/Web/API/Window/parent
// Don't run inside of a frame
}
// #region Config and Variables
let btn_category_class = 'dr_category_button'
let btn_category_id = 'dr_markCategory'
let btn_category_text = 'Checked 0/0'
let game_store_link_selector = '.game_cell_data a.game_link, .bundle_game_grid_widget .game_cell a.title'
let category_text_class = 'dr_category_text'
let meta_tag_class = 'meta_tag'
let game_cell_class = 'game_cell'
let game_cell_data_class = 'game_cell_data'
let categoryMap = [
{
container: '#wrapper',
categories: [
{
title: 'Co-op',
searchStrings: ['co-op', ' coop ']
}
]
},
{
container: '.game_info_panel_widget',
categories: [
{
title: 'Local Multiplayer',
searchStrings: ['Local Multiplayer']
},
{
title: 'Networked',
searchStrings: ['Networked Multiplayer']
},
{
title: 'Controller',
searchStrings: ['Gamepad', 'Xbox Controller', 'Joystick', 'Playstation Controller', 'Joy-Con', 'Wiimote']
},
{
title: 'Phone Control',
searchStrings: ['Smartphone']
},
{
title: 'Physical',
searchStrings: ['Physical Game', 'Tabletop']
}
]
}
]
let gamesToCheck = new Map()
// #endregion
// #region Create button and styles
let divButton = document.createElement('div')
divButton.classList.add(btn_category_class)
divButton.id = btn_category_id
let eleA = document.createElement('a')
eleA.setAttribute('onclick', 'return false;')
eleA.textContent = btn_category_text
divButton.appendChild(eleA)
GM_addStyle(`
.${btn_category_class} {
border-radius: 2px;
border: medium none;
padding: 10px;
display: inline-block;
cursor: pointer;
background: #67C1F5 none repeat scroll 0% 0%;
width: 120px;
text-align: center;
}
.${btn_category_class} a {
text-decoration: none !important;
color: #FFF !important;
padding: 0px 2px;
}
.${btn_category_class}:hover a {
color: #0079BF !important;
}
.${btn_category_class}, .${btn_category_class} a {
font-family: Verdana;
font-size: 12px;
line-height: 16px;
}
.${game_cell_class} .${game_cell_data_class} a.${category_text_class}.${meta_tag_class}, .${game_cell_class} a.${category_text_class}.${meta_tag_class} {
padding: 3px;
margin: 2px;
font-size: 14px;
color: #ffffff;
background-color: #17199d;
}
#${btn_category_id} {
position: fixed;
right: 20px;
bottom: 65px;
z-index: 33;
}
.scrolling_outer {
height: auto !important;
}
`)
// #endregion
function queueCheckingGames() {
let storePageLinkElements = document.querySelectorAll(game_store_link_selector)
for (let storelink of storePageLinkElements) {
// Don't search bundle pages for game details
if (!storelink.href.includes('/b/')) {
if (!gamesToCheck.has(storelink.href)) {
// New link
gamesToCheck.set(storelink.href,
{
link: storelink.href,
elements: new Set([storelink.parentElement]),
checked: false,
categories: new Set()
})
} else {
// Existing link
gamesToCheck.get(storelink.href).elements.add(storelink.parentElement)
}
// Update the count on the button
eleA.innerText = getNumberOfCheckedGames()
}
}
return
}
async function checkGameLinks() {
for (let game of gamesToCheck.values()) {
await fetchGameCategories(game.link)
.then(() => {
for (let element of game.elements) {
let nextSibling = element.nextElementSibling
if (nextSibling !== null && nextSibling.classList.contains(category_text_class)) {
// console.log('Categories already added')
} else {
for (let category of game.categories) {
addCategoryText(element, category)
}
}
}
})
eleA.innerText = getNumberOfCheckedGames()
}
return
}
function getNumberOfCheckedGames() {
return `Checked ${Array.from(gamesToCheck.values()).reduce((acc, game) => {
if (game.checked) {
// eslint-disable-next-line no-param-reassign
acc++
}
return acc
}, 0)}/${gamesToCheck.size}`
}
/**
* Checks a page for game categories
* @param {string} storePageUrl The store page that contains the categories
* @returns {Promise} The categories of the game
*/
function fetchGameCategories(storePageUrl) {
return new Promise((resolve, reject) => {
let game = gamesToCheck.get(storePageUrl)
if (game.checked) {
resolve(game.categories)
} else {
GM_xmlhttpRequest({
method: 'GET',
url: game.link,
onload: function (response) {
console.assert(response.status === 200, [
response.status,
response.statusText,
response.readyState,
response.responseHeaders,
response.responseText,
response.finalUrl
].join(' - '))
let parser = new DOMParser()
let storePage = parser.parseFromString(response.responseText, 'text/html')
let validCategories = new Set()
for (let scope of categoryMap) {
let scopedElements = storePage.querySelectorAll(scope.container)
console.assert(scopedElements.length > 0, `No elements matching "${scope.container}" found on ${game.link}`)
for (let scopedElement of scopedElements) {
for (let category of scope.categories) {
for (let searchString of category.searchStrings) {
if (scopedElement.textContent.toLowerCase().includes(searchString.toLowerCase())) {
validCategories.add(category.title)
}
}
}
}
}
game.checked = true
game.categories = validCategories
resolve(validCategories)
}
})
}
})
}
/**
* @description Add text after an element
* @param {HTMLElement} element the element to add the text to
* @param {string} text the contents of the text
* @returns {undefined}
*/
function addCategoryText(element, text) {
if (typeof element !== 'undefined' && element !== null) {
let categoryText = document.createElement('a')
categoryText.classList.add(meta_tag_class)
categoryText.classList.add(category_text_class)
categoryText.innerText = text
element.parentNode.insertBefore(categoryText, element.nextSibling)
// console.log(`Adding ${text}`)
} else {
console.log(`Element null, cannot add: ${text}`)
}
return
}
let url = document.documentURI
let checking = false
if (url.includes('/my-collections') || url.includes('/my-purchases') || url.includes('/games') || url.includes('/s/') || url.includes('/c/') || url.includes('/b/')) {
divButton.addEventListener('click', async () => {
if (!checking) {
checking = true
queueCheckingGames()
await checkGameLinks()
checking = false
} else {
console.log('Wait a second, eh')
}
})
document.body.appendChild(divButton)
queueCheckingGames()
}
}())