diff --git a/src/content/content__collect-ld.js b/src/content/content__collect-ld.js index d544094..4c79fa1 100644 --- a/src/content/content__collect-ld.js +++ b/src/content/content__collect-ld.js @@ -1,19 +1,20 @@ -import { resolvers } from './resolver.js'; +import { resolvers } from "./resolver.js"; async function parse(thing, ids, url) { - - if (thing.hasOwnProperty('@type') && ['BreadcrumbList'].includes(thing['@type'])) { + if ( + thing.hasOwnProperty("@type") && + ["BreadcrumbList"].includes(thing["@type"]) + ) { return null; } - if (thing.hasOwnProperty('url')) { - let link = document.createElement('a'); - - if (thing['url'].startsWith('/') || thing['url'].startsWith('.')) { - link.setAttribute('href', url); - link.pathname = thing['url']; + if (thing.hasOwnProperty("url")) { + let link = document.createElement("a"); + if (thing?.["url"]?.startsWith("/") || thing?.["url"]?.startsWith(".")) { + link.setAttribute("href", url); + link.pathname = thing["url"]; } else { - link.setAttribute('href', thing['url']); + link.setAttribute("href", thing["url"]); } for (let id of Object.keys(resolvers)) { @@ -22,10 +23,10 @@ async function parse(thing, ids, url) { let entityId = await resolvers[id].getEntityId(link); if (entityId) { let wdUrl = `https://www.wikidata.org/wiki/${entityId}`; - if (Array.isArray(thing['sameAs'])) { - thing['sameAs'].push(wdUrl); + if (Array.isArray(thing["sameAs"])) { + thing["sameAs"].push(wdUrl); } else { - thing['sameAs'] = wdUrl; + thing["sameAs"] = wdUrl; } } else { if (JSON.stringify(isApplicable) === JSON.stringify(ids)) { @@ -38,23 +39,30 @@ async function parse(thing, ids, url) { for (let prop in thing) { if (Array.isArray(thing[prop])) { for (let i in thing[prop]) { - if (typeof thing[prop][i] === 'object' && thing[prop][i].hasOwnProperty('@type')) { - thing[prop][i] = await parse(thing[prop][i], ids, url) + if ( + typeof thing[prop][i] === "object" && + thing[prop][i].hasOwnProperty("@type") + ) { + thing[prop][i] = await parse(thing[prop][i], ids, url); } } - } else if (typeof thing[prop] === 'object' && thing[prop].hasOwnProperty('@type')) { - thing[prop] = await parse(thing[prop], ids, url) + } else if ( + thing?.[prop] && + typeof thing[prop] === "object" && + thing[prop].hasOwnProperty("@type") + ) { + thing[prop] = await parse(thing[prop], ids, url); } } return thing; } function jsonParse(i) { - return JSON.parse(i.replace(/\/\*[\s\S]*?\*\//g, '')); + return JSON.parse(i.replace(/\/\*[\s\S]*?\*\//g, "")); } async function enrichLinkedData(snippeds, ids, url) { - let parsed = [] + let parsed = []; for (let snipped of snippeds) { parsed.push(await parse(jsonParse(snipped.innerHTML), ids, url)); @@ -65,22 +73,25 @@ async function enrichLinkedData(snippeds, ids, url) { }); if (parsed.length === 1) { - parsed[parsed.findIndex(() => { - return true; - })].isNeedle = true; + parsed[ + parsed.findIndex(() => { + return true; + }) + ].isNeedle = true; } return parsed; - } function findLinkedData(document) { - const snippeds = document.querySelectorAll('script[type="application/ld+json"]'); + const snippeds = document.querySelectorAll( + 'script[type="application/ld+json"]' + ); if (snippeds.length < 1) { - return [] + return []; } return snippeds; } -export { findLinkedData, enrichLinkedData } +export { findLinkedData, enrichLinkedData }; diff --git a/src/manifest.json b/src/manifest.json index a3fe450..f7b5cfe 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Wikidata", - "version": "0.214", + "version": "0.215", "description": "Display information from wikidata.", "icons": { "48": "icons/wd.svg",