-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.html
97 lines (83 loc) · 2.71 KB
/
background.html
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
<script type="text/javascript">
var dbUrl = 'http://wedata.net/databases/LDRFullFeed/items.json';
function getSiteInfo(data, callback){
if(localStorage.length != 0){
callback({
task: "siteinfo",
result: true,
content: JSON.parse(localStorage.getItem("SITEINFO"))
});
}
try{
var req = new XMLHttpRequest();
req.open('GET', dbUrl, true);
req.onload = function(res) {
try {
if (req.status != 200){
callback(req.onerror(req.statusText));
return;
}
var siteinfo_cache = eval(req.responseText)
.map(function(i){ return i.data })
.sort(function(lhs, rhs){ return rhs.priority - lhs.priority });
localStorage.setItem("SITEINFO", JSON.stringify(siteinfo_cache));
callback({task: "siteinfo", result: true, content: siteinfo_cache});
} catch(e) {
callback({
task: "siteinfo", result: false, reason: 'maybe invalid siteinfo'
})
}
};
req.onerror =
function(e) { return {task: "siteinfo", result: false, reason: e} };
req.send(null);
} catch(e) { callback({task: "siteinfo", result: false, reason: e}) }
}
window.onload = function() {
var ports = [];
chrome.extension.onConnect.addListener(function(port) {
chrome.pageAction.show(port.tab.id);
ports.push(port);
port.onMessage.addListener(function(data) {
switch(data.task) {
case "open":
chrome.tabs.create({url: data.url, selected: false})
break;
case "siteinfo":
getSiteInfo(data, function(result){ port.postMessage(result) });
break;
case "expand":
data.url = 'http://expand-ext0.appspot.com/?0=' + encodeURIComponent(data.url);
case "fullfeed":
try{
var req = new XMLHttpRequest();
req.open('GET', data.url, true);
req.onload = function(res){
if(req.status != 200){ return req.onerror(req.statusText) }
data.result = true;
data.content = req.responseText;
port.postMessage(data);
};
req.onerror = function(e){
data.result = false;
data.reason = e;
port.postMessage(data);
};
req.send(null);
} catch(e) {
data.result = false;
data.reason = e;
port.postMessage(data);
}
break;
}
});
});
chrome.pageAction.onClicked.addListener(function() {
localStorage.clear();
ports.forEach(function(port) {
port.postMessage({result: true, task: 'update-siteinfo'})
})
});
}
</script>