-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
104 lines (84 loc) · 2.85 KB
/
popup.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
/**
* "OtakuTomo"
* Copyright © 2013 Brianna Shade
*
* This program is licensed under the "MIT License"
* Please see the file COPYING in the source distribution of this software for license terms.
*
* popup.js
* TODO details on this class
*/
function displayResults(body, results){
var htmlHead = "<head><title>OtakuTomo Search Results</title></head>";
var htmlBody = body + results + "</body>";
var htmlCode = "<html>" + htmlHead + htmlBody + "</html>";
var url = "data:text/html," + encodeURIComponent(htmlCode);
chrome.tabs.create({url: url});
}
function processResults(resp){
var nodes = resp.getElementsByTagName("entry");
var results = "";
if(nodes == null){
displayResults("Element \"entry\" does not exist");
}
else{
for(var i = 0; i < nodes.length; i++){
var title = nodes[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
var imgurl = nodes[i].getElementsByTagName("image")[0].childNodes[0].nodeValue;
var id = nodes[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
results += "\n<div><b>" + title + "</b></div>";
results += '\n<a href=\'http://myanimelist.net/anime/' + id + '\'><img src=\'' + imgurl + '\'/></a>';
results += "<br><br>";
}
return results;
}
}
function redirect(query){
var url = "http://myanimelist.net/anime.php?q=" + query;
chrome.extension.sendRequest({redirect: url});
}
function getResults(query){
var searchURL = "http://otakutomo:[email protected]/api/anime/search.xml?q=" + query;
var body = "<body>";
var pgtxt = "";
var request = new XMLHttpRequest();
if (request == null){
displayResults(body, "Unable to create request");
}
else{
request.open("GET", searchURL, true);
request.onreadystatechange = function(){
if(request.readyState == 4){
if(request.responseXML == null && request.responseText != ""){
//body = "<body onLoad=\"setTimeout('delayer()', 1000)\">";
pgtxt = "Sorry, response data was not correctly formatted. Redirecting...";
setTimeout(redirect(query), 5000);
}
else{
switch(request.status){
case 200: pgtxt = processResults(request.responseXML); break;
case 204: pgtxt = "No results found for: " + query; break;
case 0: pgtxt = "Sorry, request status came back as zero :("; break;
case 401: pgtxt = "Not correctly logged in"; break;
default: pgtxt = "Unknown request status: " + request.status;
}
}
displayResults(body, pgtxt);
}
};
request.overrideMimeType("text/xml");
request.send();
}
}
function submitHandler(event){
var query = document.getElementById("textbox").value;
if(query == "Enter anime title" || query == "");
else{
getResults(query);
event.preventDefault();
}
}
document.addEventListener("DOMContentLoaded", function () {
document.getElementById("form").onsubmit = submitHandler;
});