Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added custom directories to context menu

  • Loading branch information...
commit bba1ec67a8e89d415621b98e06faa368aa87742e 1 parent 4980f95
lehenry authored Janez Troha committed
Showing with 170 additions and 158 deletions.
  1. +163 −156 source/background.html
  2. +7 −2 source/js/inject.js
View
319 source/background.html
@@ -1,163 +1,170 @@
<!DOCTYPE html>
+<script type="text/javascript" src="js/torentAdder.js"></script>
<script type="text/javascript">
-
-// global variables
-var completedTorrents = '', // string of completed torrents to prevent duplicate notifications
- notificationTimer; // holds the timeout so that it can be cancelled and stopped
-var dirs=JSON.parse(localStorage.dirs);
-
-// send context-menu link to inject.js
-function sendLink(info, tab) {
- var customDir="";
- for (var i = 0, dirsLength = dirs.length; i < dirsLength; i++) {
- if(dirs[i].cid==info.menuItemId){
- customDir=dirs[i].dir;
- break;
- }
- }
+ // global variables
+ var completedTorrents = '',
+ // string of completed torrents to prevent duplicate notifications
+ notificationTimer; // holds the timeout so that it can be cancelled and stopped
+ // list of custom directories
+ var dirs=JSON.parse(localStorage.dirs);
- chrome.tabs.sendRequest(tab.id, { 'url': info.linkUrl, 'dir': customDir });
-}
-
-// context menu for forcably sending a link to transmission
-if (localStorage.dLocation === 'dlcustom' && dirs.length > 0) {
- for (var i = 0, dirsLength = dirs.length; i < dirsLength; i++) {
- var idDir=chrome.contextMenus.create({ 'title': dirs[i].label, 'contexts': [ 'link' ], 'onclick': sendLink });
- dirs[i].cid=idDir;
- }
-} else {
- chrome.contextMenus.create({ 'title': 'Send link to Transmission', 'contexts': [ 'link' ], 'onclick': sendLink });
-}
-
-// temporarily display text on browser icon
-function showBadge(text, color, duration) {
- chrome.browserAction.setBadgeBackgroundColor({ color: color });
- chrome.browserAction.setBadgeText({ text: text });
-
- setTimeout(function () { chrome.browserAction.setBadgeText({ text: '' }); }, duration);
-}
-
-// format the torrent download link into a valid torrent link
-function formatLink(url) {
- if ((/kickasstorrents\.com\/torrents/i).test(url)) {
- url = url.replace(/\/$/i, '.torrent');
- }
-
- return url;
-}
-
-// send request to the remote Transmission client
-function rpcTransmission(args, method, tag, port) {
- var xhr = new XMLHttpRequest();
- xhr.open('POST', localStorage.server + '/rpc', true, localStorage.user, localStorage.pass);
- xhr.setRequestHeader('X-Transmission-Session-Id', localStorage.sessionId);
-
- if (typeof tag === 'undefined') {
- xhr.send('{ "arguments": { ' + args + ' }, "method": "' + method + '" }');
- } else {
- xhr.send('{ "arguments": { ' + args + ' }, "method": "' + method + '", "tag": ' + tag + ' }');
- }
-
- xhr.onreadystatechange = function() { if (xhr.readyState === 4) {
- if (xhr.getResponseHeader('X-Transmission-Session-Id')) { // re-request with the correct session id
- localStorage.sessionId = xhr.getResponseHeader('X-Transmission-Session-Id');
- return rpcTransmission(args, method, tag, port);
- }
-
- var responseJSON = JSON.parse(xhr.responseText);
-
- if (method === 'torrent-add') {
- switch(responseJSON.result)
- {
- case 'success':
- showBadge('add', [0, 255, 0, 255], 5000);
- break;
- case 'duplicate torrent':
- showBadge('dup', [0, 0, 255, 255], 5000);
+ // send context-menu link to inject.js
+ function sendLink(info, tab) {
+ var customDir="";
+ //look for the selected directory using the menu item id
+ for (var i = 0, dirsLength = dirs.length; i < dirsLength; i++) {
+ if(dirs[i].cid==info.menuItemId){
+ customDir=dirs[i].dir;
break;
- default:
- showBadge('fail', [255, 0, 0, 255], 5000);
- alert('Torrent download failed!\n\n' + responseJSON.result);
}
- } else if (tag === 10) { // create a notification when a torrent finishes
- var notification;
-
- for (var i = 0, torrent; torrent = responseJSON.arguments.torrents[i]; ++i) {
- if (torrent.status === 16 && torrent.leftUntilDone === 0 && completedTorrents.indexOf(torrent.id) < 0) {
- notification = webkitNotifications.createNotification(
- 'images/icon48.png',
- 'Torrent Download Complete',
- torrent.name + ' has finished downloading.'
- );
- notification.show();
-
- // hide the notification after 30 seconds
- setTimeout(function() { notification.cancel(); }, '30000');
-
- // mark the completed torrent so another notification isn't displayed for it
- completedTorrents += torrent.id + ',';
- }
- }
- } else if (typeof port !== 'undefined') {
- port.postMessage({ args: responseJSON.arguments, tag: responseJSON.tag });
}
- }};
-}
-
-// receive messages from the popup and injected scripts
-chrome.extension.onConnect.addListener(function(port) {
- switch(port.name)
- {
- case 'popup':
- port.onMessage.addListener(function(msg) {
- switch(msg.method)
- {
- case 'torrent-get': case 'session-get':
- rpcTransmission(msg.args, msg.method, msg.tag, port);
- break;
- default:
- rpcTransmission(msg.args, msg.method);
- }
- });
- break;
- case 'inject':
- port.onMessage.addListener(function(msg) {
- // check if custom download location is enabled
- if (localStorage.dLocation === 'dlcustom' && dirs.length > 0) {
- if (typeof msg.dir === 'undefined') { // need a download location
- port.postMessage({ url: msg.url, dirs: dirs, method: 'customdl' });
- } else {
- rpcTransmission('"filename": "' + formatLink(msg.url) + '", "download-dir": "' + msg.dir + '"', msg.method);
- }
- } else {
- rpcTransmission('"filename": "' + formatLink(msg.url) + '"', msg.method);
- }
- });
- break;
- case 'options':
- port.onMessage.addListener(function(msg) {
- // stop the notification timer
- clearTimeout(notificationTimer);
-
- // start it up again if it's enabled
- if (msg.notifications) notificationRefresh();
- });
- break;
+ chrome.tabs.sendRequest(tab.id, {
+ url: info.linkUrl, dir: customDir
+ });
+ }
+
+ // context menu for forcably sending a link to transmission
+ // if custom locations are enabled
+ if (localStorage.dLocation === 'dlcustom' && dirs.length > 0) {
+ for (var i = 0, dirsLength = dirs.length; i < dirsLength; i++) {
+ // create an entry in context menu for the custom location
+ var idDir=chrome.contextMenus.create({ 'title': dirs[i].label, 'contexts': [ 'link' ], 'onclick': sendLink });
+ // save the context menu id on the location
+ dirs[i].cid=idDir;
+ }
+ } else {
+ // custom location disabled
+ chrome.contextMenus.create({
+ 'title': 'Send link to Transmission',
+ 'contexts': ['link'],
+ 'onclick': sendLink
+ });
}
-});
-
-// show notifications when a torrent completes (30s timer)
-function notificationRefresh() {
- rpcTransmission('"fields": [ "id", "name", "status", "leftUntilDone" ], "ids": "recently-active"', 'torrent-get', 10);
-
- notificationTimer = setTimeout(notificationRefresh, 30000);
-}
-
-// only display notifications and keep-alive if it's enabled
-if (localStorage.notifications === 'true') {
- notificationRefresh();
-}
-
-// first time users and new upgraders
-if (typeof localStorage.verConfig === 'undefined' || localStorage.verConfig < 2) chrome.tabs.create({ url: 'options.html' });
-</script>
+
+ // format the torrent download link into a valid torrent link
+
+ function formatLink(url) {
+ if ((/kickasstorrents\.com\/torrents/i).test(url)) {
+ url = url.replace(/\/$/i, '.torrent');
+ }
+
+ return url;
+ }
+
+ // send request to the remote Transmission client
+
+ function rpcTransmission(args, method, tag, port) {
+ var xhr = new XMLHttpRequest();
+ xhr.open('POST', localStorage.server + '/rpc', true, localStorage.user, localStorage.pass);
+ xhr.setRequestHeader('X-Transmission-Session-Id', localStorage.sessionId);
+ if (method === 'torrent-add' && typeof(new XMLHttpRequest().responseType) != "undefined") {
+ downloadTorrent(JSON.parse("{" + args + "}").filename);
+ } else {
+ if (typeof tag === 'undefined') {
+ xhr.send('{ "arguments": { ' + args + ' }, "method": "' + method + '" }');
+ } else {
+ xhr.send('{ "arguments": { ' + args + ' }, "method": "' + method + '", "tag": ' + tag + ' }');
+ }
+ }
+ xhr.onreadystatechange = function() {
+ if (xhr.readyState === 4) {
+ if (xhr.getResponseHeader('X-Transmission-Session-Id')) { // re-request with the correct session id
+ localStorage.sessionId = xhr.getResponseHeader('X-Transmission-Session-Id');
+ return rpcTransmission(args, method, tag, port);
+ }
+
+ var responseJSON = JSON.parse(xhr.responseText);
+
+ if (tag === 10) { // create a notification when a torrent finishes
+ var notification;
+
+ for (var i = 0, torrent; torrent = responseJSON.arguments.torrents[i]; ++i) {
+ if (torrent.status === 16 && torrent.leftUntilDone === 0 && completedTorrents.indexOf(torrent.id) < 0) {
+ notification = webkitNotifications.createNotification('images/icon48.png', 'Torrent Download Complete', torrent.name + ' has finished downloading.');
+ notification.show();
+
+ // hide the notification after 30 seconds
+ setTimeout(function() {
+ notification.cancel();
+ }, '30000');
+
+ // mark the completed torrent so another notification isn't displayed for it
+ completedTorrents += torrent.id + ',';
+ }
+ }
+ } else if (typeof port !== 'undefined') {
+ port.postMessage({
+ args: responseJSON.arguments,
+ tag: responseJSON.tag
+ });
+ }
+ }
+ };
+ }
+
+ // receive messages from the popup and injected scripts
+ chrome.extension.onConnect.addListener(function(port) {
+ switch (port.name) {
+ case 'popup':
+ port.onMessage.addListener(function(msg) {
+ switch (msg.method) {
+ case 'torrent-get':
+ case 'session-get':
+ rpcTransmission(msg.args, msg.method, msg.tag, port);
+ break;
+ default:
+ rpcTransmission(msg.args, msg.method);
+ }
+ });
+ break;
+ case 'inject':
+ port.onMessage.addListener(function(msg) {
+
+ // check if custom download location is enabled
+ if (localStorage.dLocation === 'dlcustom' && dirs.length > 0) {
+ if (typeof msg.dir === 'undefined') { // need a download location
+ port.postMessage({
+ url: msg.url,
+ dirs: dirs,
+ method: 'customdl'
+ });
+ } else {
+ rpcTransmission('"filename": "' + formatLink(msg.url) + '", "download-dir": "' + msg.dir + '"', msg.method);
+ }
+ } else {
+ rpcTransmission('"filename": "' + formatLink(msg.url) + '"', msg.method);
+ }
+ });
+ break;
+ case 'options':
+ port.onMessage.addListener(function(msg) {
+ // stop the notification timer
+ clearTimeout(notificationTimer);
+
+ // start it up again if it's enabled
+ if (msg.notifications) notificationRefresh();
+ });
+ break;
+ }
+ });
+
+ // show notifications when a torrent completes (30s timer)
+
+ function notificationRefresh() {
+ rpcTransmission('"fields": [ "id", "name", "status", "leftUntilDone" ], "ids": "recently-active"', 'torrent-get', 10);
+
+ notificationTimer = setTimeout(notificationRefresh, 30000);
+ }
+
+ // only display notifications and keep-alive if it's enabled
+ if (localStorage.notifications === 'true') {
+ notificationRefresh();
+ }
+
+ // first time users and new upgraders
+ if (typeof localStorage.verConfig === 'undefined' || localStorage.verConfig < 2) chrome.tabs.create({
+ url: 'options.html'
+ });
+
+
+</script>
View
9 source/js/inject.js
@@ -1,6 +1,7 @@
// global variables
var port = chrome.extension.connect({ name: 'inject' }),
customDLElem, torrentURL,
+ added = false,
searchPattern = /(^magnet:|\.torrent$|torrents\.php\?action=download|\?.*info_hash=|(bt-chat\.com|torrentreactor\.net|vertor\.com|seedpeer\.com|torrentzap\.com|limetorrents\.com|h33t\.com|ahashare\.com|1337x\.org|bitenova\.nl|bibliotik\.org).*download|alivetorrents\.com\/dl\/|newtorrents\.info\/down\.php|mininova\.org\/get|kickasstorrents\.com\/torrents)/i;
// attach clickTorrent as an onclick event on links that are found to be torrents
@@ -9,6 +10,11 @@ function findTorrentLinks() {
for (var i = 0, link; link = links[i]; ++i) {
if (searchPattern.test(link.href)) {
+ if (!added) {
+ // create the custom download location element
+ createCustomDLElem();
+ added = true;
+ };
link.addEventListener('click', clickTorrent, true);
}
}
@@ -162,5 +168,4 @@ port.onMessage.addListener(function(msg) {
findTorrentLinks();
document.addEventListener('load', findTorrentLinks, true);
-// create the custom download location element
-createCustomDLElem();
+
Please sign in to comment.
Something went wrong with that request. Please try again.