1
+ let overseerrContainer , senscritiqueId , currentSenscritiqueUrl , tmdbId , mediaType , mediaInfo ;
2
+
3
+ containerOptions . anchorElement = 'div[data-testid="product-infos"]' ;
4
+ containerOptions . textClass = 'text-sm' ;
5
+ containerOptions . containerClass = 'oa-mt-2 oa-py-2' ;
6
+ containerOptions . plexButtonClass = 'oa-bg-gray-800' ;
7
+ containerOptions . badgeBackground = '#032541' ;
8
+
9
+ mediaType = document . location . pathname . startsWith ( '/film' ) ? 'movie' : 'tv' ;
10
+ const senscritiqueRegex = / \/ (?: f i l m | s e r i e ) \/ \w * \/ ( \d + ) / ;
11
+
12
+ chrome . runtime . onMessage . addListener ( ( message , sender , sendResponse ) => {
13
+ if ( message . newUrl && message . newUrl !== currentSenscritiqueUrl ) {
14
+ currentSenscritiqueUrl = message . newUrl ;
15
+ setTimeout ( ( ) => {
16
+ checkForMedia ( message . newUrl ) ;
17
+ } , 500 ) ;
18
+ }
19
+ } ) ;
20
+
21
+ chrome . runtime . sendMessage ( { contentScriptQuery : 'listenForUrlChange' } ) ;
22
+
23
+ const checkForMedia = async ( urlToCheck ) => {
24
+ let matches = urlToCheck . match ( senscritiqueRegex ) ;
25
+ if ( matches !== null && matches . length > 1 ) {
26
+ mediaType = document . location . pathname . startsWith ( '/film' ) ? 'movie' : 'tv' ;
27
+
28
+ const titleElement = document . querySelector ( 'h1' ) ;
29
+ let title = titleElement . textContent ;
30
+ let releaseYear = extractYear ( document . querySelector ( 'p[data-testid="creators"]' ) . textContent ) ;
31
+ let displayedYear = parseInt ( titleElement . nextElementSibling . querySelector ( 'p:not([opacity])' ) . textContent ) ;
32
+
33
+ initializeContainer ( ) ;
34
+ insertSpinner ( ) ;
35
+
36
+ pullStoredData ( async function ( ) {
37
+ if ( ! userId ) {
38
+ removeSpinner ( ) ;
39
+ insertNotLoggedInButton ( ) ;
40
+ return ;
41
+ }
42
+
43
+ try {
44
+ let json = await sendMessageToBackground ( { contentScriptQuery : 'search' , title : title } ) ;
45
+ json . results = filterResults ( json . results , releaseYear , displayedYear ) ;
46
+ if ( json . results . length === 0 ) {
47
+ removeSpinner ( ) ;
48
+ insertStatusButton ( 'Media not found' , 0 ) ;
49
+ return ;
50
+ }
51
+ const firstResult = json . results [ 0 ] ;
52
+ json = await sendMessageToBackground ( { contentScriptQuery : 'queryMedia' , tmdbId : firstResult . id , mediaType : mediaType } ) ;
53
+ mediaInfo = json ;
54
+ tmdbId = json . id ;
55
+ console . log ( `TMDB id: ${ tmdbId } ` ) ;
56
+ removeSpinner ( ) ;
57
+ fillContainer ( json . mediaInfo ) ;
58
+ } catch ( error ) {
59
+ console . error ( error ) ;
60
+ }
61
+ } ) ;
62
+ }
63
+ }
64
+
65
+ const extractYear = ( creatorInformationsString ) => {
66
+ let parts = creatorInformationsString . split ( '·' ) ;
67
+ if ( parts . length >= 3 ) {
68
+ let yearPart = parts [ 2 ] . trim ( ) ;
69
+ let regex = / \b ( \d { 4 } ) \b / ;
70
+ let match = yearPart . match ( regex ) ;
71
+ if ( match ) {
72
+ let year = match [ 1 ] ;
73
+ return parseInt ( year ) ;
74
+ }
75
+ }
76
+ return false ;
77
+ }
78
+
79
+ const sendMessageToBackground = ( message ) => {
80
+ return new Promise ( ( resolve , reject ) => {
81
+ chrome . runtime . sendMessage ( message , ( response ) => {
82
+ if ( chrome . runtime . lastError ) {
83
+ reject ( chrome . runtime . lastError ) ;
84
+ } else {
85
+ resolve ( response ) ;
86
+ }
87
+ } ) ;
88
+ } ) ;
89
+ }
90
+
91
+ const filterResults = ( results , releaseYear , displayedYear ) => {
92
+ return results . filter ( ( result ) => {
93
+ return result . mediaType === mediaType &&
94
+ ( ! releaseYear || ( result . releaseDate && [ releaseYear , displayedYear ] . includes ( parseInt ( result . releaseDate . slice ( 0 , 4 ) ) ) ) ) ;
95
+ } ) ;
96
+ }
0 commit comments