|
| 1 | +const presence = new Presence({ |
| 2 | + clientId: '1498741548865552614', |
| 3 | +}) |
| 4 | +const browsingTimestamp = Math.floor(Date.now() / 1000) // Show elapsed time |
| 5 | + |
| 6 | +enum ActivityAssets { |
| 7 | + Logo = 'https://i.imgur.com/oRapkfu.png', |
| 8 | +} |
| 9 | + |
| 10 | +presence.on('UpdateData', async () => { |
| 11 | + const { pathname, hostname } = document.location |
| 12 | + |
| 13 | + const presenceData: PresenceData = { |
| 14 | + largeImageKey: ActivityAssets.Logo, |
| 15 | + startTimestamp: browsingTimestamp, |
| 16 | + } |
| 17 | + |
| 18 | + let title = document.title.split(' · ')[0]?.split(' | ')[0]?.trim() || '' |
| 19 | + if (title.toLowerCase().endsWith(' - solution')) { |
| 20 | + title = title.substring(0, title.length - 11).trim() |
| 21 | + } |
| 22 | + else if (title.toLowerCase().endsWith(' - editorial')) { |
| 23 | + title = title.substring(0, title.length - 12).trim() |
| 24 | + } |
| 25 | + |
| 26 | + if (hostname === 'ide.usaco.guide') { |
| 27 | + presenceData.details = 'Using IDE' |
| 28 | + presenceData.state = title === 'USACO IDE' || !title ? 'Coding' : title |
| 29 | + } |
| 30 | + else if (hostname === 'forum.usaco.guide') { |
| 31 | + presenceData.details = 'Browsing Forum' |
| 32 | + presenceData.state = title === 'USACO Forum' || !title ? 'Homepage' : title |
| 33 | + } |
| 34 | + else { |
| 35 | + const pathParts = pathname.split('/').filter(Boolean) |
| 36 | + const section = pathParts[0] |
| 37 | + |
| 38 | + const sections: Record<string, string> = { |
| 39 | + general: 'General section', |
| 40 | + bronze: 'Bronze section', |
| 41 | + silver: 'Silver section', |
| 42 | + gold: 'Gold section', |
| 43 | + plat: 'Platinum section', |
| 44 | + adv: 'Advanced section', |
| 45 | + problems: 'Problems', |
| 46 | + groups: 'Groups', |
| 47 | + editor: 'Editor', |
| 48 | + settings: 'Settings', |
| 49 | + } |
| 50 | + |
| 51 | + if (!section || pathname === '/') { |
| 52 | + presenceData.details = 'Browsing USACO Guide' |
| 53 | + presenceData.state = 'Homepage' |
| 54 | + } |
| 55 | + else if (section === 'problems' && pathParts[1]) { |
| 56 | + let problemName = pathParts[1] |
| 57 | + |
| 58 | + problemName = problemName.replace(/^(?:usaco|cf|cses|ac|spoj|kattis|poi|joi|joic|boi|ceoi|ioi|apio|coci|ys|infoarena|balkan|szkopul)(?:-[a-z0-9]+)?-/i, '') |
| 59 | + |
| 60 | + problemName = problemName.split('-').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' ') |
| 61 | + |
| 62 | + if (pathParts.includes('solution')) { |
| 63 | + presenceData.details = 'Viewing Solution' |
| 64 | + } |
| 65 | + else { |
| 66 | + presenceData.details = 'Solving Problem' |
| 67 | + } |
| 68 | + presenceData.state = title === 'USACO Guide' || !title ? problemName : title |
| 69 | + } |
| 70 | + else if (section && sections[section]) { |
| 71 | + presenceData.details = `Browsing ${sections[section]}` |
| 72 | + presenceData.state = title === 'USACO Guide' || !title ? 'Exploring guides' : title |
| 73 | + } |
| 74 | + else { |
| 75 | + presenceData.details = 'Browsing USACO Guide' |
| 76 | + presenceData.state = title === 'USACO Guide' || !title ? 'Exploring guides' : title |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + presence.setActivity(presenceData) |
| 81 | +}) |
0 commit comments