forked from henices/Chrome-proxy-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
136 lines (111 loc) · 3.63 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
function setProxyIcon() {
var icon = {
path: "images/off.png",
}
chrome.proxy.settings.get(
{'incognito': false},
function(config) {
if (config["value"]["mode"] == "system") {
chrome.browserAction.setIcon(icon);
} else if (config["value"]["mode"] == "direct") {
chrome.browserAction.setIcon(icon);
} else {
icon["path"] = "images/on.png";
chrome.browserAction.setIcon(icon);
}
}
);
}
function gotoPage(url) {
var fulurl = chrome.extension.getURL(url);
chrome.tabs.getAllInWindow(undefined, function(tabs) {
for (var i in tabs) {
tab = tabs[i];
if (tab.url == fulurl) {
chrome.tabs.update(tab.id, { selected: true });
return;
}
}
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.create({url: url,index: tab.index + 1});
});
});
}
function callbackFn(details) {
var proxySetting = JSON.parse(localStorage.proxySetting);
if (proxySetting){
var auth = proxySetting['auth'];
var username = auth['user'];
var password = auth['pass'];
}
if (proxySetting['auth']['user'] == '' &&
proxySetting['auth']['pass'] == '')
return {};
return { authCredentials: {username: username, password: password} };
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking'] );
var proxySetting = {
'pac_script_url' : {'http': '', 'https': '', 'file' : ''},
'pac_type' : 'file://',
'http_host' : '',
'http_port' : '',
'https_host' : '',
'https_port' : '',
'socks_host' : '',
'socks_port' : '',
'socks_type' : 'socks5',
'bypasslist' : '<local>,192.168.0.0/16,172.16.0.0/12,169.254.0.0/16,10.0.0.0/8',
'proxy_rule' : 'singleProxy',
'internal' : '',
'auth' : {'enable': '', 'user': '', 'pass': ''},
'rules_mode' : 'Whitelist'
}
var chinaList = ['*.cn']
localStorage.chinaList = JSON.stringify(chinaList);
function getBypass() {
var req = new XMLHttpRequest();
var url = "https://raw.github.com/henices/Chrome-proxy-helper/master/data/cn.bypasslist";
req.open('GET', url, true);
req.onreadystatechange = processResponse;
req.send(null);
function processResponse() {
if (req.readyState == 4 &&
req.status == 200) {
localStorage.chinaList = JSON.stringify(req.responseText.split(','));
} else
localStorage.chinaList = JSON.stringify(chinaList);
}
}
chrome.runtime.onInstalled.addListener(function(details){
if(details.reason == "install") {
localStorage.proxySetting = JSON.stringify(proxySetting);
gotoPage('options.html');
}
/*
else if(details.reason == "update") {
gotoPage('CHANGELOG');
}
*/
});
chrome.commands.onCommand.addListener(function(command) {
if (command == 'open-option')
gotoPage('options.html');
});
// sync extension settings from google cloud
//chrome.storage.sync.get('proxySetting', function(val) {
// if (typeof val.proxySetting !== "undefined")
// localStorage.proxySetting = val.proxySetting;
//});
chrome.proxy.onProxyError.addListener(function(details) {
console.log("fatal: ", details.fatal);
console.log("error: ", details.error);
console.log("details: ", details.details)
});
setProxyIcon();
// sync bypass list from github.com
//getBypass();
//setInterval(function() { getBypass(); }, interval);
//var interval = 1000 * 60 * 60;