-
Notifications
You must be signed in to change notification settings - Fork 10
/
background.js
147 lines (135 loc) · 3.37 KB
/
background.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
var reqUrl = 'http://localhost:8080/content';
var isClosePage = false;
var isCrawl = false;
var isBlockMedia = false;
var detailsCallback = function(details) {return { cancel: true};};
var filter = {
urls: ["*://*/*"],
types: ["font", "image", "media"]
};
var opt_extraInfoSpec = ["blocking"];
chrome.storage.sync.get(['isCrawl', 'isClose', 'reqUrl','blockMedia'], function (result) {
console.log("settting is get ", result);
if (result.isCrawl == true) {
isCrawl = true;
console.log("isCrawl", isCrawl);
if (result.reqUrl) {
reqUrl = result.reqUrl;
console.log("reqUrl", reqUrl);
}
}
if (result.isClose == true) {
isClosePage = true;
console.log("isClosePage", isClosePage);
}
if (result.blockMedia == true) {
isBlockMedia = true;
console.log("isBlockMedia", isBlockMedia);
chrome.webRequest.onBeforeRequest.addListener(
detailsCallback,
filter,
opt_extraInfoSpec);
}
});
chrome.webNavigation.onCompleted.addListener(function (details) {
if (details.frameId == 0 && details.parentFrameId === -1) {
if (isCrawl) {
chrome.tabs.sendMessage(details.tabId, "getContent", null, (result) => {
var param = { "content": result.content };
sendContent(reqUrl, param, details.tabId);
})
}
if (isClosePage) {
var tab = new Array(1);
tab[0] = details.tabId;
chrome.tabs.remove(tab);
}
}
})
/**
* 自动关闭设置
* @param {!boolean} checked
*/
function autoCloseSet(checked) {
if (checked == true) {
isClosePage = true
} else {
isClosePage = false;
}
}
/**
* 设置后台接受参数的接口
* @param {String} url
*/
function reqUrlSet(url) {
reqUrl = url;
}
/**
* 是否爬取页面
* @param {boolean} checked
*/
function isCrawlPage(checked) {
isCrawl = checked;
}
/**
* 是否不显示图片设置
* @param {boolean} checked
*/
function blockMediaSet(checked) {
isBlockMedia = checked;
console.log(chrome.webRequest.onBeforeRequest.removeListener)
if(!checked){
chrome.webRequest.onBeforeRequest.removeListener(detailsCallback);
}else{
chrome.webRequest.onBeforeRequest.addListener(
detailsCallback,
filter,
opt_extraInfoSpec);
}
}
/**
*
* @param {String} url request url
* @param {String} param page content
* @param {*} sender
*/
function sendContent(url, param, tabId) {
var xmlhttp = null;
if (window.XMLHttpRequest) {// code for all new browsers
xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {// code for IE5 and IE6
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp != null) {
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
let querySring = getQueryString(param);
xmlhttp.send(querySring);
} else {
console.log("Your browser does not support XMLHTTP.");
}
}
/**
*
* @param {any} paramObj
*/
function getQueryString(paramObj) {
if (typeof paramObj == 'object') {
let keys = Object.keys(paramObj);
let querySring = '';
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
if (i == 0) {
//encodeURIComponent(key) + '=' + encodeURIComponent(val)
querySring = encodeURIComponent(key) + '=' + encodeURIComponent(paramObj[key]);
} else {
querySring = '&' + encodeURIComponent(key) + '=' + encodeURIComponent(paramObj[key]);
}
}
return querySring;
} else if (typeof paramObj === 'string') {
return paramObj;
} else {
return null;
}
}