-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc_handlers.js
More file actions
120 lines (102 loc) · 3.77 KB
/
Copy pathcalc_handlers.js
File metadata and controls
120 lines (102 loc) · 3.77 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const smartdown = window.smartdown;
function calcWikidataImageUtil(thumb, key, body, done) {
function lookupComplete() {
/* eslint no-invalid-this: 0 */
var lookupResult = this.responseText;
// console.log('lookupResult', JSON.parse(lookupResult));
var parsedResult = JSON.parse(lookupResult).query.pages;
var thumbs = [];
parsedResult.forEach(function(val, idx) {
if (thumb && val.thumbnail) {
thumbs.push(val.thumbnail.source);
}
else if (!thumb && val.original) {
thumbs.push(val.original.source);
}
});
thumbs.elementType = 'image';
done(thumbs);
}
var wdKey = body;
if (body.indexOf('[') === 0) {
var possibleJSONArray = JSON.parse(body);
if (Array.isArray(possibleJSONArray)) {
wdKey = '';
possibleJSONArray.forEach(function(val, idx) {
wdKey += val;
if (idx < possibleJSONArray.length - 1) {
wdKey += '|';
}
});
}
}
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", lookupComplete);
// https://en.wikipedia.org/wiki/Special:ApiSandbox#action=query&format=json&prop=pageimages&titles=Albert+Einstein&piprop=thumbnail%7Cname%7Coriginal&pithumbsize=300
// /w/api.php?action=query&format=json&prop=pageimages&titles=Albert+Einstein&piprop=thumbnail%7Cname%7Coriginal&pithumbsize=300
var url = 'https://en.wikipedia.org/w/api.php?action=query&prop=pageimages%7Cpageterms&format=json&origin=*&wbptterms=description&redirects=&formatversion=2';
url += '&titles=' + encodeURI(wdKey);
url += '&piprop=thumbnail%7Cname%7Coriginal&pithumbsize=300';
/*
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", lookupComplete);
var url = 'https://en.wikipedia.org/w/api.php?action=query&formatversion=2&prop=pageimages%7Cpageterms&';
url += 'titles=' + encodeURI(wdKey);
url += '&pilimit=3&piprop=thumbnail&wbptterms=description&redirects=&format=json&origin=*';
*/
oReq.open('GET', url);
oReq.send();
}
function calcWikidataImages(key, body, done) {
return calcWikidataImageUtil(false, key, body, done);
}
function calcWikidataThumbs(key, body, done) {
return calcWikidataImageUtil(true, key, body, done);
}
function calcWikidata(key, body, done) {
function lookupComplete() {
/* eslint no-invalid-this: 0 */
var lookupResult = this.responseText;
// console.log('calcWikidata', JSON.parse(lookupResult));
var parsedResult = JSON.parse(lookupResult).query.pages;
// console.log('calcWikidata parsedResult', parsedResult);
var info = [];
window.lodashEach(parsedResult, function(val, idx) {
// console.log('...val', val);
info.push({
title: val.displaytitle,
url: val.canonicalurl
});
});
info.elementType = 'title/url';
done(info);
}
var wdKey = body;
if (body.indexOf('[') === 0) {
var possibleJSONArray = JSON.parse(body);
if (Array.isArray(possibleJSONArray)) {
wdKey = '';
possibleJSONArray.forEach(function(val, idx) {
wdKey += val;
if (idx < possibleJSONArray.length - 1) {
wdKey += '|';
}
});
}
}
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", lookupComplete);
var url = 'https://en.wikipedia.org/w/api.php?action=query&format=json&prop=info&origin=*&redirects=';
url += '&titles=' + encodeURI(wdKey);
url += '&inprop=displaytitle%7Curl';
// var url = 'https://en.wikipedia.org/w/api.php?action=query&formatversion=2&prop=pageimages%7Cpageterms&';
// url += 'titles=' + encodeURI(wdKey);
// url += '&pilimit=3&piprop=thumbnail&wbptterms=description&redirects=&format=json&origin=*';
oReq.open('GET', url);
oReq.send();
}
smartdown.defaultCalcHandlers = {
wikidataImages: calcWikidataImages,
wikidataThumbs: calcWikidataThumbs,
wikidata: calcWikidata
};