Skip to content

Commit

Permalink
0.9.38
Browse files Browse the repository at this point in the history
- Remove pdf viewer from Firefox version
- use markedjs 0.3.19
- use shadydom.min.js 1.1.2
- Multiple proxies support in byhost mode
- Remove mermaid from Firefox version
  • Loading branch information
brookhong committed Aug 27, 2018
1 parent 5068e73 commit 5b39696
Show file tree
Hide file tree
Showing 16 changed files with 416 additions and 249 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ To avoid manually editing PAC script and reloading/switching profile by clicking
* setProxyMode, to set proxy mode, there are five modes: direct, byhost, bypass, always, system and clear.

direct Chrome will connect to all sites directly.
byhost Chrome will only connect to sites added by `addProxySite` through proxy.
byhost Chrome will only connect to sites added by `addProxySite` through related proxy. You could add multiple pairs of `proxy` and `hosts`, for hosts matched with `hosts` `proxy` will be used.
bypass Chrome will connect to all sites through proxy, with specified hosts excluded.
always Chrome will connect to all sites through proxy.
system Use proxy configuration taken from the operating system.
Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ SwitchySharp是个很好的代理管理插件,但我的用法很简单,
* setProxyMode, 设置代理模式,有五种模式:direct, byhost, bypass, always, system 和 clear。

direct Chrome不使用代理访问任何网站。
byhost Chrome只在访问你通过`addProxySite`命令添加过的网站时使用代理。
byhost Chrome只在访问你通过`addProxySite`命令添加过的网站时使用代理。你可以添加多条映射,让不同的网站使用不同的代理。
bypass Chrome使用代理访问所有网站,除了通过`addProxySite`命令添加过的网站。
always Chrome使用代理访问所有网站。
system Chrome使用操作系统设置的代理。
Expand Down
71 changes: 45 additions & 26 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,16 @@ var ChromeService = (function() {
findHistory: [],
cmdHistory: [],
sessions: {},
autoproxy_hosts: [],
proxyMode: 'clear',
proxy: "DIRECT",
autoproxy_hosts: [],
proxy: []
};

loadRawSettings(keys, function(set) {
if (typeof(set.proxy) === "string") {
set.proxy = [set.proxy];
set.autoproxy_hosts = [set.autoproxy_hosts];
}
if (set.localPath) {
request(set.localPath, function(resp) {
set.snippets = resp;
Expand Down Expand Up @@ -1167,33 +1171,48 @@ var ChromeService = (function() {

function updateProxy(message, cb) {
loadSettings(['proxyMode', 'proxy', 'autoproxy_hosts'], function(proxyConf) {
if (message.proxy) {
proxyConf.proxy = message.proxy;
}
if (message.mode) {
if (message.operation === "deleteProxyPair") {
proxyConf.proxy.splice(message.number, 1);
proxyConf.autoproxy_hosts.splice(message.number, 1);
} else if (message.operation === "set") {
proxyConf.proxyMode = message.mode;
}
if (message.host) {
var hostsDict = dictFromArray(proxyConf.autoproxy_hosts, 1);
var hosts = message.host.split(/\s*[ ,\n]\s*/);
if (message.operation === "toggle") {
hosts.forEach(function(host) {
if (hostsDict.hasOwnProperty(host)) {
delete hostsDict[host];
} else {
proxyConf.proxy = message.proxy;
proxyConf.autoproxy_hosts = message.host;
} else {
if (message.mode) {
proxyConf.proxyMode = message.mode;
}
if (!message.number) {
message.number = 0;
}
if (message.proxy) {
proxyConf.proxy[message.number] = message.proxy;
if (proxyConf.autoproxy_hosts.length <= message.number) {
proxyConf.autoproxy_hosts[message.number] = [];
}
}
if (message.host) {
var hostsDict = dictFromArray(proxyConf.autoproxy_hosts[message.number], 1);
var hosts = message.host.split(/\s*[ ,\n]\s*/);
if (message.operation === "toggle") {
hosts.forEach(function(host) {
if (hostsDict.hasOwnProperty(host)) {
delete hostsDict[host];
} else {
hostsDict[host] = 1;
}
});
} else if (message.operation === "add") {
hosts.forEach(function(host) {
hostsDict[host] = 1;
}
});
} else if (message.operation === "add") {
hosts.forEach(function(host) {
hostsDict[host] = 1;
});
} else {
hosts.forEach(function(host) {
delete hostsDict[host];
});
});
} else {
hosts.forEach(function(host) {
delete hostsDict[host];
});
}
proxyConf.autoproxy_hosts[message.number] = Object.keys(hostsDict);
}
proxyConf.autoproxy_hosts = Object.keys(hostsDict);
}
var diffSet = {
autoproxy_hosts: proxyConf.autoproxy_hosts,
Expand Down
62 changes: 39 additions & 23 deletions chrome_bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,55 @@ 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 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(dictFromArray(proxyConf.autoproxy_hosts, 1))},
autoproxy_pattern: '${autoproxy_pattern}',
hosts: ${JSON.stringify(autoproxy_hosts)},
autoproxy_pattern: ${JSON.stringify(autoproxy_pattern)},
proxyMode: '${proxyConf.proxyMode}',
proxy: '${proxyConf.proxy}'
proxy: ${JSON.stringify(proxyConf.proxy)}
};
function FindProxyForURL(url, host) {
var lastPos;
if (pacGlobal.proxyMode === "always") {
return pacGlobal.proxy;
}
var gates = [pacGlobal.proxy, "DIRECT"];
if (pacGlobal.proxyMode === "bypass") {
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];
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);
}
lastPos = host.indexOf('.') + 1;
host = host.slice(lastPos);
} while (lastPos >= 1);
return gates[1];
return "DIRECT";
}
}`
}
};
Expand Down
15 changes: 15 additions & 0 deletions content_scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,18 @@ HTMLElement.prototype.removeAttributes = function () {
this.removeAttribute(this.attributes[0].name);
}
};
NodeList.prototype.remove = function() {
this.forEach(function(node) {
node.remove();
});
};
NodeList.prototype.show = function() {
this.forEach(function(node) {
node.show();
});
};
NodeList.prototype.hide = function() {
this.forEach(function(node) {
node.hide();
});
};
15 changes: 11 additions & 4 deletions firefox_bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ function _applyProxySettings(proxyConf) {
if (!proxyConf.proxyMode || proxyConf.proxyMode === 'clear') {
browser.proxy.unregister();
} else {
var autoproxy_pattern = proxyConf.autoproxy_hosts.filter(function(a) {
return a.indexOf('*') !== -1;
}).join('|');
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);
});
browser.proxy.register("firefox_pac.js");
var pacv = {
hosts: dictFromArray(proxyConf.autoproxy_hosts, 1),
hosts: autoproxy_hosts,
autoproxy_pattern: autoproxy_pattern,
proxyMode: proxyConf.proxyMode,
proxy: proxyConf.proxy
Expand Down
65 changes: 38 additions & 27 deletions firefox_pac.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,48 @@ var pacGlobal = {};

browser.runtime.onMessage.addListener((message) => {
pacGlobal = message;
if (pacGlobal.proxy.toLowerCase().indexOf("socks") === 0) {
var p = pacGlobal.proxy.split(" ");
var h = p[1].split(":");
pacGlobal.proxy = [{
type: p[0],
host: h[0],
port: h[1],
proxyDNS: true
}];
}
pacGlobal.proxy.forEach(function(proxy, i) {
if (proxy.toLowerCase().indexOf("socks") === 0) {
var p = proxy.split(" ");
var h = p[1].split(":");
pacGlobal.proxy[i] = [{
type: p[0],
host: h[0],
port: h[1],
proxyDNS: true
}];
}
});
});

function FindProxyForURL(url, host) {
var lastPos;
if (pacGlobal.proxyMode === "always") {
return pacGlobal.proxy;
}
var gates = [pacGlobal.proxy, "DIRECT"];
if (pacGlobal.proxyMode === "bypass") {
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];
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);
}
lastPos = host.indexOf('.') + 1;
host = host.slice(lastPos);
} while (lastPos >= 1);
return gates[1];
return "DIRECT";
}
}
35 changes: 26 additions & 9 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ gulp.task('clean', function () {

gulp.task('copy-html-files', function() {
if (buildTarget === "Firefox") {
return gulp.src(['pages/*.html'], {base: "."})
return gulp.src(['pages/*.html', '!pages/pdf_viewer.html'], {base: "."})
.pipe(replace(/\s*<script src="ga.js"><\/script>\n\s*<script async src='https:\/\/www.google-analytics.com\/analytics.js'><\/script>/, ''))
.pipe(gulp.dest(`dist/${buildTarget}-extension`));
} else {
Expand All @@ -48,14 +48,19 @@ gulp.task('copy-html-files', function() {
});

gulp.task('copy-non-js-files', function() {
return gulp.src(['icons/**',
var nonJsFiles = ['icons/**',
'content_scripts/**',
'!content_scripts/**/*.js',
'pages/**',
'libs/marked.min.js',
'libs/mermaid.min.js',
'!pages/**/*.html',
'!pages/**/*.js'], {base: "."})
'!pages/**/*.js'];
if (buildTarget === "Firefox") {
nonJsFiles.push('!pages/pdf/**');
} else {
nonJsFiles.push('libs/mermaid.min.js');
}
return gulp.src(nonJsFiles, {base: "."})
.pipe(gulp.dest(`dist/${buildTarget}-extension`));
});

Expand All @@ -75,10 +80,12 @@ gulp.task('copy-es-files', function() {

gulp.task('copy-js-files', gulp.series('copy-es-files', function() {
var libs = [
'libs/ace/*.js',
'pages/pdf/*.js',
'libs/webfontloader.js'
'libs/ace/*.js'
];
if (buildTarget === "Chrome") {
libs.push("pages/pdf/*.js");
libs.push("libs/webfontloader.js");
}
return gulp.src(libs, {base: "."})
.pipe(gulpif(options.env === 'development', sourcemaps.init()))
.pipe(gulpif(!options.nominify, gp_uglify().on('error', gulpUtil.log)))
Expand Down Expand Up @@ -110,7 +117,7 @@ gulp.task('build_background', function() {
.pipe(gulp.dest(`dist/${buildTarget}-extension`));
});

gulp.task('build_common_content_min', function() {
gulp.task('build_common_content_min_without_lib', function() {
var common_content = [
"libs/trie.js",
"content_scripts/keyboardUtils.js",
Expand All @@ -123,7 +130,6 @@ gulp.task('build_common_content_min', function() {
"content_scripts/clipboard.js",
];
if (buildTarget === "Firefox") {
common_content.push("libs/shadydom.min.js");
common_content.push("content_scripts/firefox_fg.js");
} else {
common_content.push("content_scripts/chrome_fg.js");
Expand All @@ -137,6 +143,17 @@ gulp.task('build_common_content_min', function() {
.pipe(gulp.dest(`dist/${buildTarget}-extension/content_scripts`));
});

gulp.task('build_common_content_min', gulp.series('build_common_content_min_without_lib', function(cb) {
if (buildTarget === "Firefox") {
return gulp.src([`dist/${buildTarget}-extension/content_scripts/common_content.min.js`,"libs/shadydom.min.js"])
.pipe(gp_concat('common_content.min.js'))
.pipe(gulp.dest(`dist/${buildTarget}-extension/content_scripts`));
} else {
return gulp.src([`dist/${buildTarget}-extension/content_scripts/common_content.min.js`])
.pipe(gulp.dest(`dist/${buildTarget}-extension/content_scripts`));
}
}));

gulp.task('build_manifest', gulp.series('copy-non-js-files', 'copy-html-files', function(cb) {
var json = JSON.parse(fs.readFileSync('manifest.json'));
if (buildTarget === "Firefox") {
Expand Down
4 changes: 2 additions & 2 deletions libs/marked.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 5b39696

Please sign in to comment.