Skip to content

Commit

Permalink
修复单页面多iframe引发的问题
Browse files Browse the repository at this point in the history
优化拉取cookie的逻辑
  • Loading branch information
miniers committed Jun 15, 2017
1 parent 29bb238 commit 10a14d6
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 86 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### v1.4:
1. 修复单页面多iframe引发的相同文件下载多次问题
2. 优化拉取cookie的逻辑
3. **aria2可以不用开启证书了**

### v1.3:
1. 增加工具栏图标和快捷键(option + shift + ,),快速打开设置界面
2. 全局拦截模式,按着 shift 会拦截所有请求,此模式非常适合百度云和迅雷离线,可以不用任何其他插件直接导入至aria2
Expand Down
41 changes: 3 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
4. 支持百度云和迅雷离线导出
5. 全局拦截模式


### 快捷键:
功能 | 快捷键
---- | ---
Expand All @@ -21,7 +22,8 @@
设置 | option+shift+,

### 注意事项
请在aria2c的配置文件中加入如下配置,用以开启百度云和迅雷离线导出时自动更改下载文件名
1. aria2已经无需配置SSL证书了
2. 请在aria2c的配置文件中加入如下配置,用以开启百度云和迅雷离线导出时自动更改下载文件名
```
content-disposition-default-utf8=true
```
Expand All @@ -48,40 +50,3 @@ content-disposition-default-utf8=true
- 如当前为自动拦截模式,长按**shift**并点击**普通下载**按钮即可导出下载至默认rpc服务器
- 如当前关闭了自动拦截默认,需长按**shift+cmd**并点击**普通下载**按钮即可导出下载至默认rpc服务器


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

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

详细原因请浏览此篇博文 [https://imququ.com/post/sth-about-switch-to-https.html#toc-0-2](https://imququ.com/post/sth-about-switch-to-https.html#toc-0-2)

请在 aria2c 的配置文件中添加如下配置引入证书
```
rpc-secure=true
rpc-certificate=path/to/you/cert
rpc-private-key=path/to/you/key
```

如果你的aria2c运行于macos下,

则需要通过**钥匙串访问**程序将证书先行导入

并在证书简介页面的最低部找到SHA-1 指纹,填写入aria2c的配置文件
```
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的配置文件中添加证书

4 changes: 2 additions & 2 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.3</string>
<string>1.4</string>
<key>CFBundleVersion</key>
<string>1.3</string>
<string>1.4</string>
<key>Chrome</key>
<dict>
<key>Database Quota</key>
Expand Down
18 changes: 9 additions & 9 deletions safari2aria.safariextension/js/endscript.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function () {
if (window.top === window){(function () {

var mObserver;
var config;
Expand Down Expand Up @@ -32,6 +32,11 @@
config = e.message || {};
catchIframe();
}
if(e.message && e.message.hasCb){
safari.self.tab.dispatchMessage([e.name,'cb'].join('_'), {
cookie: document.cookie
});
}
}

function handleContextMenu (e) {
Expand Down Expand Up @@ -101,30 +106,25 @@
safari.self.tab.dispatchMessage("keyPress", {
keyPressed: keyPressed
});
saveCookie();
}

function saveCookie () {
safari.self.tab.dispatchMessage("setCookie", {
cookie: document.cookie
});
}


function init () {
miniToastr.init({
appendTarget: document.body,
timeout: 5000
});
saveCookie();
safari.self.tab.dispatchMessage("getConfig");

document.addEventListener("contextmenu", handleContextMenu, !1);


safari.self.addEventListener("message", handleMessage, !1);

sendKeyPressEvent();
}

init()

})();
})()}
107 changes: 70 additions & 37 deletions safari2aria.safariextension/js/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,44 @@ var cookie;
var isCommandPressed, isShiftPressd, isOptionPressd;
var fileTypes = [];
var rpcList = [];
var timers={};
var messageAction = {
updateSafari2Aria:function (msg) {
localStorage.setItem("safari2aria", JSON.stringify(msg));
restoreOptions()
},
keyPress:function (msg) {
keyPressAction(msg)
},
getConfig:function () {
sendMsg('receiveConfig', config);
},
downloadFromIframe:function (msg,e) {
if(!isCommandPressed){
var t = [
rpcList[config.defaultRpcIndex],
msg.url,
e.target.url,
msg.cookie
];
sendToAria2(t);
}
}
}

function delay (name, func, wait, scope) {
return function delayed() {
var context = scope || this,
args = Array.prototype.slice.call(arguments);
if(timers[name]){
clearTimeout(timers[name])
}
timers[name] = setTimeout(function () {
delete timers[name];
func.apply(context, args);
}, wait || 10);
};
}

var ARIA2 = (function () {
var jsonrpc_version = '2.0';
Expand Down Expand Up @@ -47,7 +85,7 @@ var ARIA2 = (function () {
return function (jsonrpc_path) {
this.jsonrpc_path = jsonrpc_path;
this.addUri = function (uri, options, cb) {
request(this.jsonrpc_path, 'aria2.addUri', [[uri,], options], cb);
delay(uri,request,100,this)(this.jsonrpc_path, 'aria2.addUri', [[uri], options], cb);
};
return this;
}
Expand All @@ -61,22 +99,22 @@ function sendToAria2 (e) {
"user-agent":config.userAgent
}, function (err) {
if (err) {
showToast("showMassage", {
sendMsg("showMassage", {
action: 'error',
text: ['添加到', e[0].name, '失败'].join('')
text: ['添加到', e[0].name, '失败',config.enableCookie?"":'(关闭cookie)'].join('')
});
} else {
showToast("showMassage", {
sendMsg("showMassage", {
action: 'success',
text: ['添加到', e[0].name, '成功'].join('')
text: ['添加到', e[0].name, '成功',config.enableCookie?"":'(关闭cookie)'].join('')
});
}
});
}
}

function openPreferences (e) {
"showOptions" === e.key && (openOptions(), optionsEvent = e)
"showOptions" === e.key && openOptions()
}
function openOptions () {
safari.application.activeBrowserWindow.openTab().url = safari.extension.baseURI + "options.html"
Expand All @@ -93,34 +131,26 @@ function restoreOptions () {
fileTypes = config.filetypes ? config.filetypes.split(" ") : [];
for (var a = 0; a < fileTypes.length; a++)fileTypes[a] = fileTypes[a].toLowerCase()
rpcList = config.rpcList;
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage('receiveConfig', config);
sendMsg('receiveConfig', config);
}
function messageHandler (e) {
if (e.name === "updateSafari2Aria") {
localStorage.setItem("safari2aria", JSON.stringify(e.message));
restoreOptions()
if(messageAction[e.name]){
messageAction[e.name](e.message,e);
}
if (e.name === "keyPress") {
keyPressAction(e.message)
}
if (e.name === "setCookie") {
cookie = e.message.cookie;
}
if (e.name === "getConfig") {
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage('receiveConfig', config);
}
function sendMsg (type, msg , cb) {
if(msg instanceof Function){
cb = msg;
msg={};
}
if (e.name === "downloadFromIframe" && !isCommandPressed) {
var t = [
rpcList[config.defaultRpcIndex],
e.message.url,
e.target.url,
e.message.cookie
];
sendToAria2(t);
if(cb){
msg = Object.assign(msg || {},{
hasCb:true
});
messageAction[type+'_cb'] = cb;
}
}
function showToast (type, msg) {
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage(type, msg);

}
function keyPressAction (keys) {
var keyPressed = keys.keyPressed || {};
Expand All @@ -135,12 +165,12 @@ function keyPressAction (keys) {
name: 'updateSafari2Aria',
message: config
});
showToast("changeRpc", rpcList[config.defaultRpcIndex].name);
sendMsg("changeRpc", rpcList[config.defaultRpcIndex].name);
break;
}
}
if (keyPressed[192]) {
showToast("currentRpc", rpcList[config.defaultRpcIndex].name);
sendMsg("currentRpc", rpcList[config.defaultRpcIndex].name);
}
if (keyPressed[188]) {
openOptions();
Expand Down Expand Up @@ -172,13 +202,16 @@ function handleNavigation (e) {
for (var n = 0; n < fileTypes.length; n++) {
if (a === fileTypes[n] || isShiftPressd) {
e.preventDefault();
var t = [
rpcList[config.defaultRpcIndex],
e.url,
e.target.url,
cookie
];
sendToAria2(t);
sendMsg('getCookie',function (msg) {
var t = [
rpcList[config.defaultRpcIndex],
e.url,
e.target.url,
msg.cookie
];
sendToAria2(t);
});

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

0 comments on commit 10a14d6

Please sign in to comment.