@@ -71,11 +71,16 @@ function formatTitleForSuggestion(resultItem) {
71
71
}
72
72
73
73
function formatDateString ( dateString ) {
74
- const [ day , month , year ] = dateString . split ( ' ' ) ;
74
+ if ( ! dateString || dateString === "N/A" ) return "" ;
75
+ const parts = dateString . split ( ' ' ) ;
76
+ if ( parts . length !== 3 ) return dateString ;
77
+ const [ day , month , year ] = parts ;
75
78
const monthNames = [ 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' , 'Jul' , 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec' ] ;
76
79
const monthIndex = monthNames . indexOf ( month ) ;
80
+ if ( monthIndex < 0 ) return dateString ;
77
81
78
- const date = new Date ( year , monthIndex , day ) ;
82
+ const date = new Date ( Number ( year ) , monthIndex , Number ( day ) ) ;
83
+ if ( isNaN ( date . getTime ( ) ) ) return dateString ;
79
84
80
85
// Format the date as yyyy-mm-dd
81
86
const formattedYear = date . getFullYear ( ) ;
@@ -112,25 +117,35 @@ async function getByImdbId(id) {
112
117
}
113
118
114
119
function linkifyList ( list ) {
115
- if ( list . length === 0 ) return "" ;
116
- if ( list . length === 1 ) return `\n - "[[${ list [ 0 ] } ]]"` ;
120
+ if ( ! Array . isArray ( list ) || list . length === 0 ) return "" ;
121
+ if ( list . length === 1 ) return `\n - "[[${ list [ 0 ] . trim ( ) } ]]"` ;
117
122
118
123
return list . map ( item => `\n - "[[${ item . trim ( ) } ]]"` ) . join ( "" ) ;
119
124
}
120
125
121
- function replaceIllegalFileNameCharactersInString ( string ) {
122
- return string . replace ( / [ \\ , # % & \{ \} \/ * < > $ \' \" : @ ] * / g, '' ) ;
126
+ function replaceIllegalFileNameCharactersInString ( input ) {
127
+ if ( ! input ) return "" ;
128
+ return input . replace ( / [ \\ , # % & \{ \} \/ * < > $ ' \" : @ ] / g, '' ) . trim ( ) ;
123
129
}
124
130
125
- async function apiGet ( url , data ) {
126
- let finalURL = new URL ( url ) ;
127
- if ( data )
128
- Object . keys ( data ) . forEach ( key => finalURL . searchParams . append ( key , data [ key ] ) ) ;
131
+ async function apiGet ( _url , data ) {
132
+ const params = new URLSearchParams ( ) ;
133
+ if ( data ) {
134
+ Object . entries ( data ) . forEach ( ( [ key , value ] ) => {
135
+ if ( value != null && value !== '' ) params . append ( key , String ( value ) ) ;
136
+ } ) ;
137
+ }
129
138
130
- finalURL . searchParams . append ( "apikey" , Settings [ API_KEY_OPTION ] ) ;
139
+ const apiKey = Settings ?. [ API_KEY_OPTION ] ;
140
+ if ( ! apiKey || String ( apiKey ) . trim ( ) === '' ) {
141
+ notice ( 'Please set your OMDb API key in the script settings.' ) ;
142
+ throw new Error ( 'Missing OMDb API key.' ) ;
143
+ }
144
+ params . append ( 'apikey' , String ( apiKey ) . trim ( ) ) ;
131
145
146
+ const href = `${ API_URL } ?${ params . toString ( ) } ` ;
132
147
const res = await request ( {
133
- url : finalURL . href ,
148
+ url : href ,
134
149
method : 'GET' ,
135
150
cache : 'no-cache' ,
136
151
headers : {
0 commit comments