-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathquery.js
39 lines (31 loc) · 988 Bytes
/
query.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
"use strict";
function query(){
var queryTerm = document.getElementById("queryTerm").value;
var queryURL = "http://en.wikipedia.org/w/api.php?" +
"action=query&" +
"prop=revisions&" +
"rvprop=content&" +
"rvexpandtemplates&" +
"format=json&" +
"callback=callback&" +
"indexpageids&" +
"redirects&" +
"titles=" + encodeURIComponent(queryTerm);
var request = document.createElement("script");
request.setAttribute("src", queryURL);
document.getElementsByTagName('head')[0].appendChild(request);
}
function callback(data){
var content = document.getElementById("content");
var pageid = data["query"]["pageids"][0];
if (pageid == "-1"){
content.innerHTML = "Page not found";
return;
}
var wikitext = data["query"]["pages"][pageid]["revisions"][0]["*"];
var parsed = txtwiki.parseWikitext(wikitext);
parsed = parsed.replace(/>/g, ">");
parsed = parsed.replace(/</g, "<");
parsed = parsed.replace(/\n/g, "<br>");
content.innerHTML = parsed;
}