-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Apple Podcasts): add presence (#8726)
- Loading branch information
Showing
2 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"$schema": "https://schemas.premid.app/metadata/1.10", | ||
"author": { | ||
"name": "ray", | ||
"id": "516333697163853828" | ||
}, | ||
"service": "Apple Podcasts", | ||
"description": { | ||
"en": "Apple Podcasts is an audio streaming service and media player application developed by Apple for playing podcasts." | ||
}, | ||
"url": "podcasts.apple.com", | ||
"version": "1.0.0", | ||
"logo": "https://i.imgur.com/ibW2CvC.png", | ||
"thumbnail": "https://i.imgur.com/uwYJseY.jpeg", | ||
"color": "#6b2ad0", | ||
"category": "music", | ||
"tags": [ | ||
"apple", | ||
"podcast", | ||
"audio", | ||
"listen" | ||
], | ||
"settings": [ | ||
{ | ||
"id": "privacy", | ||
"title": "Privacy Mode", | ||
"icon": "fad fa-user-secret", | ||
"value": false | ||
}, | ||
{ | ||
"id": "useEpisodeAsTitle", | ||
"title": "Use Episode as Title", | ||
"icon": "fad fa-podcast", | ||
"value": false | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
const presence = new Presence({ | ||
clientId: "1281538997235224596", | ||
injectOnComplete: true, | ||
}), | ||
browsingTimestamp = Math.floor(Date.now() / 1000); | ||
|
||
const enum Assets { // Other default assets can be found at index.d.ts | ||
Logo = "https://i.imgur.com/ibW2CvC.png", | ||
} | ||
|
||
presence.on("UpdateData", async () => { | ||
const presenceData: PresenceData = { | ||
largeImageKey: Assets.Logo, | ||
startTimestamp: browsingTimestamp, | ||
}, | ||
strings = await presence.getStrings({ | ||
play: "general.playing", | ||
pause: "general.paused", | ||
browsing: "general.browsing", | ||
}), | ||
[privacy, useEpisodeAsTitle] = await Promise.all([ | ||
presence.getSetting("privacy"), | ||
presence.getSetting("useEpisodeAsTitle"), | ||
]), | ||
shadowRootDir = document.querySelector(".lcd").shadowRoot; | ||
|
||
if (shadowRootDir?.querySelector(".lcd__active").ariaHidden !== "true") { | ||
presenceData.type = ActivityType.Listening; | ||
const episodeImage = shadowRootDir | ||
.querySelector<HTMLImageElement>(".lcd__artwork > picture > img") | ||
.src.replace("88x88", "450x450"), | ||
playingShadow = document.querySelector(".chrome-player").shadowRoot, | ||
episodeTitle = shadowRootDir.querySelector( | ||
".lcd-meta-line__fragment" | ||
).textContent, | ||
podcastName = shadowRootDir.querySelectorAll( | ||
".lcd-meta-line__fragment" | ||
)[2].textContent; | ||
|
||
presenceData.largeImageKey = episodeImage; | ||
|
||
if (useEpisodeAsTitle) { | ||
presenceData.name = episodeTitle; | ||
presenceData.details = podcastName; | ||
} else { | ||
presenceData.name = podcastName; | ||
presenceData.details = episodeTitle; | ||
} | ||
presenceData.state = "on Apple Podcasts"; | ||
|
||
const progress = shadowRootDir.querySelector("#playback-progress"), | ||
elapsedSeconds = Number(progress.ariaValueNow); | ||
|
||
if ( | ||
playingShadow.querySelector(".playback-play__pause").ariaHidden !== "true" | ||
) { | ||
const ts = Date.now() / 1000; | ||
presenceData.startTimestamp = ts - elapsedSeconds; | ||
presenceData.endTimestamp = | ||
ts + Number(progress.ariaValueMax) - elapsedSeconds; | ||
|
||
presenceData.smallImageKey = Assets.Play; | ||
presenceData.smallImageText = strings.play; | ||
} else if ( | ||
playingShadow.querySelector(".playback-play__play").ariaHidden !== "true" | ||
) { | ||
presenceData.startTimestamp = browsingTimestamp; | ||
presenceData.smallImageKey = Assets.Pause; | ||
presenceData.smallImageText = strings.pause; | ||
} | ||
} else if (document.location.pathname.includes("/podcast")) { | ||
presenceData.details = document.querySelector( | ||
".headings > .headings__title" | ||
).textContent; | ||
presenceData.state = document.querySelector( | ||
".headings > .headings__subtitles" | ||
).textContent; | ||
presenceData.largeImageKey = | ||
document.querySelector<HTMLImageElement>("picture > img").currentSrc; | ||
presenceData.smallImageKey = Assets.Reading; | ||
presenceData.smallImageText = strings.browsing; | ||
} else if (document.location.pathname.includes("/home")) | ||
presenceData.details = "On the home page"; | ||
else if (document.location.pathname.includes("/charts")) | ||
presenceData.details = "Viewing top charts"; | ||
else if (document.location.pathname.includes("/library")) | ||
presenceData.details = "Viewing library"; | ||
else if (document.location.pathname.includes("/settings")) | ||
presenceData.details = "Viewing settings"; | ||
else if (document.location.pathname.includes("/search")) { | ||
presenceData.details = "Searching for podcasts"; | ||
if (document.location.search.includes("?term=")) { | ||
presenceData.details = `Searched for ${decodeURIComponent( | ||
document.location.search.split("?term=")[1] | ||
)}`; | ||
} else { | ||
presenceData.smallImageKey = Assets.Search; | ||
presenceData.details = "Searching..."; | ||
} | ||
} else { | ||
presenceData.details = "Browsing..."; | ||
presenceData.smallImageKey = Assets.Search; | ||
presenceData.smallImageText = strings.browsing; | ||
} | ||
|
||
if (presenceData.state && privacy) delete presenceData.state; | ||
|
||
if (presenceData.details && privacy) delete presenceData.details; | ||
|
||
if (presenceData.details) presence.setActivity(presenceData); | ||
else { | ||
presenceData.type = ActivityType.Listening; | ||
presence.setActivity(); | ||
} | ||
}); |