Skip to content

Commit

Permalink
fixed researchgate.net bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fuddl committed Jan 17, 2022
1 parent a84fa3e commit 3885a1d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
63 changes: 37 additions & 26 deletions src/content/content__collect-ld.js
Original file line number Diff line number Diff line change
@@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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));
Expand All @@ -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 };
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 3885a1d

Please sign in to comment.