-
Notifications
You must be signed in to change notification settings - Fork 6
/
search.js
123 lines (108 loc) · 4.3 KB
/
search.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
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
121
122
123
document.addEventListener("DOMContentLoaded", function() {
chrome.storage.sync.get({"search": "google", "searchplace": "bottom"}, searchCallback);
});
var jsonmain;
function jsonparse(json) {
jsonmain = json;
searchbox = document.getElementById("search-box");
if (json.AbstractSource == "Wikipedia") {
document.getElementById("duckduckres").innerHTML = "";
var script = document.createElement("script");
script.setAttribute("src", "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles="+json.AbstractURL.split('/')[json.AbstractURL.split('/').length-1]+"&callback=wikicallback");
//script.setAttribute("src", "https://en.wikipedia.org/w/api.php?format=json&action=query&list=search&prop=extracts&exintro=&explaintext=&callback=wikicallback&srsearch="+searchbox.value);
document.body.appendChild(script);
script.outerHTML = "";
script.delete;
if (typeof json.RelatedTopics[0] != "undefined") {
var div = document.createElement('div');
div.innerHTML = json.RelatedTopics[0].Result;
var a = div.getElementsByTagName("a")[0];
a.className = "extrares";
document.getElementById("duckduckres").appendChild(a);
div.innerHTML = json.RelatedTopics[1].Result;
var a = div.getElementsByTagName("a")[0];
a.className = "extrares";
document.getElementById("duckduckres").appendChild(a);
div.innerHTML = json.RelatedTopics[2].Result;
var a = div.getElementsByTagName("a")[0];
a.className = "extrares";
document.getElementById("duckduckres").appendChild(a);
}
}
}
function wikicallback(json) {
var page = json.query.pages[Object.keys(json.query.pages)[0]];
//console.log(json.query.pages);
var a = document.createElement('a');
a.href = "http://en.wikipedia.org/?curid="+page.pageid;
a.innerText = page.title + " - "+page.extract;
if (page.extract != "") {
a.innerText = page.title + " - "+page.extract;
} else {
a.innerText = page.title;
}
a.className = "mainres";
[].forEach.call(document.querySelectorAll('.mainres'),function(e){
e.parentNode.removeChild(e);
});
document.getElementById("wikires").appendChild(a);
}
var searchCallback = function(list) {
var form;
var searchbox;
var searchplace = list["searchplace"];
var formstring = "<form id=\"search-form\" method=\"get\" action=\"https://google.com/search?\">\
<input id=\"search-box\" name=\"q\" type=\"text\" placeholder=\"search\" autofocus autocomplete='on'/>\
</form>";
if (searchplace == "top") {
document.getElementById("search1").className += " active";
document.getElementById("search1").insertAdjacentHTML("beforeend", formstring);
} else if (searchplace == "middle") {
document.getElementById("search2").className += " active";
document.getElementById("search2").insertAdjacentHTML("beforeend", formstring);
} else {
document.getElementById("search3").className += " active";
document.getElementById("search3").insertAdjacentHTML("beforeend", formstring);
}
form = document.getElementById("search-form");
searchbox = document.getElementById("search-box");
searchbox.onkeyup = function(){
var val = searchbox.value;
if (val.length > 1) {
var script = document.createElement('script');
script.src = 'https://api.duckduckgo.com/?q='+searchbox.value+'&format=json&callback=jsonparse';
document.body.appendChild(script);
script.outerHTML = "";
script.delete;
}
if (val.length == 0) {
document.getElementById("callback").style.visibility = "hidden";
}
else {
document.getElementById("callback").style.visibility = "visible";
}
}
if (list["search"] == "duckduckgo") {
form.setAttribute("action", "https://duckduckgo.com/?");
} else if (list["search"] == "yahoo") {
form.setAttribute("action", "https://search.yahoo.com/search?");
} else if (list["search"] == "bing") {
form.setAttribute("action", "https://www.bing.com/search?");
}
form.addEventListener("submit",
function(e){
var value = searchbox.value;
e.preventDefault();
var value = searchbox.value;
var url = asUrl(value);
if (url != null) {
window.location = url;
} else {
form.submit();
}
}, false);
}
function asUrl(str) {
// If the url is valid (with or without http/s), add http:// if missing and return - otherwise return null
return (/^(?:(http|https):\/\/)?(?:[\w-]+\.)+[a-z]{2,24}(\/[\w-]*)*(\.[\w-]+)?$/i.test(str)) ? addhttp(str) : null;
}