-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontents.js
229 lines (200 loc) · 9.4 KB
/
contents.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* Inform the background page that this tab should have a page-action */
chrome.runtime.sendMessage({
from: "content",
subject: "showPageAction"
});
/* Listen for update message from the popup, with simplified version passing only two attributes */
chrome.runtime.onMessage.addListener(function(msg, sender, response) {
/* First, validate the message's structure */
if (msg.from && (msg.from === "background")
&& msg.subject && (msg.subject === "updateContent")) {
/* Collect the necessary data
* (For your specific requirements `document.querySelectorAll(...)`
* should be equivalent to jquery's `$(...)`)*/
//alert('message subject in contents '+msg.subject);
var myNode = document.getElementById('dataset-items');
while (myNode.firstChild) {
myNode.removeChild(myNode.firstChild);
}
var items = msg.data;
//alert('here get items size '+items.length);
for (var i = 0; i <items.length; i++) {
var item = items[i];
var liElement = document.createElement('li');
liElement.setAttribute("class", "dataset-item");
var link = document.createElement('a');
link.setAttribute("href", item.url);
var aElemTN = document.createTextNode(item.itemText);
link.appendChild(aElemTN);
var divElement1 = document.createElement('div');
var strongElement = document.createElement('strong');
strongElement.appendChild(link);
divElement1.appendChild(strongElement);
liElement.appendChild(divElement1);
if (item.itemDesc != null && item.itemDesc != '') {
var divElement2 = document.createElement('div');
var textElementDesc = document.createTextNode(LZString.decompressFromBase64(item.itemDesc));
divElement2.appendChild(textElementDesc);
liElement.appendChild(divElement2);
}
var twitterText = "<a href\""+item.url+"\">"+item.itemText+"</a>";
var link2 = document.createElement('a');
link2.setAttribute("href", "#");
var facebookImage = document.createElement('img');
facebookImage.setAttribute("src", "img/facebook.png");
facebookImage.setAttribute("width", "36");
facebookImage.setAttribute("height", "36");
facebookImage.setAttribute("alt", twitterText);
link2.appendChild(facebookImage);
liElement.appendChild(link2);
link2.addEventListener('click', function(event){openLinkHandler("http://shrouded-dawn-1777.herokuapp.com/facebook.html?codecText="+encodeURIComponent(event.target.getAttribute("alt"))+"&lang="+chrome.i18n.getMessage("language"))});
var link3 = document.createElement('a');
link3.setAttribute("href", "#");
var twitterImage = document.createElement('img');
twitterImage.setAttribute("src", "img/twitter.png");
twitterImage.setAttribute("width", "36");
twitterImage.setAttribute("height", "36");
twitterImage.setAttribute("alt", twitterText);
link3.appendChild(twitterImage);
liElement.appendChild(link3);
link3.addEventListener('click', function(event){openLinkHandler("http://shrouded-dawn-1777.herokuapp.com/facebook.html?codecText="+encodeURIComponent(event.target.getAttribute("alt"))+"&lang="+chrome.i18n.getMessage("language"))});
//Check extras/spatial
if (item.geocode != null && item.geocode != "QAA=") {
var geocode = item.geocode;
var link4 = document.createElement('a');
link4.setAttribute("href", "#");
var mapImage = document.createElement('img');
mapImage.setAttribute("src", "img/google-map.png");
mapImage.setAttribute("width", "36");
mapImage.setAttribute("height", "36");
mapImage.setAttribute("alt", geocode);
link4.appendChild(mapImage);
liElement.appendChild(link4);
link4.addEventListener('click', function(event){openLinkHandler("http://shrouded-dawn-1777.herokuapp.com/googlemap.html?geocode="+event.target.getAttribute("alt"))});
}
document.getElementById('dataset-items').appendChild(liElement);
link.addEventListener('click', clickHandler);
};
response({backgroundMsg: 'success-content'});
setLabels();
return true;
}
});
document.addEventListener('DOMContentLoaded', function () {
init();
});
/**
* Initial loading during tab creation
*/
function init() {
setLabels();
var myterms = getTerms();
if (myterms != null) {
processTerms(myterms.asArray());
} else {
//TODO: add empty message
}
}
function setLabels() {
document.title=chrome.i18n.getMessage("search_result");
var result_summary = chrome.i18n.getMessage("with_result1");
var result_info = chrome.i18n.getMessage("with_result2");
var searchterm = localStorage['searchterm'];
result_summary = result_summary.replace("{term}", (searchterm != undefined)?searchterm:"");
document.getElementById('result_summary').innerHTML=result_summary;
var myterms = getTerms();
if (myterms != null) {
result_info = result_info.replace("{count}", myterms.asArray().length);
document.getElementById('result_info').innerHTML=result_info;
} else {
result_info = result_info.replace("{count}", 0);
document.getElementById('result_info').innerHTML=result_info;
}
}
function processTerms(terms) {
var myNode = document.getElementById('dataset-items');
while (myNode.firstChild) {
myNode.removeChild(myNode.firstChild);
}
for (var i = 0; i <terms.length; i++) {
var term = terms[i];
var liElement = document.createElement('li');
liElement.setAttribute("class", "dataset-item");
var link = document.createElement('a');
link.setAttribute("href", term.url);
var aElemTN = document.createTextNode(term.text);
link.appendChild(aElemTN);
var divElement1 = document.createElement('div');
var strongElement = document.createElement('strong');
strongElement.appendChild(link);
divElement1.appendChild(strongElement);
liElement.appendChild(divElement1);
if (term.desc != null && term.desc != '') {
var divElement2 = document.createElement('div');
//alert('term desc '+term.desc);
var textElementDesc = document.createTextNode(LZString.decompressFromBase64(term.desc));
divElement2.appendChild(textElementDesc);
liElement.appendChild(divElement2);
}
var twitterText = "<a href\""+term.url+"\">"+term.text+"</a>";
var link2 = document.createElement('a');
link2.setAttribute("href", "#");
var facebookImage = document.createElement('img');
facebookImage.setAttribute("src", "img/facebook.png");
facebookImage.setAttribute("width", "36");
facebookImage.setAttribute("height", "36");
facebookImage.setAttribute("alt", twitterText);
link2.appendChild(facebookImage);
liElement.appendChild(link2);
link2.addEventListener('click', function(event){openLinkHandler("http://shrouded-dawn-1777.herokuapp.com/facebook.html?codecText="+encodeURIComponent(event.target.getAttribute("alt"))+"&lang="+chrome.i18n.getMessage("language"))});
var link3 = document.createElement('a');
link3.setAttribute("href", "#");
var twitterImage = document.createElement('img');
twitterImage.setAttribute("src", "img/twitter.png");
twitterImage.setAttribute("width", "36");
twitterImage.setAttribute("height", "36");
twitterImage.setAttribute("alt", twitterText);
link3.appendChild(twitterImage);
liElement.appendChild(link3);
link3.addEventListener('click', function(event){openLinkHandler("http://shrouded-dawn-1777.herokuapp.com/facebook.html?codecText="+encodeURIComponent(event.target.getAttribute("alt"))+"&lang="+chrome.i18n.getMessage("language"))});
//Check extras/spatial
if (term.geocode != null && term.geocode != "QAA=") {
var geocode = term.geocode;
var link4 = document.createElement('a');
link4.setAttribute("href", "#");
var mapImage = document.createElement('img');
mapImage.setAttribute("src", "img/google-map.png");
mapImage.setAttribute("width", "36");
mapImage.setAttribute("height", "36");
mapImage.setAttribute("alt", geocode);
link4.appendChild(mapImage);
liElement.appendChild(link4);
link4.addEventListener('click', function(event){openLinkHandler("http://shrouded-dawn-1777.herokuapp.com/googlemap.html?geocode="+event.target.getAttribute("alt"))});
}
document.getElementById('dataset-items').appendChild(liElement);
link.addEventListener('click', clickHandler);
}
}
function clickHandler(event) {
var link = event.target;
if (link != null) {
var myterms = getTerms();
if (myterms != null) {
var term = myterms.findById(link);
if (term != null) {
term.visited = true;
myterms.sync();
chrome.runtime.sendMessage(
{from: "contents", subject: "visitedLink"},
function(response) {
response({result: 'success'});
}
);
}
}
}
}
function openLinkHandler(url) {
var child = window.open(url, "Media", 'left=20,top=20,width=500,height=500,toolbar=1,resizable=0');
return false;
}