|
| 1 | +import { Assets } from 'premid' |
| 2 | + |
| 3 | +const presence = new Presence({ |
| 4 | + clientId: '1457403769116561528', |
| 5 | +}) |
| 6 | + |
| 7 | +const browsingTimestamp = Math.floor(Date.now() / 1000) |
| 8 | + |
| 9 | +enum ActivityAssets { |
| 10 | + RTVE = 'https://i.imgur.com/0begwZJ.png', |
| 11 | + RTVE_PLAY = 'https://i.imgur.com/Ru8b2AX.png', |
| 12 | +} |
| 13 | + |
| 14 | +function getCleanTitle(): string { |
| 15 | + const ogTitle = document.querySelector('meta[property="og:title"]')?.getAttribute('content') |
| 16 | + const h1Title = document.querySelector('h1')?.textContent?.trim() |
| 17 | + |
| 18 | + let title = ogTitle || h1Title || document.title |
| 19 | + title = title.replace(/\s*\|\s*Ver.*$/, '').replace(/\s*-\s*RTVE\.es$/, '').trim() |
| 20 | + return title |
| 21 | +} |
| 22 | + |
| 23 | +function getSectionInfo(): { sectionName: string } { |
| 24 | + const path = document.location.pathname.toLowerCase() |
| 25 | + |
| 26 | + if (path.includes('/videos/directo/')) { |
| 27 | + return { sectionName: 'EN DIRECTO' } |
| 28 | + } |
| 29 | + else if (path.includes('/noticias/')) { |
| 30 | + return { sectionName: 'Noticias' } |
| 31 | + } |
| 32 | + else if (path.startsWith('/infantil/')) { |
| 33 | + return { sectionName: 'Clan TV' } |
| 34 | + } |
| 35 | + else if (path.startsWith('/playz/')) { |
| 36 | + return { sectionName: 'Playz' } |
| 37 | + } |
| 38 | + else if (path.startsWith('/eltiempo/')) { |
| 39 | + return { sectionName: 'El Tiempo' } |
| 40 | + } |
| 41 | + else if (path.includes('/play/radio/') || path.includes('/play/audios/')) { |
| 42 | + return { sectionName: 'RNE Radio' } |
| 43 | + } |
| 44 | + else if (path.includes('/series/') || path.includes('/programas/')) { |
| 45 | + return { sectionName: 'Series y Programas' } |
| 46 | + } |
| 47 | + else if (path.startsWith('/play/')) { |
| 48 | + return { sectionName: 'RTVE Play' } |
| 49 | + } |
| 50 | + else { |
| 51 | + return { sectionName: 'RTVE.es' } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +function getLogoToUse(): ActivityAssets { |
| 56 | + const path = document.location.pathname.toLowerCase() |
| 57 | + return path.startsWith('/play/') ? ActivityAssets.RTVE_PLAY : ActivityAssets.RTVE |
| 58 | +} |
| 59 | + |
| 60 | +async function getStrings() { |
| 61 | + return presence.getStrings({ |
| 62 | + play: 'general.playing', |
| 63 | + pause: 'general.paused', |
| 64 | + live: 'general.live', |
| 65 | + browsing: 'general.browsing', |
| 66 | + reading: 'general.reading', |
| 67 | + watchLive: 'general.buttonWatchStream', |
| 68 | + listenRadio: 'general.buttonListen', |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +presence.on('UpdateData', async () => { |
| 73 | + const [buttons, privacy, showCover] = await Promise.all([ |
| 74 | + presence.getSetting<boolean>('buttons'), |
| 75 | + presence.getSetting<boolean>('privacy'), |
| 76 | + presence.getSetting<boolean>('cover'), |
| 77 | + ]) |
| 78 | + |
| 79 | + const strings = await getStrings() |
| 80 | + const { href, pathname } = document.location |
| 81 | + const { sectionName } = getSectionInfo() |
| 82 | + const cleanTitle = getCleanTitle() |
| 83 | + const video = document.querySelector('video') |
| 84 | + const audio = document.querySelector('audio') |
| 85 | + |
| 86 | + const presenceData: PresenceData = { |
| 87 | + largeImageKey: showCover ? getLogoToUse() : ActivityAssets.RTVE, |
| 88 | + } |
| 89 | + |
| 90 | + // --- EN DIRECTO --- |
| 91 | + if (pathname.includes('/videos/directo/')) { |
| 92 | + presenceData.details = sectionName |
| 93 | + presenceData.state = privacy ? '' : cleanTitle |
| 94 | + presenceData.smallImageKey = Assets.Live |
| 95 | + presenceData.smallImageText = strings.live |
| 96 | + presenceData.startTimestamp = browsingTimestamp |
| 97 | + |
| 98 | + if (buttons && !privacy) { |
| 99 | + presenceData.buttons = [{ label: strings.watchLive, url: href }] |
| 100 | + } |
| 101 | + |
| 102 | + // --- RADIO --- |
| 103 | + } |
| 104 | + else if (pathname.includes('/play/radio/') || pathname.includes('/play/audios/')) { |
| 105 | + presenceData.details = sectionName |
| 106 | + presenceData.state = privacy ? '' : cleanTitle |
| 107 | + presenceData.smallImageKey = audio?.paused ? Assets.Pause : Assets.Play |
| 108 | + presenceData.smallImageText = audio?.paused ? strings.pause : strings.play |
| 109 | + presenceData.startTimestamp = browsingTimestamp |
| 110 | + |
| 111 | + if (buttons && !privacy) { |
| 112 | + presenceData.buttons = [{ label: strings.listenRadio, url: href }] |
| 113 | + } |
| 114 | + |
| 115 | + // --- VIDEO --- |
| 116 | + } |
| 117 | + else if (video && !Number.isNaN(video.duration)) { |
| 118 | + presenceData.details = sectionName |
| 119 | + presenceData.state = privacy ? '' : cleanTitle |
| 120 | + presenceData.smallImageKey = video.paused ? Assets.Pause : Assets.Play |
| 121 | + presenceData.smallImageText = video.paused ? strings.pause : strings.play |
| 122 | + presenceData.startTimestamp = browsingTimestamp |
| 123 | + |
| 124 | + if (buttons && !privacy) { |
| 125 | + presenceData.buttons = [{ label: 'Ver en RTVE Play', url: href }] |
| 126 | + } |
| 127 | + |
| 128 | + // --- NOTICIAS --- |
| 129 | + } |
| 130 | + else if (pathname.includes('/noticias/')) { |
| 131 | + presenceData.details = sectionName |
| 132 | + presenceData.state = privacy ? '' : cleanTitle |
| 133 | + presenceData.smallImageKey = Assets.Reading |
| 134 | + presenceData.smallImageText = strings.reading |
| 135 | + presenceData.startTimestamp = browsingTimestamp |
| 136 | + |
| 137 | + if (buttons && !privacy) { |
| 138 | + presenceData.buttons = [{ label: 'Leer noticia', url: href }] |
| 139 | + } |
| 140 | + |
| 141 | + // --- DEFAULT --- |
| 142 | + } |
| 143 | + else { |
| 144 | + presenceData.details = sectionName |
| 145 | + presenceData.state = privacy ? '' : cleanTitle |
| 146 | + presenceData.smallImageKey = Assets.Search |
| 147 | + presenceData.smallImageText = strings.browsing |
| 148 | + presenceData.startTimestamp = browsingTimestamp |
| 149 | + |
| 150 | + if (buttons && !privacy) { |
| 151 | + presenceData.buttons = [{ label: `Visitar ${sectionName}`, url: href }] |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if (privacy) { |
| 156 | + delete presenceData.state |
| 157 | + delete presenceData.buttons |
| 158 | + } |
| 159 | + |
| 160 | + presence.setActivity(presenceData) |
| 161 | +}) |
0 commit comments