ef416274151211dde6371e5d15794734faec5077
1document.addEventListener("DOMContentLoaded", function() {
2 chrome.storage.sync.get({"search": "google", "searchplace": "bottom"}, searchCallback);
3});
4
5var jsonmain;
6
7function jsonparse(json) {
8 jsonmain = json;
9 searchbox = document.getElementById("search-box");
10 if (json.AbstractSource == "Wikipedia") {
11 document.getElementById("duckduckres").innerHTML = "";
12 var script = document.createElement("script");
13 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");
14 //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);
15 document.body.appendChild(script);
16 script.outerHTML = "";
17 script.delete;
18
19 if (typeof json.RelatedTopics[0] != "undefined") {
20 var div = document.createElement('div');
21 div.innerHTML = json.RelatedTopics[0].Result;
22 var a = div.getElementsByTagName("a")[0];
23 a.className = "extrares";
24 document.getElementById("duckduckres").appendChild(a);
25
26 div.innerHTML = json.RelatedTopics[1].Result;
27 var a = div.getElementsByTagName("a")[0];
28 a.className = "extrares";
29 document.getElementById("duckduckres").appendChild(a);
30
31 div.innerHTML = json.RelatedTopics[2].Result;
32 var a = div.getElementsByTagName("a")[0];
33 a.className = "extrares";
34 document.getElementById("duckduckres").appendChild(a);
35 }
36 }
37}
38
39function wikicallback(json) {
40 var page = json.query.pages[Object.keys(json.query.pages)[0]];
41 //console.log(json.query.pages);
42 var a = document.createElement('a');
43 a.href = "http://en.wikipedia.org/?curid="+page.pageid;
44 a.innerText = page.title + " - "+page.extract;
45 if (page.extract != "") {
46 a.innerText = page.title + " - "+page.extract;
47 } else {
48 a.innerText = page.title;
49 }
50 a.className = "mainres";
51 [].forEach.call(document.querySelectorAll('.mainres'),function(e){
52 e.parentNode.removeChild(e);
53 });
54 document.getElementById("wikires").appendChild(a);
55}
56
57var searchCallback = function(list) {
58 var form;
59 var searchbox;
60
61 var searchplace = list["searchplace"];
62 var formstring = "<form id=\"search-form\" method=\"get\" action=\"https://google.com/search?\">\
63 <input id=\"search-box\" name=\"q\" type=\"text\" placeholder=\"search\" autofocus autocomplete='on'/>\
64 </form>";
65
66 if (searchplace == "top") {
67 document.getElementById("search1").className += " active";
68 document.getElementById("search1").insertAdjacentHTML("beforeend", formstring);
69 } else if (searchplace == "middle") {
70 document.getElementById("search2").className += " active";
71 document.getElementById("search2").insertAdjacentHTML("beforeend", formstring);
72 } else {
73 document.getElementById("search3").className += " active";
74 document.getElementById("search3").insertAdjacentHTML("beforeend", formstring);
75 }
76
77 form = document.getElementById("search-form");
78 searchbox = document.getElementById("search-box");
79
80 searchbox.onkeyup = function(){
81 var val = searchbox.value;
82 if (val.length > 1) {
83 var script = document.createElement('script');
84 script.src = 'https://api.duckduckgo.com/?q='+searchbox.value+'&format=json&callback=jsonparse';
85 document.body.appendChild(script);
86 script.outerHTML = "";
87 script.delete;
88 }
89 }
90
91 if (list["search"] == "duckduckgo") {
92 form.setAttribute("action", "https://duckduckgo.com/?");
93 } else if (list["search"] == "yahoo") {
94 form.setAttribute("action", "https://search.yahoo.com/search?");
95 } else if (list["search"] == "bing") {
96 form.setAttribute("action", "https://www.bing.com/search?");
97 }
98
99 form.addEventListener("submit",
100 function(e){
101 var value = searchbox.value;
102 e.preventDefault();
103 var value = searchbox.value;
104 if (value.indexOf(".") >= 0) {
105 window.location = addhttp(value);;
106 } else {
107 form.submit();
108 }
109 }, false);
110}
111