-
Notifications
You must be signed in to change notification settings - Fork 5
/
popup.js
99 lines (81 loc) · 2.32 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
function generateHtml(url) {
return `
<div class="item">
<span class="url">${url}</span>
<span class="del">✕</span>
</div>
`
}
var key = 'y_request_allow_urls';
var urls = chrome.runtime.sendMessage({action:'get', name: key}, function(urls){
var urlDom = $('#urls');
try{
urls = JSON.parse(urls);
}catch(e){
urls = null;
}
if(!urls || Object.keys(urls).length === 0){
urls = { '*': true};
chrome.runtime.sendMessage({action:'set', name: key, value: JSON.stringify(urls)})
}
for (var url in urls) {
urlDom.append(generateHtml(url));
}
$('#add .submit').bind('click', function () {
var val = $('#add input').val()
if (val) urls[val] = true;
chrome.runtime.sendMessage({
action:'set',
name: key,
value: JSON.stringify(urls)
})
urlDom.append(generateHtml(val))
})
$(document).ready(function () {
console.log("clear");
$('#content').hide();
var bg = chrome.extension.getBackgroundPage();
bg.getProxy(function (details) {
console.log(details);
if (details.hasOwnProperty('value')) {
var value = details.value;
var mode = value.mode;
var singleProxy = null;
if (value.hasOwnProperty('rules')){
var rules = value.rules;
if (rules.hasOwnProperty('singleProxy')) {
singleProxy = rules.singleProxy;
}
}
if (singleProxy != null){
$('#text').text("当前代理:" + singleProxy.host + ":" + singleProxy.port);
}
}
});
});
$("#clear").bind('click', function () {
console.log("clear");
var bg = chrome.extension.getBackgroundPage();
bg.clearProxy(); //是background中的一个方法
});
$("#get").bind('click', function () {
console.log("get");
var bg = chrome.extension.getBackgroundPage();
//是background中的一个方法
$('#content').show();
bg.getProxy(function (details) {
$('#content').text(JSON.stringify(details, null, '\t' ));
});
});
urlDom.on('click', '.del', function (event) {
var p = event.target.parentNode;
var url = $(p).find('.url').text();
delete urls[url]
chrome.runtime.sendMessage({
action:'set',
name: key,
value: JSON.stringify(urls)
})
p.parentNode.removeChild(p)
})
});