Skip to content

Commit 1f14acc

Browse files
committed
fix(sources): speedrun.com works again!
1 parent dcee1a5 commit 1f14acc

File tree

4 files changed

+62
-33
lines changed

4 files changed

+62
-33
lines changed

background/sources/collections/speedrundotcom.js

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
11
import * as util from '../../util.js';
22

33

4-
let speedrundotcomKey = localStorage.getItem('speedrundotcomKey');
4+
let cachedKey = localStorage.getItem('speedrundotcomKey');
5+
const getSpeedrundotcomKey = async () => {
6+
if (cachedKey) {
7+
return cachedKey;
8+
} else {
9+
const settings = await util.ajax('https://www.speedrun.com/settings');
10+
const $username = settings.querySelector(
11+
'.container.navbar-bottom .dropdown.user .username');
12+
if (!$username) {
13+
throw Error('Unable to get API token from speedrun.com, not logged in?');
14+
}
15+
const username = $username.textContent;
16+
const apiPage = await util.ajax(
17+
`https://www.speedrun.com/${username}/settings/api`);
18+
const $code = apiPage.querySelector('code');
19+
if (!$code) {
20+
throw Error('Unable to get API token from speedrun.com');
21+
}
22+
cachedKey = $code.textContent;
23+
localStorage.setItem('speedrundotcomKey', cachedKey);
24+
return cachedKey;
25+
}
26+
};
27+
528
export default async () => {
629
const getMetaForRun = (url) => {
730
return util.ajax(url, {
@@ -17,6 +40,7 @@ export default async () => {
1740
users: response.data.players,
1841
};
1942
},
43+
ttl: 1000 * 60 * 30 // 30 min
2044
},
2145
});
2246
};
@@ -27,13 +51,25 @@ export default async () => {
2751
if (user.rel === 'guest') {
2852
return { name: user.name };
2953
} else {
30-
return util.ajax('http://www.speedrun.com/api/v1/users/' + user.id, {
54+
return util.ajax('https://www.speedrun.com/api/v1/users/' + user.id, {
3155
cache: {
32-
transform: (response) => ({
33-
url: response.data.weblink,
34-
name: response.data.names.international,
35-
})
36-
}
56+
transform: async (response) => {
57+
const url = response.data.weblink;
58+
const user = url.slice(url.lastIndexOf('/') + 1);
59+
let thumbnail =
60+
`https://www.speedrun.com/themes/user/${user}/image.png`;
61+
const imageRes = await fetch(thumbnail, {
62+
method: 'HEAD'
63+
});
64+
if (!imageRes.ok) { thumbnail = null; }
65+
return {
66+
url,
67+
name: response.data.names.international,
68+
thumbnail,
69+
};
70+
},
71+
ttl: 1000 * 60 * 60 * 24 // 1day
72+
},
3773
});
3874
}
3975
}));
@@ -44,11 +80,12 @@ export default async () => {
4480
const addMetaToVideo = async (run, meta) => {
4581
if (!run.game && meta.gameID) {
4682
const game = await util.ajax(
47-
'http://www.speedrun.com/api/v1/games/' + meta.gameID, {
83+
'https://www.speedrun.com/api/v1/games/' + meta.gameID, {
4884
cache: {
4985
transform: (response) => ({
5086
name: response.data.names.international
5187
}),
88+
ttl: 1000 * 60 * 60 * 24 // 1day
5289
},
5390
});
5491
run.game = game.name;
@@ -63,23 +100,15 @@ export default async () => {
63100
run.url = meta.url;
64101
run.desc = meta.desc;
65102
const results = await Promise.all([
66-
addUsersToRun.bind(null, run, meta),
67-
addMetaToVideo.bind(null, run, meta)
103+
addUsersToRun(run, meta),
104+
addMetaToVideo(run, meta)
68105
]);
69106
return results[0] && results[1];
70107
};
71108

72-
if (!speedrundotcomKey) {
73-
const body = await util.ajax('http://www.speedrun.com/settings');
74-
const $code = body.getElementsByTagName('code')[0];
75-
if (!$code) {
76-
throw Error('Unable to retrieve API token from speedrun.com');
77-
}
78-
speedrundotcomKey = $code.textContent;
79-
localStorage.setItem('speedrundotcomKey', speedrundotcomKey);
80-
}
81-
const results = await util.ajax('http://www.speedrun.com/api/v1/notifications', {
82-
headers: { 'X-API-Key': speedrundotcomKey },
109+
const key = await getSpeedrundotcomKey();
110+
const results = await util.ajax('https://www.speedrun.com/api/v1/notifications', {
111+
headers: { 'X-API-Key': key },
83112
});
84113
const runs = results.data
85114
.filter(noti => noti.item.rel === 'run')
@@ -89,7 +118,7 @@ export default async () => {
89118
url: noti.item.uri,
90119
},
91120
url: noti.links[0].uri,
92-
title: noti.text,
121+
title: null,
93122
timestamp: new Date(noti.created).getTime(),
94123
};
95124
});

background/sources/videos/twitch.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import * as util from '../../util.js';
22

33

4-
let twitchToken = null;
4+
let cachedToken = null;
55
const getTwitchToken = () => {
66
return new Promise((resolve) => {
7-
if (twitchToken) {
8-
resolve(twitchToken);
7+
if (cachedToken) {
8+
resolve(cachedToken);
99
} else {
1010
chrome.cookies.get({
1111
url: 'https://www.twitch.tv',
1212
name: 'auth-token',
1313
}, (cookie) => {
14-
twitchToken = cookie && cookie.value;
15-
resolve(twitchToken);
14+
cachedToken = cookie && cookie.value;
15+
resolve(cachedToken);
1616
});
1717
}
1818
});
@@ -42,7 +42,7 @@ const getUserImages = async (videos) => {
4242
login: user.login,
4343
profile_image_url: user.profile_image_url,
4444
})),
45-
ttl: 1000 * 60 * 60 * 24 // 24 hours
45+
ttl: 1000 * 60 * 60 * 24 // 1day
4646
}
4747
});
4848
const usersMap = new Map();
@@ -83,7 +83,7 @@ export default {
8383
},
8484
};
8585
},
86-
ttl: 1000 * 60 * 30 // 30 min
86+
ttl: 1000 * 60 * 30 // 30min
8787
},
8888
headers: { 'Authorization': `OAuth ${token}` },
8989
});

manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
"https://i.ytimg.com/vi/*/hqdefault.jpg?*",
2121
"https://i.ytimg.com/vi/*/mqdefault.jpg?*",
2222
"https://haloruns.com/records?recent",
23-
"http://www.speedrun.com/settings",
24-
"http://www.speedrun.com/api/v1/*",
23+
"https://www.speedrun.com/settings",
24+
"https://www.speedrun.com/api/v1/*",
2525
"https://t.co/*",
2626
"contextMenus",
2727
"cookies",

popup/style.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,11 @@ a:hover {
351351
}
352352

353353
.favicon.source-haloruns {
354-
background-image: url(http://haloruns.com/favicon.png);
354+
background-image: url(https://haloruns.com/favicon.png);
355355
}
356356

357357
.favicon.source-speedrundotcom {
358-
background-image: url(http://www.speedrun.com/themes/default/favicon.png);
358+
background-image: url(https://www.speedrun.com/themes/Default/favicon.png);
359359
}
360360

361361
.user img {

0 commit comments

Comments
 (0)