-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(monochrome): add activity #10365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
p1nkhamster
wants to merge
12
commits into
PreMiD:main
Choose a base branch
from
p1nkhamster:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ff7a6cb
Create presence.ts
p1nkhamster cc57692
Add files via upload
p1nkhamster f8d87e9
feat(website): add Monochrome
p1nkhamster 01fa26c
feat(website): add Monochrome
p1nkhamster 347f3a9
feat(website): add Monochrome
p1nkhamster 0b59839
fix(style): resolve linting errors
p1nkhamster 7d6498b
fix: remove unused ts directive
p1nkhamster a486122
Change author name and update description in metadata
p1nkhamster dec8b9b
Update thumbnail URL in metadata.json
p1nkhamster f56a5c4
Refactor presence management and activity data handling
p1nkhamster b1bfd99
forgot npm run lint :p
p1nkhamster 138535f
Update regExp to include additional mirrors
p1nkhamster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,23 @@ | ||
| { | ||
| "$schema": "https://schemas.premid.app/metadata/1.16", | ||
| "apiVersion": 1, | ||
| "author": { | ||
| "id": "838652741965971517", | ||
| "name": "p1nkhamster" | ||
| }, | ||
| "service": "Monochrome", | ||
| "description": { | ||
| "en": "Monochrome is an open-source, privacy-respecting, ad-free TIDAL web UI, built on top of Hi-Fi." | ||
| }, | ||
| "url": "monochrome.samidy.com", | ||
| "regExp": "^https?[:][/][/]([a-z0-9-]+[.])*monochrome[.]samidy[.]com[/]", | ||
| "version": "1.0.0", | ||
| "logo": "https://i.imgur.com/XHf3H8Y.png", | ||
| "thumbnail": "https://i.imgur.com/XHf3H8Y.png", | ||
| "color": "#000000", | ||
| "category": "music", | ||
| "tags": [ | ||
| "music", | ||
| "tidal" | ||
| ] | ||
| } | ||
This file contains hidden or 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,102 @@ | ||
| declare const Presence: any | ||
|
|
||
| // Static Asset Configuration | ||
| enum ActivityAssets { | ||
| Logo = 'https://i.imgur.com/XHf3H8Y.png', | ||
| Play = 'https://i.imgur.com/ryNutI5.png', | ||
| Pause = 'https://i.imgur.com/TlMwR5i.png', | ||
| } | ||
|
|
||
| let presence | ||
|
|
||
| try { | ||
| presence = new Presence({ | ||
| clientId: '1459594619972096248', | ||
| }) | ||
| } | ||
| catch { | ||
| // Silent catch for production environments | ||
| } | ||
|
|
||
| if (presence) { | ||
p1nkhamster marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| presence.on('UpdateData', async () => { | ||
| // 1. DYNAMIC IMAGE LOGIC | ||
| // Default to the static logo | ||
| let currentLargeImage: string = ActivityAssets.Logo | ||
|
|
||
| // standard mediaSession check for high-res artwork | ||
| const artwork = navigator.mediaSession?.metadata?.artwork | ||
|
|
||
| if (artwork && artwork.length > 0) { | ||
| // Select the last image in the array (typically the highest resolution) | ||
| const coverUrl = artwork[artwork.length - 1]?.src | ||
| if (coverUrl) { | ||
| currentLargeImage = coverUrl | ||
| } | ||
| } | ||
|
|
||
| // 2. INITIALIZE ACTIVITY DATA | ||
| // We create a fresh object every tick to avoid 'undefined' property issues | ||
| const presenceData: any = { | ||
| type: 2, // ActivityType.Listening | ||
p1nkhamster marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| largeImageKey: currentLargeImage, | ||
| largeImageText: 'Listening on Monochrome', | ||
| // Default small icon (overwritten below if paused) | ||
| smallImageKey: ActivityAssets.Play, | ||
| smallImageText: 'Playing', | ||
| } | ||
|
|
||
| // 3. TEXT STRATEGY (Browser Tab) | ||
| // Parses "Song - Artist" or "Song • Artist" from the document title | ||
| const tabTitle = document.title || '' | ||
| let separator = '' | ||
|
|
||
| if (tabTitle.includes(' - ')) | ||
| separator = ' - ' | ||
| else if (tabTitle.includes(' • ')) | ||
| separator = ' • ' | ||
|
|
||
| if (separator) { | ||
| const parts = tabTitle.split(separator) | ||
| presenceData.details = parts[0]?.trim() || 'Unknown Song' | ||
| presenceData.state = parts.slice(1).join(separator).trim() || 'Unknown Artist' | ||
| } | ||
| else { | ||
| // Fallback for non-standard titles | ||
| presenceData.details = 'Monochrome' | ||
| presenceData.state = 'Listening...' | ||
| } | ||
|
|
||
| // 4. AUDIO STATUS & TIMESTAMPS | ||
| const mediaElement = document.querySelector('audio') | ||
|
|
||
| if (mediaElement) { | ||
| if (!mediaElement.paused) { | ||
| // -- PLAYING STATE -- | ||
| presenceData.smallImageKey = ActivityAssets.Play | ||
| presenceData.smallImageText = 'Playing' | ||
|
|
||
| // Calculate timestamps using native Date.now() for accuracy | ||
| const now = Date.now() | ||
| presenceData.startTimestamp = now - (mediaElement.currentTime * 1000) | ||
|
|
||
| // Only set endTimestamp if duration is finite and positive | ||
| if (mediaElement.duration && Number.isFinite(mediaElement.duration) && mediaElement.duration > 0) { | ||
| presenceData.endTimestamp = now + ((mediaElement.duration - mediaElement.currentTime) * 1000) | ||
| } | ||
| } | ||
| else { | ||
| // -- PAUSED STATE -- | ||
| presenceData.smallImageKey = ActivityAssets.Pause | ||
| presenceData.smallImageText = 'Paused' | ||
| // Note: We do not set timestamps here, effectively hiding the time bar | ||
| } | ||
|
|
||
| presence.setActivity(presenceData) | ||
| } | ||
| else { | ||
| // Clear activity if no audio player is found | ||
| presence.setActivity({}) | ||
p1nkhamster marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| }) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.