forked from brookhong/Surfingkeys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchrome_bg.js
95 lines (92 loc) · 3.71 KB
/
chrome_bg.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
function dictFromArray(arry, val) {
var dict = {};
arry.forEach(function(h) {
dict[h] = val;
});
return dict;
}
function loadRawSettings(keys, cb, defaultSet) {
var rawSet = defaultSet || {};
chrome.storage.local.get(null, function(localSet) {
var localSavedAt = localSet.savedAt || 0;
chrome.storage.sync.get(null, function(syncSet) {
var syncSavedAt = syncSet.savedAt || 0;
if (localSavedAt > syncSavedAt) {
extendObject(rawSet, localSet);
_save(chrome.storage.sync, localSet, function() {
var subset = getSubSettings(rawSet, keys);
if (chrome.runtime.lastError) {
subset.error = "Settings sync may not work thoroughly because of: " + chrome.runtime.lastError.message;
}
cb(subset);
});
} else if (localSavedAt < syncSavedAt) {
extendObject(rawSet, syncSet);
cb(getSubSettings(rawSet, keys));
_save(chrome.storage.local, syncSet);
} else {
extendObject(rawSet, localSet);
cb(getSubSettings(rawSet, keys));
}
});
});
}
function _applyProxySettings(proxyConf) {
if (!proxyConf.proxyMode || proxyConf.proxyMode === 'clear') {
chrome.proxy.settings.clear({scope: 'regular'});
} else {
var autoproxy_pattern = proxyConf.autoproxy_hosts.filter(function(a) {
return a.indexOf('*') !== -1;
}).join('|');
var config = {
mode: (proxyConf.proxyMode === "always" || proxyConf.proxyMode === "byhost") ? "pac_script" : proxyConf.proxyMode,
pacScript: {
data: `var pacGlobal = {
hosts: ${JSON.stringify(dictFromArray(proxyConf.autoproxy_hosts, 1))},
autoproxy_pattern: '${autoproxy_pattern}',
proxyMode: '${proxyConf.proxyMode}',
proxy: '${proxyConf.proxy}'
};
function FindProxyForURL(url, host) {
var lastPos;
var gates = [pacGlobal.proxy, "DIRECT"];
if (pacGlobal.proxyMode === "always") {
gates = ["DIRECT", pacGlobal.proxy];
}
var pp = new RegExp(pacGlobal.autoproxy_pattern);
do {
if (pacGlobal.hosts.hasOwnProperty(host)) {
return gates[0];
}
if (pacGlobal.autoproxy_pattern.length && pp.test(host)) {
return gates[0];
}
lastPos = host.indexOf('.') + 1;
host = host.slice(lastPos);
} while (lastPos >= 1);
return gates[1];
}`
}
};
chrome.proxy.settings.set( {value: config, scope: 'regular'}, function() {
});
}
}
function request(url, onReady, headers, data, onException) {
headers = headers || {};
return new Promise(function(acc, rej) {
var xhr = new XMLHttpRequest();
var method = (data !== undefined) ? "POST" : "GET";
xhr.open(method, url);
for (var h in headers) {
xhr.setRequestHeader(h, headers[h]);
}
xhr.onload = function() {
acc(xhr.responseText);
};
xhr.onerror = rej.bind(null, xhr);
xhr.send(data);
}).then(onReady).catch(function(exp) {
onException && onException(exp);
});
}