Advertisement
Guest User

Untitled

a guest
Dec 20th, 2016
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.93 KB | None | 0 0
  1. // ==UserScript==
  2. // @name PassTheHeadphones :: Send torrent to (u/ru/w)Torrent WebUI
  3. // @namespace idkwhattoputhere
  4. // @include http*://*passtheheadphones.me/*
  5. // @grant GM_registerMenuCommand
  6. // @grant GM_setValue
  7. // @grant GM_getValue
  8. // ==/UserScript==
  9. // Modified What.CD :: Send torrent to uTorrent WebUI for PassTheHeadphones
  10. // Adding custom site instructions:
  11. // Ok, the system I programed is kind of hard to explain, although it seems like the best way to
  12. // manage mutipal sites with different codebases.
  13. // On to the instructions:
  14. // The format of siteArray is:
  15. // 1 - Name of site. (If the torrent site you are trying to add is: thetorrentsite.org, the name would be "thetorrentsite" w/o the .org)
  16. // 2 - Strip SSL. (If the site is using an unsinged SSL certificate uTorrent might not like it. If so, set this to "1" for that site. ruTorrent and wTorrent auto-overide this to accept SSL)
  17. // 3 - Use cookies. (Some site require cookies to download torrents, if so, set this to "1")
  18. // 4 - Search String 1 (String of text in every download url, like "action=download" or "download.php")
  19. // 5 - Search String 2 (A string of text that is NOT in every download url.** EXPLAINED BELOW **)
  20. // 6 - Link text (By defualt, "µT " is used on What, although some sites " Send to wUI" looks better. This can be any string of text though)
  21. // 7 - Separation text (Text that will show up before the Link text, if no Separation text is needed, set it to "")
  22. //
  23. // ** SEARCH STRING 2 **
  24. // Some sites have a .torrent download link AND a .zip or .txt download link. If this is the case, set this to ".zip", ".txt", or some other string of text that comes up in every .zip/.txt link.
  25. // If the site you are trying to add to the script DOES NOT have a 2nd download link for .zip/.txt, set Search String 2 to "µ"
  26. var siteArray = [
  27. ]; // << DO NOT EDIT THIS LINE
  28. siteArray[0] = [
  29. 'passtheheadphones.me',
  30. '1',
  31. '0',
  32. 'action=download',
  33. 'µ',
  34. 'µT ',
  35. ' | '
  36. ];
  37. /////////////////////////////////
  38. // DO NOT EDIT BELOW THIS POINT//
  39. /////////////////////////////////
  40. //Mode changing:
  41. //Set up "User Script Command"
  42. GM_registerMenuCommand('Setup webUI', function () {
  43. var webUIPrompt = window.prompt('What webUI are you using?\n\n1) uTorrent\n2) ruTorrent v2\n3) ruTorrent v3\n4) wTorrent\n\n(Type 1,2, or 3 below)');
  44. if (webUIPrompt == '1' || webUIPrompt == '2' || webUIPrompt == '3' || webUIPrompt == '4')
  45. {
  46. GM_setValue('custom_webUI', webUIPrompt);
  47. if (webUIPrompt == '4')
  48. {
  49. var wTorrent_hostname = window.prompt('What is your wTorrent hostname or ip address? (ex: "1.2.3.4" or "user.seedboxservice.com")');
  50. var wTorrent_port = window.prompt('What port is your wTorrent running on?');
  51. var wTorrent_username = window.prompt('What is your wTorrent username?');
  52. var wTorrent_password = window.prompt('What is your wTorrent password?');
  53. var wTorrent_ssl = window.prompt('Is your wTorrent running on an ssl encrypted server?\n"1" for yes, "2" for no');
  54. var wTorrent_pvtAdd = window.prompt('Do you want torrents to be added to wTorrent as private?\n"1" for yes, "2" for no');
  55. var wTorrent_autostart = window.prompt('Do you want downloaded torrents to autostart?\n"1" for yes, "2" for no');
  56. var wTorrent_isCorrect = window.prompt('Is this information correct? (Type "Correct" or "Incorrect")\nNote: all of this information has to be correct for the script to work!\n\nHostname: ' + wTorrent_hostname + '\nPort: ' + wTorrent_port + '\nUsername: ' + wTorrent_username + '\nPassword: ' + wTorrent_password + '\nSSL: ' + wTorrent_ssl + ' (1 is yes, 2 is no)\nAutostart: ' + wTorrent_autostart + ' (1 is yes, 2 is no)');
  57. if (wTorrent_isCorrect == 'Correct')
  58. {
  59. GM_setValue('custom_wTorrent_hostname', wTorrent_hostname);
  60. GM_setValue('custom_wTorrent_port', wTorrent_port);
  61. GM_setValue('custom_wTorrent_username', wTorrent_username);
  62. GM_setValue('custom_wTorrent_password', wTorrent_password);
  63. GM_setValue('custom_wTorrent_ssl', wTorrent_ssl);
  64. GM_setValue('custom_wTorrent_pvtAdd', wTorrent_pvtAdd);
  65. GM_setValue('custom_wTorrent_autostart', wTorrent_autostart);
  66. alert('wTorrent Settings Saved!');
  67. }
  68. else {
  69. alert('Please try entering the information in again.');
  70. }
  71. }
  72. else {
  73. var setWebIP = window.prompt('What is your hostname and port?\n(Format: "1.2.3.4:5678" or "user.seedboxservice.com")');
  74. var setruSSL = window.prompt('Do you connect to your webUI over SSL (https)?\n0) No\n1) Yes\nEnter "0" or "1" below, w/o quotes. If you are unsure, enter "0".');
  75. if (setWebIP != '' && setruSSL != '')
  76. {
  77. GM_setValue('custom_webIP', setWebIP);
  78. GM_setValue('custom_ruSSL', setruSSL);
  79. alert(setWebIP + ' has now been set as your hostname:port');
  80. }
  81. else {
  82. alert('Error. Please try again.');
  83. }
  84. }
  85. location.reload();
  86. }
  87. else {
  88. alert('Malformed entry, please try again.');
  89. }
  90. });
  91. //Set webUI mode:
  92. var webUIType = GM_getValue('custom_webUI', '0');
  93. var webIP = GM_getValue('custom_webIP', '');
  94. var ruSSL = GM_getValue('custom_ruSSL', '0');
  95. if (webUIType == '0')
  96. {
  97. alert('You must run the setup script before using this script.\nTo do this, please right click the Greasemonkey icon > User Script Commands... > Setup WebUI');
  98. }
  99. //if wTorrent
  100.  
  101. var wtorrent_connect_string;
  102. if (webUIType == '4')
  103. {
  104. var wHost = GM_getValue('custom_wTorrent_hostname', '');
  105. var wPort = GM_getValue('custom_wTorrent_port', '');
  106. var wUser = GM_getValue('custom_wTorrent_username', '');
  107. var wPass = GM_getValue('custom_wTorrent_password', '');
  108. var wPvtAdd = GM_getValue('custom_wTorrent_pvtAdd', '');
  109. var wSSL = GM_getValue('custom_wTorrent_ssl', '');
  110. var wStart = GM_getValue('custom_wTorrent_autostart', '');
  111. if (wSSL == '1') {
  112. wtorrent_connect_string = 'https://' + wHost + ':' + wPort + '/';
  113. } else {
  114. wtorrent_connect_string = 'http://' + wHost + ':' + wPort + '/';
  115. }
  116. post(wtorrent_connect_string, 'userf=' + wUser + '&passwdf=' + wPass + '&user_login=Login', function (info) {
  117. var isLoggedIn = true
  118. });
  119. }
  120. //Rest of script
  121.  
  122. var useCookie = '0';
  123. var innerSep = '';
  124. var isWhat = '';
  125. var stripssl = '';
  126. if (webUIType == '1' || webUIType == '2' || webUIType == '3')
  127. {
  128. var ifEventListener = false;
  129. var iframe = document.createElement('iframe');
  130. document.body.appendChild(iframe);
  131. iframe.style.display = 'none';
  132. }
  133. start();
  134. function start()
  135. {
  136. var links = getLinks();
  137. for (var i = 0, link; link = links[i++]; )
  138. {
  139. if (match(link.href))
  140. {
  141. if (stripssl == '1')
  142. {
  143. link.href = link.href.replace(/http?s/, 'http');
  144. if (isWhat == '1')
  145. {
  146. link.href = link.href.replace(/ssl/, 'www');
  147. }
  148. }
  149. var uTorrentLink = makeUTorrentLink(link);
  150. link.parentNode.insertBefore(uTorrentLink, link.nextSibling);
  151. var separator = makeSep();
  152. link.parentNode.insertBefore(separator, link.nextSibling);
  153. }
  154. }
  155. }
  156. function makeUTorrentLink(link) {
  157. var uTorrentLink = document.createElement('a');
  158. var theCookie = setCookie();
  159. var uTorrentFrameSrc = makeWebUILink(webIP, link.href, theCookie);
  160. var wTorrentTorrentURL = link.href;
  161. uTorrentLink.href = uTorrentFrameSrc;
  162. uTorrentLink.id = 'uTLINK';
  163. if (webUIType == '1' || webUIType == '2' || webUIType == '3')
  164. {
  165. uTorrentLink.addEventListener('click', function (e) {
  166. iframe.setAttribute('src', uTorrentFrameSrc);
  167. if (!ifEventListener) {
  168. iframe.addEventListener('load', function () {
  169. window.alert('Torrent added!');
  170. }, false);
  171. ifEventListener = true;
  172. }
  173. e.preventDefault();
  174. }, false);
  175. }
  176. if (webUIType == '4')
  177. {
  178. var wTorrentRequestURL = 'torrenturl=' + encodeURIComponent(wTorrentTorrentURL);
  179. if (wPvtAdd == '1')
  180. {
  181. wTorrentRequestURL = wTorrentRequestURL + '&private=on';
  182. }
  183. if (wStart == '1')
  184. {
  185. wTorrentRequestURL = wTorrentRequestURL + '&start_now=on';
  186. }
  187. uTorrentLink.id = wTorrentRequestURL;
  188. uTorrentLink.addEventListener('click', function (e) {
  189. e.preventDefault();
  190. wTorrentClick(e)
  191. }, false);
  192. }
  193. uTorrentLink.innerHTML = linkText;
  194. return uTorrentLink;
  195. }
  196. function makeWebUILink(webIPforLink, linkhref, cookie)
  197. {
  198. if (webUIType == '1')
  199. {
  200. return returnSSL() + '://' + webIPforLink + '/gui/?action=add-url&s=' + escape(linkhref) + cookie;
  201. }
  202. if (webUIType == '2')
  203. {
  204. return returnSSL() + '://' + webIPforLink + '/addtorrent.php?url=' + escape(linkhref) + cookie;
  205. }
  206. if (webUIType == '3')
  207. {
  208. return returnSSL() + '://' + webIPforLink + '/php/addtorrent.php?url=' + escape(linkhref) + cookie;
  209. }
  210. if (webUIType == '4')
  211. {
  212. return 'wtorrent:' + linkhref;
  213. }
  214. }
  215. function makeSep() {
  216. var separatorText = document.createElement('text');
  217. separatorText.innerHTML = innerSep;
  218. return separatorText;
  219. }
  220. function wTorrentClick(sent)
  221. {
  222. sent = sent.target.id;
  223. post(wtorrent_connect_string + 'index.php?cls=AddT', sent, function (addedResponce) {
  224. var match_added = /Torrent added correctly/gim;
  225. var already_exists = /Error: File already exists in torrent directory, can't create .torrent/gim;
  226. if (addedResponce.match(match_added)) {
  227. alert('Torrent Successfully Added wTorrent!');
  228. }
  229. else if (addedResponce.match(already_exists)) {
  230. alert('Torrent has already been added to wTorrent!');
  231. }
  232. else {
  233. alert('Something is broken! Verify that you entered your wTorrent details/information correctly!');
  234. }
  235. })
  236. }
  237. function post(url, data, func) {
  238. GM_xmlhttpRequest({
  239. method: 'POST',
  240. url: url,
  241. headers: {
  242. 'Content-type': 'application/x-www-form-urlencoded'
  243. },
  244. data: data,
  245. onload: function (xhr) {
  246. func(xhr.responseText);
  247. }
  248. });
  249. }
  250. function setCookie() {
  251. if (useCookie == '1')
  252. {
  253. return escape(':COOKIE:') + escape(document.cookie.replace(' ', ''));
  254. }
  255. return '';
  256. }
  257. function getLinks()
  258. {
  259. var doc_links = document.links;
  260. var links = [
  261. ];
  262. for (var i = 0, link; link = doc_links[i++]; ) {
  263. links.push(link);
  264. }
  265. return links;
  266. }
  267. function match(url) {
  268. for (var i = 0; i < siteArray.length; i++)
  269. {
  270. if (url.match(siteArray[i][0]) && url.match(siteArray[i][3]) && !url.match(siteArray[i][4]))
  271. {
  272. if (siteArray[i][1] == '1' && webUIType != '2' && webUIType != '3') {
  273. stripssl = '1';
  274. }
  275. if (siteArray[i][2] == '1') {
  276. useCookie = '1';
  277. }
  278. if (url.match('ssl.what.cd')) {
  279. isWhat = '1';
  280. }
  281. if (siteArray[i][6] != '') {
  282. innerSep = siteArray[i][6];
  283. }
  284. if (siteArray[i][5] != '') {
  285. linkText = siteArray[i][5];
  286. }
  287. return 1;
  288. }
  289. }
  290. }
  291. function returnSSL() {
  292. if (ruSSL == '1') {
  293. return 'https';
  294. }
  295. else {
  296. return 'http';
  297. }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement