Skip to content

Commit 536a440

Browse files
committed
fix(docs): make movie example script robust to URL constructor issues and missing API key
Avoid new URL() to fix Invalid URL error, validate OMDb API key, harden date parsing, list linkifying, and filename sanitization to prevent runtime crashes.
1 parent b148ec6 commit 536a440

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

docs/docs/Examples/Attachments/movies.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,16 @@ function formatTitleForSuggestion(resultItem) {
7171
}
7272

7373
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;
7578
const monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
7679
const monthIndex = monthNames.indexOf(month);
80+
if (monthIndex < 0) return dateString;
7781

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;
7984

8085
// Format the date as yyyy-mm-dd
8186
const formattedYear = date.getFullYear();
@@ -112,25 +117,35 @@ async function getByImdbId(id) {
112117
}
113118

114119
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()}]]"`;
117122

118123
return list.map(item => `\n - "[[${item.trim()}]]"`).join("");
119124
}
120125

121-
function replaceIllegalFileNameCharactersInString(string) {
122-
return string.replace(/[\\,#%&\{\}\/*<>$\'\":@]*/g, '');
126+
function replaceIllegalFileNameCharactersInString(input) {
127+
if (!input) return "";
128+
return input.replace(/[\\,#%&\{\}\/*<>$'\":@]/g, '').trim();
123129
}
124130

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+
}
129138

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());
131145

146+
const href = `${API_URL}?${params.toString()}`;
132147
const res = await request({
133-
url: finalURL.href,
148+
url: href,
134149
method: 'GET',
135150
cache: 'no-cache',
136151
headers: {

0 commit comments

Comments
 (0)