Skip to content

Commit 549d96b

Browse files
authored
fix(TBM): cache image conversion & fix an undefined error (#10343)
1 parent 0868765 commit 549d96b

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

websites/T/TBM/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
},
1414
"url": "www.infotbm.com",
1515
"regExp": "^https?[:][/][/]([a-z0-9-]+[.])*infotbm[.]com[/]",
16-
"version": "1.0.2",
16+
"version": "1.0.3",
1717
"logo": "https://cdn.rcd.gg/PreMiD/websites/T/TBM/assets/logo.png",
1818
"thumbnail": "https://cdn.rcd.gg/PreMiD/websites/T/TBM/assets/thumbnail.png",
1919
"color": "#FFFFFF",

websites/T/TBM/presence.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ async function getStrings() {
4242
})
4343
}
4444
const browsingTimestamp = Math.floor(Date.now() / 1000)
45+
const svgCache = new Map<string, string>()
4546

4647
enum ActivityAssets {
4748
Logo = 'https://cdn.rcd.gg/PreMiD/websites/T/TBM/assets/logo.png',
@@ -57,6 +58,10 @@ enum ActivityAssets {
5758
}
5859

5960
async function svgToPng(svgUrl: string): Promise<string | undefined> {
61+
if (svgCache.has(svgUrl)) {
62+
return svgCache.get(svgUrl)
63+
}
64+
6065
if (!svgUrl || !svgUrl.endsWith('.svg'))
6166
return
6267

@@ -92,6 +97,7 @@ async function svgToPng(svgUrl: string): Promise<string | undefined> {
9297
ctx.drawImage(img, x, y, img.width, img.height)
9398

9499
png = canvas.toDataURL('image/png')
100+
svgCache.set(svgUrl, png)
95101
}
96102

97103
URL.revokeObjectURL(url)
@@ -231,8 +237,10 @@ presence.on('UpdateData', async () => {
231237
presenceData.details = strings.viewLines
232238
presenceData.smallImageKey = ActivityAssets.Lines
233239
if (document.querySelector('.title-line')) {
240+
const departureStop = document.querySelector('.lines > h2:nth-of-type(1)')
241+
const arrivalStop = document.querySelector('.lines > h2:nth-of-type(2)')
234242
presenceData.details = strings.viewLineInfo
235-
presenceData.state = `${document.querySelector('.lines > h2:nth-of-type(1)')?.textContent} ${document.querySelector('.lines > h2:nth-of-type(2)')?.textContent}`
243+
presenceData.state = `${departureStop?.textContent}${arrivalStop ? ` ${arrivalStop?.textContent}` : ''}`
236244
svgImg = document.querySelector<HTMLImageElement>('.tbm-icon-picto > img')
237245
if (svgImg) {
238246
presenceData.smallImageKey = await svgToPng(svgImg?.src)

0 commit comments

Comments
 (0)