|
7 | 7 | // @run-at document-body
|
8 | 8 | // @supportURL https://github.com/dylanarmstrong/userscripts/issues
|
9 | 9 | // @updateURL https://raw.githubusercontent.com/dylanarmstrong/userscripts/main/chess.js
|
10 |
| -// @version 1 |
| 10 | +// @version 2 |
11 | 11 | // ==/UserScript==
|
12 | 12 |
|
13 | 13 | /**
|
14 | 14 | * Add button to analyze games on lichess after games.
|
15 | 15 | */
|
16 |
| - |
17 |
| -(function() { |
| 16 | +(function main() { |
| 17 | + // eslint-disable-next-line prefer-const |
18 | 18 | let timer;
|
19 | 19 |
|
20 | 20 | const getLink = async () => {
|
21 | 21 | const gameId = new URL(location.href).pathname.split('/').at(-1);
|
22 |
| - const playerName = document.querySelector('#notifications-request').getAttribute('username'); |
| 22 | + const playerName = document |
| 23 | + .querySelector('#notifications-request') |
| 24 | + .getAttribute('username'); |
23 | 25 |
|
24 |
| - const gameUrl = await fetch(`https://api.chess.com/pub/player/${playerName}/games/archives`) |
25 |
| - .then(r => r.json()) |
26 |
| - .then(j => j.archives.at(-1)); |
| 26 | + const gameUrl = await fetch( |
| 27 | + `https://api.chess.com/pub/player/${playerName}/games/archives`, |
| 28 | + ) |
| 29 | + .then((r) => r.json()) |
| 30 | + .then((index) => index.archives.at(-1)); |
27 | 31 |
|
28 | 32 | const { pgn } = await fetch(gameUrl)
|
29 |
| - .then(r => r.json()) |
30 |
| - .then(j => j.games.find((game) => game.url.endsWith(gameId))); |
31 |
| - |
32 |
| - const { url } = await fetch( |
33 |
| - 'https://lichess.org/api/import', |
34 |
| - { |
35 |
| - body: `pgn=${encodeURIComponent(pgn)}`, |
36 |
| - headers: { |
37 |
| - 'accept': 'application/json', |
38 |
| - 'content-type': 'application/x-www-form-urlencoded;charset=UTF-8', |
39 |
| - }, |
40 |
| - method: 'post', |
| 33 | + .then((r) => r.json()) |
| 34 | + .then((index) => index.games.find((game) => game.url.endsWith(gameId))); |
| 35 | + |
| 36 | + const { url } = await fetch('https://lichess.org/api/import', { |
| 37 | + body: `pgn=${encodeURIComponent(pgn)}`, |
| 38 | + headers: { |
| 39 | + accept: 'application/json', |
| 40 | + 'content-type': 'application/x-www-form-urlencoded;charset=UTF-8', |
41 | 41 | },
|
42 |
| - ).then(r => r.json()); |
| 42 | + method: 'post', |
| 43 | + }).then((r) => r.json()); |
43 | 44 |
|
44 | 45 | return url;
|
45 | 46 | };
|
46 | 47 |
|
47 |
| - const addLink = async (e) => { |
48 |
| - const { currentTarget } = e; |
| 48 | + const addLink = async (event) => { |
| 49 | + const { currentTarget } = event; |
49 | 50 | currentTarget.removeEventListener('click', addLink);
|
50 | 51 |
|
51 |
| - currentTarget.textContent = 'Loading...' |
| 52 | + currentTarget.textContent = 'Loading...'; |
52 | 53 | const url = await getLink();
|
53 | 54 | currentTarget.textContent = 'Lichess Review';
|
54 | 55 | currentTarget.href = url;
|
55 | 56 | currentTarget.click();
|
56 | 57 | };
|
57 | 58 |
|
| 59 | + const getElement = () => { |
| 60 | + const element = document.querySelector('#__lichess__'); |
| 61 | + if (element) { |
| 62 | + element.remove(); |
| 63 | + } |
| 64 | + |
| 65 | + const link = document.createElement('a'); |
| 66 | + link.id = '__lichess__'; |
| 67 | + |
| 68 | + link.style['-moz-user-select'] = 'auto'; |
| 69 | + link.style['-webkit-user-select'] = 'auto'; |
| 70 | + |
| 71 | + link.style.alignItems = 'center'; |
| 72 | + link.style.display = 'flex'; |
| 73 | + link.style.height = '100%'; |
| 74 | + link.style.justifyContent = 'center'; |
| 75 | + link.style.pointerEvents = 'auto'; |
| 76 | + link.style.userSelect = 'auto'; |
| 77 | + link.style.width = '100%'; |
| 78 | + |
| 79 | + link.classList.add('game-over-review-button-label'); |
| 80 | + link.textContent = 'Lichess Review'; |
| 81 | + link.rel = 'noopener noreferrer'; |
| 82 | + link.target = '_blank'; |
| 83 | + |
| 84 | + return link; |
| 85 | + }; |
| 86 | + |
58 | 87 | const loop = async () => {
|
59 |
| - const gameOver = document.querySelector('.game-over-review-button-component'); |
| 88 | + const gameOver = document.querySelector( |
| 89 | + '.game-over-review-button-component', |
| 90 | + ); |
60 | 91 | if (gameOver) {
|
61 |
| - window.clearInterval(timer); |
| 92 | + globalThis.clearInterval(timer); |
62 | 93 |
|
63 | 94 | const element = gameOver.cloneNode(true);
|
64 | 95 |
|
|
68 | 99 | oldButton.classList.add('cc-button-secondary');
|
69 | 100 | }
|
70 | 101 |
|
71 |
| - const link = document.createElement('a'); |
72 |
| - link.style['-moz-user-select'] = 'auto'; |
73 |
| - link.style['-webkit-user-select'] = 'auto'; |
74 |
| - |
75 |
| - link.style.alignItems = 'center'; |
76 |
| - link.style.display = 'flex'; |
77 |
| - link.style.height = '100%'; |
78 |
| - link.style.justifyContent = 'center'; |
79 |
| - link.style.pointerEvents = 'auto'; |
80 |
| - link.style.userSelect = 'auto'; |
81 |
| - link.style.width = '100%'; |
82 |
| - |
83 |
| - link.classList.add('game-over-review-button-label'); |
84 |
| - link.textContent = 'Lichess Review'; |
85 |
| - link.rel = 'noopener noreferrer'; |
86 |
| - link.target = '_blank'; |
| 102 | + const link = getElement(); |
87 | 103 |
|
88 | 104 | link.addEventListener('click', addLink);
|
89 | 105 |
|
|
92 | 108 | element.querySelector('.game-over-review-button-label'),
|
93 | 109 | );
|
94 | 110 |
|
95 |
| - gameOver.insertAdjacentElement('beforebegin', element); |
| 111 | + gameOver.before(element); |
96 | 112 | }
|
97 | 113 | };
|
98 | 114 |
|
99 |
| - timer = window.setInterval(loop, 500); |
| 115 | + timer = globalThis.setInterval(loop, 500); |
100 | 116 | })();
|
0 commit comments