Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
miniers committed Jun 13, 2017
1 parent 6c2b6e8 commit a3f68f2
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 118 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### v1.2更新内容:
1. 优化代码结构,修复包含iframe页面下载重复问题
2. 图片下载

### v1.1.2:
1. 增加自动更新

### v1.1:
1. 增加快捷键来切换默认rpc服务 (option+shift+[123456789]
2. 增加快捷键展示当前默认rpc服务(option+shift+`)
3. 设置界面可以设置默认rpc服务
4. 增加快捷键临时禁制拦截 (按着cmd点击链接)

### v1.0:
1. 自动拦截指定后缀文件的下载事件并通过aria2c来下载该文件
2. 可配置多个aria2c服务地址
3. 右键链接后可选择指定aria2c服务进行下载)
39 changes: 24 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# # safari2aria
# safari2aria

用 aria2c 来替代你的 safari 默认下载

请搭配mac下的safari进行食用
### v1.1.2更新内容:
增加自动更新
### v1.1更新内容:
1. 增加快捷键来切换默认rpc服务 (option+shift+[123456789]
2. 增加快捷键展示当前默认rpc服务(option+shift+`)
3. 设置界面可以设置默认rpc服务
4. 增加快捷键临时禁制拦截 (按着cmd点击链接)

### 暂时实现了一些基础功能:
1. 自动拦截指定后缀文件的下载事件并通过aria2c来下载该文件
2. 可配置多个aria2c地址
3. 右键链接后可选择指定aria2c服务进行下载

### 已有功能:
1. 拦截指定后缀文件下载
2. 多aria2c服务支持
3. 右键菜单指定aria2c服务

### 快捷键:
1. 切换默认rpc服务 (option+shift+[123456789]
2. 展示当前默认rpc服务(option+shift+`)
3. 临时禁用或启用拦截(按着cmd点击链接)

[下载链接](https://github.com/miniers/safari2aria/releases)

Expand All @@ -25,8 +25,8 @@
![image](https://user-images.githubusercontent.com/2039910/27039821-b4518ce6-4fc1-11e7-8dc2-a9b9c1621ae0.png)


### 已知问题
1. 如果aria2c端没有配置ssl证书,则https网站的下载请求无法发送至aria2c
### 注意事项
如果aria2c端没有配置ssl证书,则https网站的下载请求无法发送至aria2c

该问题为safari的安全策略,暂时没有思路修复

Expand All @@ -49,3 +49,12 @@ rpc-secure=true
rpc-certificate=01 01 01 01 01 01 01
```

## 友情福利

鉴于有些用户没有申请域名或者证书,现提供一套自用ssl证书用以搭建本地aria2c https支持
域名: aria2.zc.ci
解析: 127.0.0.1
证书下载地址:[aria2.zc.ci.zip](https://github.com/miniers/safari2aria/files/1070942/aria2.zc.ci.zip)

请参照注意事项在aria2c的配置文件中添加证书

6 changes: 3 additions & 3 deletions safari2aria.safariextension/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<key>CFBundleInfoDictionaryVersion</key>
<string>1.0</string>
<key>CFBundleShortVersionString</key>
<string>1.1.2</string>
<string>1.2</string>
<key>CFBundleVersion</key>
<string>1.1.2</string>
<string>1.2</string>
<key>Chrome</key>
<dict>
<key>Database Quota</key>
Expand Down Expand Up @@ -56,7 +56,7 @@
<key>Update From Gallery</key>
<true/>
<key>Update Manifest URL</key>
<string>https://gist.githubusercontent.com/miniers/c62891753a2b9c05a154ea5d53efebee/raw/cdbfd3126f0b1120517039e6b15709b6fdd7b07d/update.plist</string>
<string>http://miniers.github.io/safari2aria/update.plist?t=1.1.3</string>
<key>Website</key>
<string>https://github.com/miniers/safari2aria</string>
</dict>
Expand Down
117 changes: 26 additions & 91 deletions safari2aria.safariextension/js/endscript.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,16 @@
var ARIA2 = (function() {
var jsonrpc_version = '2.0';

function get_auth(url) {
return url.match(/^(?:(?![^:@]+:[^:@\/]*@)[^:\/?#.]+:)?(?:\/\/)?(?:([^:@]*(?::[^:@]*)?)?@)?/)[1];
};

function request(jsonrpc_path, method, params,cb) {
var xhr = new XMLHttpRequest();
var auth = get_auth(jsonrpc_path);
jsonrpc_path = jsonrpc_path.replace(/^((?![^:@]+:[^:@\/]*@)[^:\/?#.]+:)?(\/\/)?(?:(?:[^:@]*(?::[^:@]*)?)?@)?(.*)/, '$1$2$3'); // auth string not allowed in url for firefox

var request_obj = {
jsonrpc: jsonrpc_version,
method: method,
id: (new Date()).getTime().toString(),
};
if (params) request_obj['params'] = params;
if (auth && auth.indexOf('token:') == 0) params.unshift(auth);

xhr.open("POST", jsonrpc_path+"?tm="+(new Date()).getTime().toString(), true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
if (auth && auth.indexOf('token:') != 0) {
xhr.setRequestHeader("Authorization", "Basic "+btoa(auth));
}
xhr.send(JSON.stringify(request_obj));
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
cb&&cb()
} else {
console.log('failed');
cb&cb("err");
}
}
}
};

return function(jsonrpc_path) {
this.jsonrpc_path = jsonrpc_path;
this.addUri = function (uri, options,cb) {
request(this.jsonrpc_path, 'aria2.addUri', [[uri, ], options],cb);
};
return this;
}
})();


safe_title = function safe_title(title) {
return title.replace(/[\\\|\:\*\"\?\<\>]/g, "_");
};
function linkForTarget (e) {
return "BODY" === e.tagName ? null : e.href ? e.href : e.parentNode ? linkForTarget(e.parentNode) : void 0
var result = null;
if ("BODY" === e.tagName) {
result = null
} else if (e.tagName === "IMG" && e.src) {
result = e.src
} else if (e.href) {
result = e.href
} else if (e.parentNode) {
result = linkForTarget(e.parentNode)
}
return result
}

function linksFromContainer (e) {
Expand Down Expand Up @@ -88,33 +48,16 @@ function selectedLinks () {
}
return null
}
function sendToAria2 (e) {
var aria = ARIA2(e[0]);
if (e[1]) {
aria.addUri(e[1], {
header: 'Cookie: ' + document.cookie
},function (err) {
if(err){
miniToastr.error('添加到aria2失败')
}else{
miniToastr.success('添加到aria2成功')
}
});
}
}

function handleMessage (e) {
if ("sendToAria2" === e.name && e.message[2] === document.location.href){
sendToAria2(e.message);
}
if(e.name === "changeRpc"){
miniToastr.success('成功切换默认下载服务至'+e.message);
if (e.name === "changeRpc") {
miniToastr.success('成功切换默认下载服务至' + e.message);
}
if(e.name === "currentRpc"){
miniToastr.success('当前下载服务为'+e.message);
if (e.name === "currentRpc") {
miniToastr.success('当前下载服务为' + e.message);
}
if(e.name === "showMassage"){
miniToastr[e.message.action](e.message.text);
if (e.name === "showMassage") {
miniToastr[e.message.action || "success"](e.message.text);
}
}

Expand All @@ -129,38 +72,30 @@ function handleContextMenu (e) {


//handle command key
document.onkeydown=function(event){
var unicode=event.charCode ? event.charCode : event.keyCode;
if(unicode === 91) //chrome:COMMAND
{
isCommandKeyPressed = true;
}
keyPressed[unicode]=true;
document.onkeydown = function (event) {
var unicode = event.charCode ? event.charCode : event.keyCode;
keyPressed[unicode] = true;
sendKeyPressEvent()
};

document.onkeyup=function (event){
var unicode=event.charCode ? event.charCode : event.keyCode;
if(unicode === 91){
isCommandKeyPressed = false;
}
keyPressed[unicode]=false;
document.onkeyup = function (event) {
var unicode = event.charCode ? event.charCode : event.keyCode;
keyPressed[unicode] = false;
sendKeyPressEvent()
};

function sendKeyPressEvent () {
safari.self.tab.dispatchMessage("keyPress", {
keyPressed:keyPressed
keyPressed: keyPressed
});
}

var isCommandKeyPressed,isShiftPressd; //cmd键是否被按压
var keyPressed={};
var rpcList=[];
var keyPressed = {};
miniToastr.init({
appendTarget: document.body,
timeout: 5000
});
sendKeyPressEvent();
document.addEventListener("contextmenu", handleContextMenu, !1);

safari.self.addEventListener("message", handleMessage, !1);
92 changes: 83 additions & 9 deletions safari2aria.safariextension/js/global.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,71 @@
var ARIA2 = (function () {
var jsonrpc_version = '2.0';

function get_auth (url) {
return url.match(/^(?:(?![^:@]+:[^:@\/]*@)[^:\/?#.]+:)?(?:\/\/)?(?:([^:@]*(?::[^:@]*)?)?@)?/)[1];
}

function request (jsonrpc_path, method, params, cb) {
var xhr = new XMLHttpRequest();
var auth = get_auth(jsonrpc_path);
jsonrpc_path = jsonrpc_path.replace(/^((?![^:@]+:[^:@\/]*@)[^:\/?#.]+:)?(\/\/)?(?:(?:[^:@]*(?::[^:@]*)?)?@)?(.*)/, '$1$2$3'); // auth string not allowed in url for firefox

var request_obj = {
jsonrpc: jsonrpc_version,
method: method,
id: (new Date()).getTime().toString(),
};
if (params) request_obj['params'] = params;
if (auth && auth.indexOf('token:') == 0) params.unshift(auth);

xhr.open("POST", jsonrpc_path + "?tm=" + (new Date()).getTime().toString(), true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
if (auth && auth.indexOf('token:') != 0) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(auth));
}
xhr.send(JSON.stringify(request_obj));
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
cb && cb()
} else {
console.log('failed');
cb & cb("err");
}
}
}
}

return function (jsonrpc_path) {
this.jsonrpc_path = jsonrpc_path;
this.addUri = function (uri, options, cb) {
request(this.jsonrpc_path, 'aria2.addUri', [[uri,], options], cb);
};
return this;
}
})();

function sendToAria2 (e) {
var aria = ARIA2(e[0].url);
if (e[1]) {
aria.addUri(e[1], {
header: 'Cookie: ' + document.cookie
}, function (err) {
if (err) {
showToast("showMassage", {
action:'error',
text:['添加到',e[0].name,'失败'].join('')
});
} else {
showToast("showMassage", {
action:'success',
text:['添加到',e[0].name,'成功'].join('')
});
}
});
}
}

function openPreferences (e) {
"showOptions" === e.key && (safari.application.activeBrowserWindow.openTab().url = safari.extension.baseURI + "options.html", optionsEvent = e)
}
Expand All @@ -23,8 +91,11 @@ function messageHandler (e) {
keyPressAction(e.message)
}
}
function showToast (type,msg) {
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage(type, msg);
}
function keyPressAction (keys) {
keyPressed = keys.keyPressed || {};
var keyPressed = keys.keyPressed || {};
isCommandPressed = keyPressed[91];
isShiftPressd = keyPressed[16];
isOptionPressd = keyPressed[18];
Expand All @@ -36,22 +107,25 @@ function keyPressAction (keys) {
name:'updateSafari2Aria',
message:config
});
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("changeRpc", rpcList[config.defaultRpcIndex].name);
showToast("changeRpc", rpcList[config.defaultRpcIndex].name);
break;
}
}
if(keyPressed[192]){
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("currentRpc", rpcList[config.defaultRpcIndex].name);
showToast("currentRpc", rpcList[config.defaultRpcIndex].name);
}
}

}
function handleCommand (e) {
var n = [e.command].concat(e.userInfo);
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("sendToAria2", n)
var index = e.command.split('.')[1];
var rpc = index&&rpcList[index]?rpcList[index]:rpcList[0];
var n = [rpc].concat(e.userInfo);
sendToAria2(n)
}
function validateCommand (e) {
if ("DownloadWithAria2" === e.command) {
var match = e.command&&e.command.match(/^DownloadWithAria2/);
if (match&&match.length>=0) {
var a = e.userInfo;
(a && a.length && a[0]) || (e.target.disabled = !0)
}
Expand All @@ -63,19 +137,19 @@ function handleNavigation (e) {
for (var n = 0; n < fileTypes.length; n++)if (a === fileTypes[n]) {
e.preventDefault();
var t = [
rpcList[config.defaultRpcIndex].url,
rpcList[config.defaultRpcIndex],
e.url,
e.target.url
];
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("sendToAria2", t);
sendToAria2(t);
break
}
}
}

function handleContextMenu(event) {
rpcList.forEach(function (rpc,index) {
event.contextMenu.appendContextMenuItem(rpc.url, ['下载至',rpc.name].join(''));
event.contextMenu.appendContextMenuItem(["DownloadWithAria2",index].join("."), ['下载至',rpc.name].join(''));
});
}

Expand Down
Binary file modified safari2aria.safariextz
Binary file not shown.

0 comments on commit a3f68f2

Please sign in to comment.