-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetacritic-score-for-gog.user.js
627 lines (544 loc) · 16.9 KB
/
metacritic-score-for-gog.user.js
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
Metacritic score for GOG - Adds metacritic score to GOG game's page.
Copyright (C) 2019 T1mL3arn
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// ==UserScript==
// @name Metacritic score for GOG
// @description Adds metacritic score to GOG game's page
// @version 1.2.2
// @author T1mL3arn
// @namespace https://github.com/T1mL3arn
// @icon https://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Metacritic.svg/88px-Metacritic.svg.png
// @match https://gog.com/game/*
// @match https://gog.com/*/game/*
// @match https://www.gog.com/game/*
// @match https://www.gog.com/*/game/*
// @require https://code.jquery.com/jquery-3.3.1.min.js
// @grant GM_xmlhttpRequest
// @grant GM.xmlhttpRequest
// @grant GM_xmlHttpRequest
// @grant GM.xmlHttpRequest
// @grant GM_addStyle
// @grant GM.addStyle
// @license GPLv3
// @homepageURL https://github.com/t1ml3arn-userscript-js/Metacritic-score-for-GOG
// @supportURL https://github.com/t1ml3arn-userscript-js/Metacritic-score-for-GOG/issues
// @run-at document-start
// ==/UserScript==
(function () {
// =============================================================
//
// Greasemonkey polyfill
//
// =============================================================
// probably it is Greasemonkey
if (typeof GM !== 'undefined') {
if (typeof GM.info !== 'undefined')
window.GM_info = GM.info;
// VM has GM_xmlhttpRequest but GM has GM_xmlHttpRequest
// Mad mad world !
if (typeof GM.xmlHttpRequest !== 'undefined') {
window.GM_xmlhttpRequest = GM.xmlHttpRequest
}
// addStyle
window.GM_addStyle = function(css) {
return new Promise((resolve, reject) => {
try {
let style = document.head.appendChild(document.createElement('style'))
style.type = 'text/css'
style.textContent = css;
resolve(style)
} catch(e) {
console.error(`It is not possible to add style with GM_addStyle()`)
reject(e)
}
})
}
}
//console.log(`[${GM_info.scriptHandler}][${GM_info.script.name} v${GM_info.script.version}] inited`)
// =============================================================
//
// API section
//
// =============================================================
const css = (() => {
return `
.mcg-wrap {
/* Base size for all icons */
--size: 80px;
display: flex;
flex-flow: row;
flex-wrap: wrap;
align-items: center;
justify-content: center;
width: auto;
padding: 4px;
box-sizing: border-box;
}
.mcg-wrap * {
all: unset;
box-sizing: border-box;
}
.mcg-score-summary {
display: flex;
flex-flow: row nowrap;
justify-content: flex-start;
align-items: center;
margin: 0 2px 0 2px;
}
.mcg-score-summary__score {
display: flex;
flex-flow: row;
justify-content: center;
align-items: center;
min-width: calc(var(--size) * 0.5);
min-height: calc(var(--size) * 0.5);
width: calc(var(--size) * 0.5);
height: calc(var(--size) * 0.5);
margin: 0 4px 0 4px;
background-color: #0f0;
background-color: #c0c0c0;
border-radius: 6px;
font-family: sans-serif;
font-size: 1.2em;
font-weight: bold;
color: white;
}
.mcg-score--bad {
background-color: #f00;
color: white;
}
.mcg-score--mixed {
background-color: #fc3;
color: #111;
}
.mcg-score--good {
background-color: #6c3;
color: white;
}
.mcg-score-summary__score--circle {
border-radius: 50%;
}
.mcg-score-summary .mcg-score-summary__label {
align-self: flex-start;
align-self: center;
max-width: 50px;
font-size: 0.9em;
font-size: 14px;
font-weight: bold;
text-align: left;
text-align: center;
}
.mcg-logo {
display: flex;
flex-flow: row;
align-items: center;
}
.mcg-logo__img {
background-image: url(https://upload.wikimedia.org/wikipedia/commons/thumb/2/20/Metacritic.svg/200px-Metacritic.svg.png);
background-position: center center;
background-size: cover;
min-width: calc(var(--size) * 0.5);
min-height: calc(var(--size) * 0.5);
width: calc(var(--size) * 0.5);
height: calc(var(--size) * 0.5);
}
.mcg-logo p {
display: flex;
flex-flow: column;
align-items: center;
justify-content: center;
margin: 4px 6px;
font-family: sans-serif;
font-size: 22px;
text-align: center;
font-weight: bold;
}
.mcg-logo p > a {
cursor: pointer;
text-decoration: underline;
font-size: 0.65em;
font-size: 14px;
font-weight: normal;
color: #36c;
}`
})()
const defaultHeaders = {
"Origin": null,
"Referer": null,
"Cache-Control": "max-age=3600",
}
/**
* Sends xmlHttpRequest via GM api (this allows crossdomain reqeusts).
* NOTE Different userscript engines support different
* details object format.
*
* Violentmonkey @see https://violentmonkey.github.io/api/gm/#gm_xmlhttprequest
*
* Greasemonkey @see https://wiki.greasespot.net/GM.xmlHttpRequest
* @param {Object} details @see ...
* @returns {Promise}
*/
function ajax(details) {
return new Promise((resolve, reject) => {
details.onload = resolve
details.onerror = reject
GM_xmlhttpRequest(details)
})
}
/**
* Returns an URL to make a search request
* for given game.
* @param {String} game Game name
* @param {String} platform Target platform (PC is default)
*/
function getSearhURL(game, platform) {
// searches GAME only in "game" for PC platform (plats[3]=1)
///TODO sanitize game name (trim, remove extra spacebars etc) ?
return `https://www.metacritic.com/search/game/${game}/results?search_type=advanced&plats[3]=1`
}
/**
* Returns an array of search results from given html code
* @param {String} html Raw html from which search results will be parsed
* @returns {Array} array of objects
*/
function parseSearchResults(html) {
const doc = new DOMParser().parseFromString(html, 'text/html')
const yearReg = /\d{4}/
const results = $(doc).find('ul.search_results .result_wrap')
return results.map((ind, elt) => {
const result = $(elt)
let year = yearReg.exec(result.find('.main_stats p').text())
year = year == null ? 0 : parseInt(year[0])
return {
title: result.find('.product_title').text().trim(),
pageurl: 'https://www.metacritic.com' + result.find('.product_title > a').attr('href'),
platform: result.find('.platform').text().trim(),
year,
metascore: parseInt(result.find('.metascore_w').text()),
criticReviewsCount: 0,
userscore: 0.0,
userReviewsCount: 0,
description: result.find('.deck').text().trim()
}
})
.get()
}
function swap(arr, ind1, ind2) {
const tmp = arr[ind1]
arr[ind1] = arr[ind2]
arr[ind2] = tmp
}
/**
* Returns integer which represents total user reviews
* @param {Object} doc jQuery document object
* @returns {Number}
*/
function getUserReviesCount(doc) {
const reg = /\d+/
let count = doc.find('.feature_userscore .count a').text()
count = reg.exec(count)
count = count == null ? 0 : parseInt(count[0])
return count;
}
/**
* Returns float which represents user score
* @param {Object} doc jQuery document object
* @returns {Number}
*/
function getUserScore(doc) {
return parseFloat(doc.find('.feature_userscore .metascore_w.user').eq(0).text())
}
function getMetascore(doc) {
return parseInt(doc.find('.metascore_summary .metascore_w span').text())
}
/**
* Returns a number of crititc reviews
* @param {Object} doc jQuery document object
* @returns {Number}
*/
function getCriticReviewsCount(doc) {
return parseInt(doc.find('.score_summary.metascore_summary a>span').text())
}
function parseDataFromGamePage(html) {
const doc = $(new DOMParser().parseFromString(html, 'text/html'))
const yearReg = /\d{4}/
let year = yearReg.exec(doc.find('.release_data .data').text())
year = year == null ? 0 : year[0]
return {
title: doc.find('.product_title h1').text(),
platform: doc.find('.platform a').text(),
year,
metascore: getMetascore(doc),
criticReviewsCount: getCriticReviewsCount(doc),
userscore: getUserScore(doc),
userReviewsCount: getUserReviesCount(doc),
}
}
/**
* Converts given object to string
* like `foo=bar&bizz=bazz`
* @param {Object} obj
*/
function objectToUrlArgs(obj) {
return Object.entries(obj)
.map(kv => `${kv[0]}=${kv[1]}`)
.join('&')
}
/**
* Query metacritic autosearch api.
* Returns Promise with an array with results objects.
* Result object properties:
* - url: link to page
* - name: game name
* - itemDate: release date (string ?)
* - imagePath: url to cover image
* - metaScore: critic score (int)
* - scoreWord: like mixed, good, bad etc
* - refType: item type, e.g "PC Game"
* - refTypeId: type id, (string)
* @param {String} query term for search
* @returns {Promise}
*/
function autoSearch(query) {
return ajax({
url: 'https://www.metacritic.com/autosearch',
method: 'post',
data: objectToUrlArgs({ image_size: 98, search_term: query }),
responseType: 'json',
// Strictly recomended to watch Network log
// and get Request Headers from it
headers: {
"Origin": null,
"Referer": "https://www.metacritic.com",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"X-Requested-With": "XMLHttpRequest"
}
})
.then(response => JSON.parse(response.responseText).autoComplete)
}
/**
* Queries metacritic search page with given query.
* Returns Promise with `response` object.
* Html code of the page can be read from `response.responseText`
* @param {String} query term for search
* @returns {Promise}
*/
function fullSearch(query) {
return ajax({
url: getSearhURL(query),
method: "GET",
headers: defaultHeaders,
context: { query }
})
}
/**
* Returns a resolved Promise with game data object on success
* and rejected Promise on failure.
* Game data object has these fields:
* - title: Game title
* - pageurl: Game page url
* - platform: Game platform (pc, ps3 etc)
* - year: Release year (int)
* - metascore: Critic score (int)
* - criticReviewsCount: The number of critic reviews
* - userscore: User score (float)
* - userReviewsCount: The number of user reviews
* - description: Description of the game
* - queryString: Original query string
* @param {String} gameName Game name
*/
function getMetacriticGameDetails(gameName) {
return fullSearch(gameName)
.then(response => {
const { context, responseText } = response
const results = parseSearchResults(responseText)
if (results.length == 0) {
throw `Can't find game "${context.query}" on www.metacritic.com`
}
// I have to find the game in results and this is not so easy,
// metacritic gives stupid order, e.g
// most relevant game for "mass effect" is ME: Andromeda,
// not the first Mass Effect game from 2007.
// lets assume that GOG has correct game titles
// (which is not always true)
// then we can get game from results with the same
// title as in search query
const ind = results.findIndex(result => result.title.toLowerCase()===context.query.toLowerCase())
if (ind != -1)
return results[ind]
else {
console.error('Metacritic results:', results)
throw `There are results, but can't find game "${context.query}" on www.metacritic.com`
}
} /* Network error */
)
.then(gameData => {
// request to the game page to get
// user score and reviews count
return ajax({
url: gameData.pageurl,
method: 'GET',
headers: defaultHeaders,
context: { gameData },
})
} /* catch error, if there is no such game */
).then(response => {
const { context, responseText } = response
const { gameData } = context
const doc = $(new DOMParser().parseFromString(responseText, 'text/html'))
gameData.userReviewsCount = getUserReviesCount(doc)
gameData.userscore = getUserScore(doc)
gameData.criticReviewsCount = getCriticReviewsCount(doc)
return { ...gameData, queryString: gameName }
} /* catches error when fetching game page */
);
}
/**
* Get gog product details via REST api
* @see https://gogapidocs.readthedocs.io/en/latest/galaxy.html#get--products-(int-product_id)
* @param {String} productId
* @param {String} locale
* @returns {Promise} fullfiled with json object
*/
function getGOGProductDetails(productId, locale) {
return ajax({
url: `https://api.gog.com/products/${productId}?locale=${locale}`,
method: 'get',
defaultHeaders: { 'Cache-Control': 'max-age=3600' },
responseType: 'json',
}).then(response => JSON.parse(response.responseText))
}
function MetacriticLogo(props) {
let { reviewsUrl } = props
return `
<div class="mcg-logo">
<div class="mcg-logo__img" title="metacritic logo"></div>
<p>
metacritic
<a href=${ reviewsUrl || "#" } target="_blank" rel="noopener noreferer">
Read reviews
<img src="data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%2212%22 height=%2212%22%3E %3Cpath fill=%22%23fff%22 stroke=%22%2336c%22 d=%22M1.5 4.518h5.982V10.5H1.5z%22/%3E %3Cpath fill=%22%2336c%22 d=%22M5.765 1H11v5.39L9.427 7.937l-1.31-1.31L5.393 9.35l-2.69-2.688 2.81-2.808L4.2 2.544z%22/%3E %3Cpath fill=%22%23fff%22 d=%22M9.995 2.004l.022 4.885L8.2 5.07 5.32 7.95 4.09 6.723l2.882-2.88-1.85-1.852z%22/%3E %3C/svg%3E" />
</a>
</p>
</div>
`
}
function getScoreColor(score) {
// tbd - gray
// 0-49 - red
// 50-74 - yellow
// 75 - 100 - green
if (score === 'tbd' || score !== score)
// default bg color is already present in css
return ''
else {
if (score < 50) return 'mcg-score--bad'
else if (score < 75) return 'mcg-score--mixed'
else return 'mcg-score--good'
}
}
/**
* Converts user score value to its string representation.
* @param {Number} score user score
* @returns {String} a string in format like "7.0" or "8.8",
* or "tbd" if a given score is NaN
*/
function formatUserScore(score) {
return score !== score ? 'tbd' : score.toFixed(1)
}
/**
* Converts critic score to its string representation.
* @param {Number} score critic score
* @returns {String} a string like "98" or "100",
* or "tbd" if a given score is NaN
*/
function formatMetaScore(score) {
return score !== score ? 'tbd' : score
}
function ScoreSummary(props) {
const { score, scoreLabel, scoreTypeClass, scoreColorClass } = props
const scoreEltClass = `"mcg-score-summary__score ${scoreTypeClass} ${scoreColorClass}"`
return `
<div class="mcg-score-summary">
<span class=${ scoreEltClass }>${ score }</span>
<span class="mcg-score-summary__label">${ scoreLabel }</span>
</div>
`
}
function MetacriticScore(props) {
const { metascore, userscore, pageurl } = props;
return `
<div class='mcg-wrap'>
${ ScoreSummary({
score: formatUserScore(userscore),
scoreLabel: 'User score',
scoreTypeClass: 'mcg-score-summary__score--circle',
scoreColorClass: getScoreColor(userscore * 10),
})
}
${ ScoreSummary({
score: formatMetaScore(metascore),
scoreLabel: 'Meta score',
scoreTypeClass: '',
scoreColorClass: getScoreColor(metascore),
})
}
${ MetacriticLogo({ reviewsUrl: pageurl }) }
</div>
`
}
function showMetacriticScoreElt(gameData) {
const metascore = MetacriticScore(gameData)
$('div[content-summary-section-id="productDetails"] > .details')
.append('<hr class="details__separator"/>')
.append(metascore)
.append('<hr class="details__separator"/>')
}
// =============================================================
//
// Code section
//
// =============================================================
const documentReady = new Promise((resolve, rej) => $(document).ready(resolve))
documentReady.then(() => GM_addStyle(css))
// get game name from page's url
let gameNameFromUrl = window.location.pathname
.replace('/game/', '')
.replace(/_/g, '-')
// first trying to get the same game page from metacritic
ajax({
url: `https://www.metacritic.com/game/pc/${gameNameFromUrl}`,
method: "GET",
headers: defaultHeaders,
}).then(response => {
const { responseText, finalUrl, status } = response
if (status === 200) {
const gameData = {
...parseDataFromGamePage(responseText),
pageurl: finalUrl
}
documentReady.then(() => showMetacriticScoreElt(gameData))
}
else if (status === 404) {
documentReady.then(() => {
const productId = $(document).find('div[card-product]').attr('card-product')
// get product details from gog api
getGOGProductDetails(productId, 'en')
.then(details => details.title)
.then(getMetacriticGameDetails)
.then(showMetacriticScoreElt)
})
}
}, e => console.error('Error', e))
})();