pt2transmission

Add a button in some private tracker site to support adding torrent to Transmission. Current support CCF and TTG.

当前为 2017-03-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name pt2transmission
  3. // @namespace https://github.com/coderant/
  4. // @version 0.0.1
  5. // @description Add a button in some private tracker site to support adding torrent to Transmission. Current support CCF and TTG.
  6. // @supportURL https://github.com/coderant/pt2transmission
  7. // @author Muffin_C
  8. // @match *://ccfbits.org/*
  9. // @match *://totheglory.im/*
  10. // @require https://code.jquery.com/jquery-3.2.1.min.js
  11. // @run-at document-end
  12. // @grant GM_xmlhttpRequest
  13. // ==/UserScript==
  14.  
  15. // Edit these before use.
  16. // http://192.168.1.1 for local access, input ddns for external access.
  17. // NO trailing slash(/).
  18. var transmission_url = "http://your.url.here";
  19.  
  20. // value of "rpc-port" in settings.json .
  21. var transmission_port = "9091";
  22.  
  23. // value of "rpc-url" in settings.json .
  24. var transmission_rpc_bind_address = "/transmission/";
  25.  
  26. // Authentication;
  27. var username = "your_username";
  28. var pw = "your_password";
  29.  
  30. // DO NOT EDIT BELOW.
  31. var rpc_url = transmission_url + ":" + transmission_port + transmission_rpc_bind_address + "rpc";
  32. console.log("Constructed url:" + rpc_url);
  33.  
  34. (function () {
  35. 'use strict';
  36. var site = window.location.href;
  37. var reCCF = /ccf/i;
  38. var reTTG = /totheglory/i;
  39. var baseURL = document.location.origin;
  40. var target;
  41. var buttonCSS = {
  42. 'background-color': '#B6B6B6',
  43. '-moz-border-radius': '2px',
  44. '-webkit-border-radius': '2px',
  45. 'border-radius': '2px',
  46. 'display': 'inline-block',
  47. 'cursor': 'pointer',
  48. 'color': '#000000',
  49. 'font-family': 'Verdana',
  50. 'font-size': '12px',
  51. 'padding': '2px 5px',
  52. 'text-decoration': 'none'
  53. };
  54.  
  55. if (reCCF.test(site)) {
  56. if (site.includes("browse")) {
  57. target = $('table[border=1][cellpadding=5]>>> td:nth-child(2):not([class])');
  58. target.each(function (i) {
  59. var pageURL = baseURL + "/" + $(this).find("a[title][href]").attr("href");
  60. var el = $('<a>', {id: "transmission_main_" + i, rel: pageURL, text: "Transmission"});
  61. el.css(buttonCSS);
  62. $(this).append(el);
  63. el.after($('<a>', {id: "transmission_main_" + i + "_result", text: "", style: "padding-left:5px"}));
  64. });
  65. }
  66. if (site.includes("details")) {
  67. target = $('a[class="index"][href*=".torrent"]');
  68. var ccfTorrentUrl = baseURL + "/" + target.attr("href");
  69. var ccfDetailInsert = $('<a>', {id: "transmission_detail", rel: ccfTorrentUrl, text: "Transmission"});
  70. ccfDetailInsert.css(buttonCSS);
  71. target.after(ccfDetailInsert);
  72. ccfDetailInsert.after($('<a>', {id: "transmission_detail_result", text: "", style: "padding-left:5px"}));
  73. target.after("<br>");
  74. }
  75. }
  76.  
  77. if (reTTG.test(site)) {
  78. if (site.includes("browse")) {
  79. target = $('tr[id]> td:nth-child(2)');
  80. target.each(function (i) {
  81. var page = $(this).find("a[href]").attr("href");
  82. var el = $('<a>', {id: "transmission_" + i, rel: baseURL + page, text: "Transmission"});
  83. el.css(buttonCSS);
  84. $(this).append(el);
  85. el.after($('<a>', {id: "transmission_" + i + "_result", text: "", style: "padding-left:5px"}));
  86. });
  87. }
  88. if (site.includes("/t/")) {
  89. target = $('a[class="index"][href*=".zip"]');
  90. var ttgTorrentUrl = baseURL + "/" + $('a[class="index"][href*=".torrent"]').attr("href");
  91. var ttgDetailInsert = $('<a>', {id: "transmission_detail", rel: ttgTorrentUrl, text: "Transmission"});
  92. ttgDetailInsert.css(buttonCSS);
  93. target.after(ttgDetailInsert);
  94. ttgDetailInsert.after($('<a>', {id: "transmission_detail_result", text: "", style: "padding-left:5px"}));
  95. target.after("<br>");
  96. }
  97. }
  98.  
  99. $('[id^=transmission]:not([id*=result]').click(function () {
  100. var torrentPage = $(this).attr('rel');
  101. var id = $(this).attr('id');
  102. console.log(id + " is clicked");
  103. if (id.includes("main")) {
  104. GM_xmlhttpRequest({
  105. method: "GET",
  106. url: torrentPage,
  107. onload: function (response) {
  108. // console.log("Start fetching torrent details");
  109. var torrentURL = baseURL + "/" + $(response.responseText).find('a[href*=".torrent"]').attr('href');
  110. // console.log("Extracted torrent url: " + torrentURL);
  111. var request = {
  112. arguments: {cookies: getCookie(), filename: torrentURL},
  113. method: "torrent-add",
  114. tag: 80
  115. };
  116. console.log("Clicked:" + id);
  117. addTorrent($("#" + id), $("#" + id + "_result"), request);
  118. }
  119. });
  120. }
  121. if (id.includes("detail")) {
  122. var torrentURL = baseURL + "/" + $('a[class="index"][href*=".torrent"]').attr('href');
  123. var request = {arguments: {cookies: getCookie(), filename: torrentURL}, method: "torrent-add", tag: 80};
  124. addTorrent($("#" + id), $("#" + id + "_result"), request);
  125. }
  126. });
  127. })();
  128.  
  129. function addTorrent(button, result, request, sessionId, tries) {
  130. if (!tries) {
  131. tries = 0;
  132. }
  133. if (tries === 3) {
  134. alert("p2transmission: Too many Error 409: Conflict.\nCheck your transmission installation");
  135. return;
  136. }
  137. console.log("sending torrent with sessionid: (" + sessionId);
  138. console.log("sending: " + JSON.stringify(request));
  139. GM_xmlhttpRequest({
  140. method: "POST",
  141. user: username,
  142. password: pw,
  143. url: rpc_url,
  144. data: JSON.stringify(request),
  145. headers: {
  146. "X-Transmission-Session-Id": sessionId
  147. },
  148. onload: function (response) {
  149. console.log([
  150. response.status,
  151. response.statusText,
  152. response.responseText
  153. ].join("\n"));
  154. var resultText;
  155. var success = false;
  156. var unclickable = false;
  157. var error = false;
  158. switch (response.status) {
  159. case 200: // status OK
  160. var rpcResponse = response.responseText;
  161. var rpcJSON = JSON.parse(rpcResponse);
  162. if (rpcJSON.result.toLowerCase() === "success") {
  163. if ("torrent-duplicate" in rpcJSON.arguments) {
  164. resultText = "Already added: " + rpcJSON['arguments']['torrent-duplicate'].name;
  165. } else {
  166. resultText = "Added: " + rpcJSON['arguments']['torrent-added'].name;
  167. }
  168. success = true;
  169. } else {
  170. resultText = 'ERROR: ' + rpcJSON.result;
  171. error = true;
  172. }
  173. unclickable = true;
  174. break;
  175. case 401:
  176. resultText = "Your username/password is not correct.";
  177. error = true;
  178. break;
  179. case 409:
  180. var headers = response.responseHeaders.split("\n");
  181. for (var i in headers) {
  182. var header = headers[i].split(":");
  183. if (header[0] == "X-Transmission-Session-Id") {
  184. sessionId = header[1].trim();
  185. console.log("Got new Session ID: (" + sessionId);
  186. addTorrent(button, result, request, sessionId, tries + 1);
  187. }
  188. }
  189. break;
  190. default:
  191. resultText = "Unknown Transmission Response";
  192. error = true;
  193. alert("Unknown Transmission Response: " + response.status + " " + response.statusText);
  194. }
  195. console.log(resultText);
  196. result.text(resultText);
  197. if (unclickable) {
  198. button.unbind('click');
  199. button.css("cursor", "default");
  200. }
  201. if (success) {
  202. button.css("background-color", "#8FFFA6");
  203. }
  204. if (error) {
  205. button.css("background-color", "#FFBAC2");
  206. }
  207. }
  208. });
  209. }
  210.  
  211. function getCookie() {
  212. // from https://github.com/bulljit/Transmission-Add-Torrent-Bookmarkelet Thanks guys.
  213. var sCookie = "";
  214. var aCookie = document.cookie.split(/;[\s\xA0]*/);
  215. if (aCookie !== "") {
  216. for (var i = 0; i < aCookie.length; i++) {
  217. if (aCookie[i].search(/(^__utm|^__qc)/) == -1) {
  218. sCookie = sCookie + aCookie[i] + '; ';
  219. }
  220. }
  221. }
  222. sCookie = sCookie.replace(/;\s+$/, "");
  223. return sCookie;
  224. }