-
Notifications
You must be signed in to change notification settings - Fork 8
/
wikipedia.js
51 lines (43 loc) · 1.03 KB
/
wikipedia.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
async function wikipedia(value, tp, doc) {
let url = await tp.system.clipboard();
if (!isValidHttpUrl(url)) {
console.error("Invalid URL for " + value);
return "";
}
if (doc === undefined) {
let page = await tp.obsidian.request({ url });
let p = new DOMParser();
doc = p.parseFromString(page, "text/html");
}
let json = "";
try {
json = JSON.parse(
doc.querySelector("script[type='application/ld+json']").innerHTML
);
} catch (error) {
new Notice(error);
}
switch (value) {
case "title":
return json?.name || "";
case "url":
return json?.url || "";
case "image":
return json?.image || "";
case "headline":
// Short description
return json?.headline || "";
default:
new Notice("Incorrect parameter: " + value, 5000);
}
}
function isValidHttpUrl(string) {
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
return url.protocol === "http:" || url.protocol === "https:";
}
module.exports = wikipedia;