1- import { ActivityType , Assets , getTimestamps , getTimestampsFromMedia } from 'premid'
1+ import type { Playback , PlaybackInfo } from './types.js'
2+ import { ActivityType , Assets , getTimestamps , getTimestampsFromMedia , StatusDisplayType } from 'premid'
3+ import { ActivityAssets , ListItemStatus , StaticBrowsing } from './constants.js'
4+ import { append , determineSeason , getAnimeIcon , getProfilePicture , getUserID } from './utils.js'
25
36const presence = new Presence ( { clientId : '1137362720254074972' } )
47
@@ -10,91 +13,6 @@ let playbackInfo: PlaybackInfo | null
1013
1114let lastWatched : Playback
1215
13- interface PlaybackInfo {
14- currTime : number
15- duration : number
16- paused : boolean
17- }
18-
19- interface Playback {
20- animeID : string
21- episode : string
22- }
23-
24- interface PictureCache {
25- id : string
26- url : string
27- date : Date
28- }
29-
30- enum ListItemStatus {
31- categoryWatching = 1 ,
32- categoryWatched = 2 ,
33- categoryPlanning = 3 ,
34- categorySuspended = 4 ,
35- categoryAbandoned = 5 ,
36- }
37-
38- enum ActivityAssets {
39- Logo = 'https://cdn.rcd.gg/PreMiD/websites/O/ogladajanime/assets/0.png' ,
40- DefaultProfilePicture = 'https://cdn.rcd.gg/PreMiD/websites/O/OgladajAnime/assets/0.png' ,
41- }
42-
43- const cache : PictureCache [ ] = [ ]
44-
45- function cacheExpired ( date : Date ) : boolean {
46- if ( date === null )
47- return true
48-
49- return ( ( new Date ( ) . getTime ( ) - date . getTime ( ) ) / 1000 / 60 ) < 5
50- }
51-
52- async function getProfilePicture ( id : string ) : Promise < string | undefined > {
53- const index = cache . findIndex ( ( val , _ , __ ) => val . id === id )
54- const _val = cache [ index ]
55- if ( index === - 1 ) {
56- const _new : PictureCache = { id, url : await internal_getPFP ( id ) , date : new Date ( ) }
57- cache . push ( _new )
58- return _new . url
59- }
60- else if ( _val !== undefined && cacheExpired ( _val . date ) ) {
61- const url = await internal_getPFP ( id )
62- _val . url = url
63- _val . date = new Date ( )
64- return url
65- }
66- else {
67- return cache [ index ] ?. url
68- }
69- }
70-
71- async function internal_getPFP ( id : string ) {
72- let url = `https://cdn.ogladajanime.pl/images/user/${ id } .webp?${ new Date ( ) . getTime ( ) } `
73- try {
74- const res = await fetch ( new URL ( url ) )
75- if ( res . status === 404 )
76- url = ActivityAssets . DefaultProfilePicture
77- }
78- catch {
79- url = ActivityAssets . DefaultProfilePicture
80- }
81- return url
82- }
83-
84- function getUserID ( ) {
85- const dropdowns = document . querySelectorAll ( 'a[class="dropdown-item"]' )
86- let found = false
87- dropdowns . forEach ( ( elem , _ , __ ) => {
88- const href = elem . getAttribute ( 'href' )
89- if ( href != null && href . startsWith ( '/profile/' ) ) {
90- userID = Number . parseInt ( href . replace ( '/profile/' , '' ) )
91- found = true
92- }
93- } )
94- if ( ! found )
95- userID = 0
96- }
97-
9816function getPlayerInfo ( ) : [ isPaused : boolean , timestamp : [ start : number , end : number ] ] {
9917 const player = getOAPlayer ( )
10018 if ( player ?. paused === false )
@@ -117,17 +35,6 @@ function getOAPlayer(): HTMLVideoElement | undefined {
11735 return undefined
11836}
11937
120- function append ( text : string , append : string | undefined | null , separator : string = ': ' ) : string {
121- if ( append ?. trim ( ) ?. replace ( ' ' , '' ) )
122- return `${ text } ${ separator } ${ append } `
123- else
124- return text
125- }
126-
127- function getAnimeIcon ( id : number | string ) : string {
128- return `https://cdn.ogladajanime.pl/images/anime_new/${ id } /2.webp`
129- }
130-
13138async function getStrings ( ) {
13239 return presence . getStrings (
13340 {
@@ -206,31 +113,6 @@ async function getStrings() {
206113 )
207114}
208115
209- // ! - use localization strings
210- // !? - uses the format: Viewing Page: {new line} {provided localization string}, example: !?terms
211- const staticBrowsing = {
212- '/watch2gether' : '!browsingRooms' ,
213- '/main2' : '!viewHome' ,
214- '/search/name/' : '!searchSomething' ,
215- '/search/custom' : '!searchSomething' ,
216- '/search/rand' : '!random' ,
217- '/search/new' : '!new' ,
218- '/search/main' : '!topRated' ,
219- '/chat' : '!chatting' ,
220- '/user_activity' : '!lastActivity' ,
221- '/last_comments' : '!?newestComments' ,
222- '/active_sessions' : '!?activeLoginSessions' ,
223- '/manage_edits' : '!?newestEdits' ,
224- '/anime_list_to_load' : '!importList' ,
225- '/discord' : '!?contact' ,
226- '/support' : '!?donate' ,
227- '/rules' : '!?terms' ,
228- '/harmonogram' : '!?upcomingEpisodes' ,
229- '/anime_seasons' : '!?upcomingAnimes' ,
230- '/all_anime_list' : '!?allAvailableAnimes' ,
231- '/' : '!viewHome' , // This MUST stay at the end, otherwise this will always display no matter the page
232- }
233-
234116let strings : Awaited < ReturnType < typeof getStrings > >
235117let oldLang : string | null = null
236118
@@ -256,18 +138,19 @@ presence.on('iFrameData', (data) => {
256138} )
257139
258140presence . on ( 'UpdateData' , async ( ) => {
259- getUserID ( )
141+ userID = getUserID ( )
260142
261143 const { pathname } = document . location
262144
263- const [ newLang , browsingStatusEnabled , useAltName , hideWhenPaused , titleAsPresence , showSearchContent , showCover ] = await Promise . all ( [
145+ const [ newLang , browsingStatusEnabled , useAltName , hideWhenPaused , titleAsPresence , showSearchContent , showCover , determineSeasonSetting ] = await Promise . all ( [
264146 presence . getSetting < string > ( 'lang' ) . catch ( ( ) => 'en' ) ,
265147 presence . getSetting < boolean > ( 'browsingStatus' ) ,
266148 presence . getSetting < boolean > ( 'useAltName' ) ,
267149 presence . getSetting < boolean > ( 'hideWhenPaused' ) ,
268150 presence . getSetting < boolean > ( 'titleAsPresence' ) ,
269151 presence . getSetting < boolean > ( 'showSearchContent' ) ,
270152 presence . getSetting < boolean > ( 'showCover' ) ,
153+ presence . getSetting < boolean > ( 'determineSeason' ) ,
271154 ] )
272155
273156 if ( oldLang !== newLang || ! strings ) {
@@ -302,13 +185,26 @@ presence.on('UpdateData', async () => {
302185 const epNum = activeEpisode ?. getAttribute ( 'value' ) ?? 0
303186 const epName = activeEpisode ?. querySelector ( 'p' ) ?. textContent
304187
188+ let season : number = - 1
189+ if ( determineSeasonSetting ) {
190+ const d = determineSeason ( useAltName )
191+ if ( d && d . found ) {
192+ name = d . name
193+ season = d . season
194+ }
195+ }
196+
305197 if ( name ) {
198+ presenceData . details = name
306199 if ( titleAsPresence )
307- presenceData . name = name
200+ presenceData . statusDisplayType = StatusDisplayType . Details
308201 else
309- presenceData . details = name
202+ presenceData . statusDisplayType = StatusDisplayType . Name
310203
311- presenceData . state = append ( `${ strings . episode } ${ epNum } ` , epName , ' • ' )
204+ if ( season !== - 1 )
205+ presenceData . state = epName
206+ else
207+ presenceData . state = append ( `${ strings . episode } ${ epNum } ` , epName , ' • ' )
312208 }
313209 else {
314210 return presence . clearActivity ( )
@@ -336,7 +232,9 @@ presence.on('UpdateData', async () => {
336232 presenceData . smallImageText = strings . browsing
337233 }
338234
339- if ( rating && voteCount )
235+ if ( season !== - 1 )
236+ presenceData . largeImageText = `Season ${ season } , Episode ${ epNum } `
237+ else if ( rating && voteCount )
340238 presenceData . largeImageText = `${ rating . textContent } • ${ strings . votes . replace ( '{0}' , voteCount ) } `
341239
342240 if ( animeID && showCover )
@@ -357,11 +255,11 @@ presence.on('UpdateData', async () => {
357255 const roomName = spans [ spans . length - 1 ] ?. textContent
358256
359257 if ( name && name . textContent ) {
258+ presenceData . details = name . textContent
360259 if ( titleAsPresence )
361- presenceData . name = name . textContent
260+ presenceData . statusDisplayType = StatusDisplayType . Details
362261 else
363- presenceData . details = name . textContent
364-
262+ presenceData . statusDisplayType = StatusDisplayType . Name
365263 presenceData . state = append ( `${ strings . episode } ${ episode } ` , `${ strings . room } '${ roomName } '` , ' • ' )
366264 }
367265 else {
@@ -466,9 +364,8 @@ presence.on('UpdateData', async () => {
466364
467365 const commentsCount = ( comments ?. length ?? 1 ) - 1
468366
469- if ( name ) {
367+ if ( name )
470368 presenceData . details = strings . viewCommentsOf . replace ( '{0}' , name ?? 'N/A' )
471- }
472369
473370 presenceData . state = strings . commentCount . replace ( '{0}' , commentsCount . toString ( ) ) . replace ( '{1}' , likes . toString ( ) ) . replace ( '{2}' , dislikes . toString ( ) )
474371
@@ -574,7 +471,7 @@ presence.on('UpdateData', async () => {
574471 else {
575472 if ( browsingStatusEnabled ) {
576473 let recognized = false
577- for ( const [ key , value ] of Object . entries ( staticBrowsing ) ) {
474+ for ( const [ key , value ] of Object . entries ( StaticBrowsing ) ) {
578475 if ( pathname . includes ( key ) ) {
579476 const parsed = parseString ( value )
580477 presenceData . details = parsed [ 0 ]
0 commit comments