forked from brookhong/Surfingkeys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchrome_bg.js
87 lines (86 loc) · 4.05 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
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.map(function(h) {
return h.filter(function(a) {
return a.indexOf('*') !== -1;
}).join('|');
});
var autoproxy_hosts = proxyConf.autoproxy_hosts.map(function(h) {
return dictFromArray(h.filter(function(a) {
return a.indexOf('*') === -1;
}), 1);
});
var config = {
mode: (["always", "byhost", "bypass"].indexOf(proxyConf.proxyMode) !== -1) ? "pac_script" : proxyConf.proxyMode,
pacScript: {
data: `var pacGlobal = {
hosts: ${JSON.stringify(autoproxy_hosts)},
autoproxy_pattern: ${JSON.stringify(autoproxy_pattern)},
proxyMode: '${proxyConf.proxyMode}',
proxy: ${JSON.stringify(proxyConf.proxy)}
};
function FindProxyForURL(url, host) {
var lastPos;
if (pacGlobal.proxyMode === "always") {
return pacGlobal.proxy[0];
} else if (pacGlobal.proxyMode === "bypass") {
var pp = new RegExp(pacGlobal.autoproxy_pattern[0]);
do {
if (pacGlobal.hosts[0].hasOwnProperty(host)
|| (pacGlobal.autoproxy_pattern[0].length && pp.test(host))) {
return "DIRECT";
}
lastPos = host.indexOf('.') + 1;
host = host.slice(lastPos);
} while (lastPos >= 1);
return pacGlobal.proxy[0];
} else {
for (var i = 0; i < pacGlobal.proxy.length; i++) {
var pp = new RegExp(pacGlobal.autoproxy_pattern[i]);
var ahost = host;
do {
if (pacGlobal.hosts[i].hasOwnProperty(ahost)
|| (pacGlobal.autoproxy_pattern[i].length && pp.test(ahost))) {
return pacGlobal.proxy[i];
}
lastPos = ahost.indexOf('.') + 1;
ahost = ahost.slice(lastPos);
} while (lastPos >= 1);
}
return "DIRECT";
}
}`
}
};
chrome.proxy.settings.set( {value: config, scope: 'regular'}, function() {
});
}
}