-
Notifications
You must be signed in to change notification settings - Fork 0
/
series_fav.js
68 lines (56 loc) · 2.24 KB
/
series_fav.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
(function() {
console.log('series_fav passed the manifest test');
// \d+ matches the series code and the (\?.+)? matches the arugments
var seriesUrlPattern = /http:\/\/www.autohome.com.cn\/\d+\/(\?.+)?/;
console.log(document.URL);
// fliter the error pages with a program way, because the matches' way of google's manifest's content_scripts is not usable.
if ( document.URL.match(seriesUrlPattern) === null ) {
return;
}
console.log('Ok, url passed the test');
var seriesScript = document.createElement('script');
seriesScript.setAttribute('src', chrome.extension.getURL('series_fav_clicked.js'));
seriesScript.setAttribute('charset', 'UTF-8');
document.getElementsByTagName('head')[0].appendChild(seriesScript);
var favText = '收藏';
// get the name and code
var nameNode = document.getElementsByClassName("mini_right")[0].getElementsByClassName("reda")[0];
if (nameNode === undefined) {
// not login
console.log('not login');
addTheFavFeature();
return;
}
var name = nameNode.textContent;
console.log("name: " + name);
var href = document.getElementsByClassName("carname")[0].getElementsByTagName("a")[0].getAttribute("href");
var code = href.substring(0, href.length - 1);
console.log("code: " + code);
// decide if it was faved.
var xhr = new XMLHttpRequest();
var url = 'http://42.121.57.45:10001/is_fav?name=' + encodeURIComponent(name) + '&type=series&code=' + code;
xhr.onreadystatechange = handler;
xhr.open("GET", url, true);
xhr.send(null);
function handler() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
console.log(xhr.responseText);
var result = JSON.parse(xhr.responseText);
if (result.result) {
favText = '已收藏';
}
addTheFavFeature();
}
else {
console.log("Error with Ajax Call!");
}
}
}
// add the feature after get the infos.
function addTheFavFeature() {
var element = document.getElementsByClassName("carname");
element[0].outerHTML = element[0].outerHTML + '<div class="series_fav" id="series_fav"> \
<a id="series_fav_href" href="javascript:void(0)" target="_blank" onclick="series_fav_clicked();">' + favText + '</a> </div>';
}
})();