Skip to content

Commit

Permalink
Update lib/routes/sciencedirect/call-for-paper.ts
Browse files Browse the repository at this point in the history
Co-authored-by: Tony <TonyRL@users.noreply.github.com>
  • Loading branch information
etShaw-zh and TonyRL authored Jan 4, 2025
1 parent 2266180 commit 9e2b2cf
Showing 1 changed file with 2 additions and 17 deletions.
19 changes: 2 additions & 17 deletions lib/routes/sciencedirect/call-for-paper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,15 @@ async function handler(ctx) {
const $ = load(response.body);

// 1) Grab the JSON from the script tag with data-iso-key="_0".
const scriptJSON = $('script[data-iso-key="_0"]').html();
const scriptJSON = $('script[data-iso-key="_0"]').text();
if (!scriptJSON) {
throw new Error('Cannot find the script with data-iso-key="_0"');
}

// 2) Parse the JSON string
let data;
try {
// It looks like the script tag might contain a *stringified* JSON object,
// so we may need to parse twice:
// 1st parse: remove surrounding quotes if present
// 2nd parse: parse the resulting JSON object
//
// The snippet in your question shows something like:
// "<script type="application/json" data-iso-key="_0">"{"config": {...}, "callsForPapers": {...}}"</script>"
// sometimes it comes with extra quotes at the start/end.
// Adjust logic below if needed based on actual site response.

let raw = scriptJSON.trim();
if (raw.startsWith('"') && raw.endsWith('"')) {
raw = raw.substring(1, raw.length - 1);
raw = raw.replaceAll(String.raw`\"`, '"');
}
data = JSON.parse(raw);
data = JSON.parse(JSON.parse(scriptJSON ));

Check failure

Code scanning / ESLint

Enforce consistent spacing inside parentheses Error

There should be no space before this paren.
} catch (error) {
throw new Error(`Failed to parse embedded script JSON: ${error.message}`);
}
Expand Down

0 comments on commit 9e2b2cf

Please sign in to comment.