Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
miniers committed Jun 12, 2017
1 parent b3378f1 commit 3758788
Show file tree
Hide file tree
Showing 11 changed files with 10,361 additions and 0 deletions.
61 changes: 61 additions & 0 deletions Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Author</key>
<string>Miniers</string>
<key>Builder Version</key>
<string>12603.2.4</string>
<key>CFBundleDisplayName</key>
<string>safari2aria</string>
<key>CFBundleIdentifier</key>
<string>miniers.safari2aria</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>1.0</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>Chrome</key>
<dict>
<key>Database Quota</key>
<integer>10485760</integer>
<key>Global Page</key>
<string>global.html</string>
</dict>
<key>Content</key>
<dict>
<key>Scripts</key>
<dict>
<key>End</key>
<array>
<string>js/endscript.js</string>
</array>
<key>Start</key>
<array>
<string>js/toastr.js</string>
</array>
</dict>
</dict>
<key>Description</key>
<string>Catches downloads for aria2</string>
<key>DeveloperIdentifier</key>
<string>V34B9RF2D5</string>
<key>ExtensionInfoDictionaryVersion</key>
<string>1.0</string>
<key>Permissions</key>
<dict>
<key>Website Access</key>
<dict>
<key>Include Secure Pages</key>
<true/>
<key>Level</key>
<string>All</string>
</dict>
</dict>
<key>Update From Gallery</key>
<true/>
<key>Website</key>
<string>http://safari2aria.lk.mk/</string>
</dict>
</plist>
14 changes: 14 additions & 0 deletions Settings.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>Key</key>
<string>showOptions</string>
<key>Title</key>
<string>点击更改设置</string>
<key>Type</key>
<string>CheckBox</string>
</dict>
</array>
</plist>
75 changes: 75 additions & 0 deletions css/options.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
body {
background-color: #fff; /* #edeff5; */
}

#wrapper {
background-color: #f6f7f9;
border: 1px solid #cacdd9;
border-radius: 7px;

width: 500px;
margin: 12px auto;
padding: 10px;
}

h1 {
/* font-family: sans-serif;*/
font-size: 1.3em;
margin: 0 0 1.3em 0;
}

.block {
border-top: 1px solid #dfe1e8;
padding: 1.2em 0 0 0;
margin: 1.2em 0 0 0;
min-width: 160px;
}

.topLabel {
font-size: 14px;
}

textarea {
width: 485px;
height: 130px;
resize: none;
border-radius: 4px;
padding: 4px 6px;
margin-top: 10px;
font-size: 13px;
}

.msgLabel {
color: green;
padding-bottom: 10px;
font-size: 11px;
font-style: italic;
}

.button {
/* background-color: white;*/
/* color: black;*/
/* border: 1px solid #4CAF50;*/
/* border-radius: 4px;*/
padding: 6px 10px;
text-align: center;
text-decoration: none;
font-size: 13px;
}
.rpc{
display: flex;

}
.rpc label{
flex: none;
}
#aria2rpc{
width: 100%;
flex:1;
}
#rpcList input{
flex:1;
}
#rpcList .name{
flex:none;
}
10 changes: 10 additions & 0 deletions global.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>safari2aria</title>
<script type="text/javascript" src="js/global.js" charset="utf-8"></script>
</head>
<body>
<div></div>
</body>
</html>
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
127 changes: 127 additions & 0 deletions js/endscript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
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
}

function linksFromContainer (e) {
var n = {};
if (e.href) {
var t = e.title || e.alt || e.innerText || e.textContent || "title";
n[t] = e.href
}
for (var o = e.childNodes, r = 0; r < o.length; r++) {
var a = this.linksFromContainer(o.item(r));
if (null != a)
for (t in a) n[t] = a[t]
}
return n
}

function selectedLinks () {
var e = window.getSelection();
if (e && e.rangeCount > 0) {
var n = e.getRangeAt(0);
if (n) {
var t = n.commonAncestorContainer;
if (t) {
var o = linksFromContainer(t),
r = e.toString(),
a = [];
for (var i in o)
if (-1 != r.search(i)) {
var s = o[i];
"" != s && a.push(s)
}
return a
}
}
}
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);
}
}

function handleContextMenu (e) {

var n = new Array;
n[0] = linkForTarget(e.target);
n[1] = document.location.href;
n[2] = selectedLinks();
safari.self.tab.setContextMenuEventUserInfo(e, n)
}
var rpcList=[];
miniToastr.init({
appendTarget: document.body,
timeout: 5000
});
document.addEventListener("contextmenu", handleContextMenu, !1);

safari.self.addEventListener("message", handleMessage, !1);
63 changes: 63 additions & 0 deletions js/global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
function openPreferences (e) {
"showOptions" === e.key && (safari.application.activeBrowserWindow.openTab().url = safari.extension.baseURI + "options.html", optionsEvent = e)
}
function restoreOptions () {
config = localStorage.getItem("safari2aria");
try{
config = JSON.parse(config || "{}");
}catch (err){
config={};
}
fileTypes = config.filetypes? config.filetypes.split(" "):[];
for (var a = 0; a < fileTypes.length; a++)fileTypes[a] = fileTypes[a].toLowerCase()
rpcList = config.rpcList
}
function storageHandler (e) {
if(e.name == "updateSafari2Aria"){
localStorage.setItem("safari2aria", JSON.stringify(e.message));
restoreOptions()
}
}
function handleCommand (e) {
var n = [e.command].concat(e.userInfo);
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("sendToAria2", n)
}
function validateCommand (e) {
if ("DownloadWithAria2" === e.command) {
var a = e.userInfo;
(a && a.length && a[0]) || (e.target.disabled = !0)
}
}
function handleNavigation (e) {
if (null !== e.url && config.enableTypefiles) {
var a = e.url.substr(e.url.lastIndexOf(".") + 1);
a = a.toLowerCase();
for (var n = 0; n < fileTypes.length; n++)if (a === fileTypes[n]) {
e.preventDefault();
var t = [
rpcList[0].url,
e.url,
e.target.url
];
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage("sendToAria2", t);
break
}
}
}

function handleContextMenu(event) {
rpcList.forEach(function (rpc,index) {
event.contextMenu.appendContextMenuItem(rpc.url, ['发送至',rpc.name].join(''));
});
}

var config;
var fileTypes = [];
var rpcList = [];
document.addEventListener("DOMContentLoaded", restoreOptions);
safari.application.addEventListener("message", storageHandler, !1);
safari.extension.settings.addEventListener("change", openPreferences, !1);
safari.application.addEventListener("command", handleCommand, !1);
safari.application.addEventListener("validate", validateCommand, !1);
safari.application.addEventListener("beforeNavigate", handleNavigation, !1);
safari.application.addEventListener("contextmenu", handleContextMenu, false);
Loading

0 comments on commit 3758788

Please sign in to comment.