Skip to content

Commit 11b77dd

Browse files
chore: lint stuff, for some reason
1 parent 43168f0 commit 11b77dd

File tree

4 files changed

+1648
-43
lines changed

4 files changed

+1648
-43
lines changed

chess.js

+59-43
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,89 @@
77
// @run-at document-body
88
// @supportURL https://github.com/dylanarmstrong/userscripts/issues
99
// @updateURL https://raw.githubusercontent.com/dylanarmstrong/userscripts/main/chess.js
10-
// @version 1
10+
// @version 2
1111
// ==/UserScript==
1212

1313
/**
1414
* Add button to analyze games on lichess after games.
1515
*/
16-
17-
(function() {
16+
(function main() {
17+
// eslint-disable-next-line prefer-const
1818
let timer;
1919

2020
const getLink = async () => {
2121
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');
2325

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));
2731

2832
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',
4141
},
42-
).then(r => r.json());
42+
method: 'post',
43+
}).then((r) => r.json());
4344

4445
return url;
4546
};
4647

47-
const addLink = async (e) => {
48-
const { currentTarget } = e;
48+
const addLink = async (event) => {
49+
const { currentTarget } = event;
4950
currentTarget.removeEventListener('click', addLink);
5051

51-
currentTarget.textContent = 'Loading...'
52+
currentTarget.textContent = 'Loading...';
5253
const url = await getLink();
5354
currentTarget.textContent = 'Lichess Review';
5455
currentTarget.href = url;
5556
currentTarget.click();
5657
};
5758

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+
5887
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+
);
6091
if (gameOver) {
61-
window.clearInterval(timer);
92+
globalThis.clearInterval(timer);
6293

6394
const element = gameOver.cloneNode(true);
6495

@@ -68,22 +99,7 @@
6899
oldButton.classList.add('cc-button-secondary');
69100
}
70101

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();
87103

88104
link.addEventListener('click', addLink);
89105

@@ -92,9 +108,9 @@
92108
element.querySelector('.game-over-review-button-label'),
93109
);
94110

95-
gameOver.insertAdjacentElement('beforebegin', element);
111+
gameOver.before(element);
96112
}
97113
};
98114

99-
timer = window.setInterval(loop, 500);
115+
timer = globalThis.setInterval(loop, 500);
100116
})();

eslint.config.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import eslint from '@dylanarmstrong/eslint-config';
2+
import globals from 'globals';
3+
4+
export default [
5+
...eslint,
6+
{
7+
languageOptions: {
8+
globals: {
9+
...globals.browser,
10+
},
11+
},
12+
},
13+
];

package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"devDependencies": {
3+
"@dylanarmstrong/eslint-config": "^0.7.1",
4+
"eslint": "^9.18.0",
5+
"eslint-config-prettier": "^10.0.1",
6+
"eslint-plugin-unicorn": "^56.0.1",
7+
"globals": "^15.14.0",
8+
"prettier": "^3.4.2"
9+
},
10+
"scripts": {
11+
"lint": "prettier --write --single-quote --trailing-comma=all --ignore-unknown . && eslint --fix *.js"
12+
},
13+
"type": "module"
14+
}

0 commit comments

Comments
 (0)