Hive - Magnet Catcher

When enabled will hook magnets when clicked and send to hive

  1. // ==UserScript==
  2. // @name Hive - Magnet Catcher
  3. // @namespace https://openuserjs.org/users/DefSoul/scripts
  4. // @description When enabled will hook magnets when clicked and send to hive
  5. // @version 2.0 > Fixed page detection (works on kat, eztv etc)
  6. // @author DefSoul
  7. // @include http*://*
  8. // @include http*://touch.hive.im/account/*
  9. // @include http*://api.hive.im/api/*
  10. // @exclude http*://www.youtube.com/*
  11. // @exclude http*://*.google.com/*
  12. // @exclude http*://*.stripe.com/*
  13. // @exclude http*://*.facebook.com/*
  14. // @exclude http*://facebook.com/*
  15. // @exclude about:blank
  16. // @grant GM_getValue
  17. // @grant GM_setValue
  18. // @grant GM_deleteValue
  19. // @grant GM_listValues
  20. // @grant GM_log
  21. // @grant GM_xmlhttpRequest
  22. // @grant GM_addStyle
  23. // @grant GM_getResourceText
  24. // @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
  25. // @resource toastrCss http://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css
  26. // @require http://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js
  27. // ==/UserScript==
  28.  
  29. // CONFIG //
  30. var folderName = "# Magnets #"; // CASE SENSITIVE
  31. // END CONFIG //
  32.  
  33. var nameB = "Magnet Catcher: Test ";
  34. GM_log(nameB + location.href);
  35.  
  36. var mag;
  37. var auth;
  38. auth = GM_getValue("auth");
  39. var link;
  40. GM_setValue("ready", "false");
  41. //GM_deleteValue("auth");
  42.  
  43. toastr.options = {
  44. "closeButton": false,
  45. "debug": false,
  46. "newestOnTop": false,
  47. "progressBar": false,
  48. "positionClass": "toast-bottom-right",
  49. "preventDuplicates": true,
  50. "onclick": null,
  51. "showDuration": "300",
  52. "hideDuration": "1000",
  53. "timeOut": "5000",
  54. "extendedTimeOut": "1000",
  55. "showEasing": "swing",
  56. "hideEasing": "linear",
  57. "showMethod": "fadeIn",
  58. "hideMethod": "fadeOut"
  59. };
  60.  
  61. var newCSS = GM_getResourceText ("toastrCss");
  62. GM_addStyle(newCSS);
  63.  
  64. function log(str, colour){console.log('%c dbg> ' + str, 'background: #D3D3D3; color: ' + colour);} // CUSTOM LOG
  65.  
  66. //=========UNSAFEWINDOW=========//
  67. var bGreasemonkeyServiceDefined = false;
  68.  
  69. try {if (typeof Components.interfaces.gmIGreasemonkeyService === "object") {bGreasemonkeyServiceDefined = true;}}
  70. catch (err) {}
  71.  
  72. if (typeof unsafeWindow === "undefined" || !bGreasemonkeyServiceDefined){
  73. unsafeWindow = (function(){
  74. var a = document.createElement('p');
  75. a.setAttribute ('onclick', 'return window;');
  76. return a.onclick ();
  77. })();
  78. }
  79. //--------------------
  80. function createFolder(uploadFolderName){
  81. GM_xmlhttpRequest({ //CROSS DOMAIN POST REQUEST
  82. "method": "get",
  83. "url": "https://api.hive.im/api/hive/get/",
  84. "headers": {
  85. 'Content-Type': 'application/x-www-form-urlencoded;',
  86. 'Authorization': auth,
  87. 'Client-Type': 'Browser',
  88. 'Client-Version': '0.1',
  89. 'Referer': 'https://touch.hive.im/myfiles/videos',
  90. 'Origin': 'https://touch.hive.im/'
  91. },
  92. "onload": function(data){
  93. var r = data.responseText;
  94. var json = JSON.parse(r);
  95. for (var i = 0; i < json.data.length; i++){
  96. var id;
  97. if (json.data[i].title === "Videos"){ // FINDS INITIAL VIDEOS FOLDER ID
  98. //log("we got a video ova here", "green");
  99. parentId = json.data[i].parentId;
  100. id = json.data[i].id;
  101. GM_xmlhttpRequest({ //CROSS DOMAIN POST REQUEST
  102. "method": "post",
  103. "url": "https://api.hive.im/api/hive/get-children/",
  104. "data": "&parentId=" + id + "&limit=1000",
  105. "headers": {
  106. 'Content-Type': 'application/x-www-form-urlencoded;',
  107. 'Authorization': auth,
  108. 'Client-Type': 'Browser',
  109. 'Client-Version': '0.1',
  110. 'Referer': 'https://touch.hive.im/',
  111. 'Origin': 'https://touch.hive.im/'
  112. },
  113. "onload": function(data){
  114. var r = data.responseText;
  115. var json = JSON.parse(r);
  116. var hasFolderIndex;
  117. Object.keys(json.data).forEach(function(key) {
  118. //log(json.data[key].title, "blue");
  119. hasFolderIndex += json.data[key].title;
  120. if (json.data[key].title === uploadFolderName){
  121. uploadFolderId = json.data[key].id;
  122. log("<" + uploadFolderName + "> Already exists. " + uploadFolderId, "green");
  123. //return json.data[key].id;
  124. }
  125. });
  126. if (hasFolderIndex.indexOf(uploadFolderName) == -1){ // SEARCHES VIDEOS FOLDER TO SEE IF uploadFolderName EXISTS
  127. log("does not contain: " + uploadFolderName, "red");
  128. GM_xmlhttpRequest({ //CROSS DOMAIN POST REQUEST
  129. "method": "post",
  130. "url": "https://api.hive.im/api/hive/create/",
  131. "data": "filename=" + uploadFolderName + "&parent=" + id + "&locked=false",
  132. "headers": {
  133. 'Content-Type': 'application/x-www-form-urlencoded;',
  134. 'Authorization': auth,
  135. 'Client-Type': 'Browser',
  136. 'Client-Version': '0.1',
  137. 'Referer': 'https://touch.hive.im/',
  138. 'Origin': 'https://touch.hive.im/'
  139. },
  140. "onload": function(data){
  141. var r = data.responseText;
  142. var json = JSON.parse(r);
  143. uploadFolderId = json.data.id;
  144.  
  145. log("Create folder <" + uploadFolderName + "> " + json.data.id);
  146. return json.data.id;
  147. }
  148. });
  149. }
  150. else{
  151. //log("does contain: " + uploadFolderName, "green");
  152. }
  153. }
  154. });
  155. //log(parentId + "\n" + currentId);
  156. }
  157. //log(item, "blue");
  158. }
  159. //log(r, "blue");
  160. }
  161. });
  162. }
  163.  
  164. function cdReq(href, nameT, folderId){
  165. //log("cdReq start: " + href);
  166. GM_xmlhttpRequest({ //CROSS DOMAIN POST REQUEST
  167. "method": "post",
  168. "url": "https://api.hive.im/api/transfer/add/",
  169. "data": "remoteUrl=" + window.btoa(href) + "&parentId=" + folderId,
  170. //"data": "remoteUrl=" + window.btoa(href),
  171. "headers": {
  172. 'Content-Type': 'application/x-www-form-urlencoded;',
  173. 'Authorization': GM_getValue("auth"),
  174. 'Client-Type': 'Browser',
  175. 'Client-Version': '0.1',
  176. 'Referer': 'https://touch.hive.im/',
  177. 'Origin': 'https://touch.hive.im/'
  178. },
  179. "onload": function(data){
  180. var r = data.responseText;
  181. var json = JSON.parse(r);
  182. if (json.status === "success"){
  183. toastr.success("Status: " + json.data.status, nameT);
  184. log("========= " + nameT + " success =========", "green");
  185. log("Job ID: " + json.data.jobId, "blue");
  186. log("Data Status: " + json.data.status, "blue");
  187. log("Folder Id: " + folderId, "blue");
  188. log("", "red");
  189. }
  190. else{
  191. if (json.message === "quotaExceeded"){
  192. toastr.warning(nameT, "Quota Exceeded");
  193. }
  194. else if (json.message === "securityViolation"){
  195. toastr.error(nameT, "Security Violation");
  196. }
  197.  
  198. log("========= " + nameT + " error =========", "green");
  199. log("Message: " + json.message, "blue");
  200. log("", "red");
  201. }
  202. //log("cdReq >" + data.responseText);
  203. //transferItemsList(); // GO GET ITEMS IN CURRENT TRANSFER LIST
  204. }
  205. });
  206. }
  207.  
  208. function torrentInfo(str){ // EXPERIMENTAL
  209. try {
  210. var result = "";
  211. result = unescape(str);
  212. log(result);
  213. result = result.match(/dn=(.*?)tr=/g); // EXTRACTION INTO ARRAY
  214. result[0] = result[0].replace("dn=", "");
  215. result[0] = result[0].replace("&amp;tr=", "");
  216. result[0] = result[0].replace("&tr=", "");
  217. result[0] = result[0].replace(/\+/g, " ");
  218.  
  219. //var myRegexp = /(.*?)\.S?(\d{1,2})E?(\d{2})\.(.*)/g;
  220. //var match = myRegexp.exec(result[0]);
  221. var name = result[0];
  222. //var season = match[2];
  223. //var episode = match[3];
  224. //name = name.replace(/\./g, " ");
  225. //var r = result[0].toString();
  226. var r = result[0];
  227. //log("" + r + "> " + folderId);
  228. return r;
  229. }
  230. catch(err){
  231. log("oh o! " + err, "red");
  232. }
  233. }
  234.  
  235. //
  236. if (window.top === window.self) {
  237. //=========MAIN WINDOW=========//
  238. //log(document.documentElement.innerHTML);
  239. var source = document.documentElement.innerHTML;
  240. try{
  241. bA = $("body h1").html();
  242. if (typeof bA == "undefined"){
  243. bA = "";
  244. }
  245. log(bA + nameB + " >body h1");
  246. }
  247. catch(err){bA = "";}
  248. if (bA.indexOf("Index of") == -1 && document.location.href.indexOf("touch.hive.im") == -1){
  249. createFolder(folderName);
  250. //var keys = GM_listValues();
  251. //for (var i=0, key=null; key=keys[i]; i++) {
  252. // GM_deleteValue(key);
  253. //}
  254. if (!$("#iframeHive").length || typeof auth === "undefined"){
  255. var iframe = document.createElement('iframe');
  256. iframe.id = "iframeHive";
  257. iframe.src = "https://touch.hive.im/account/?3";
  258. iframe.style = "height: 0px; width: 0px; display: none; overflow:hidden";
  259. document.body.appendChild(iframe);
  260. $("#iframeHive").attr("style", "height: 0px; width: 0px; display: none; overflow:hidden");
  261. //$("#iframeHive").attr("style", "height: 600px; width: 600px; display: block; overflow:hidden");
  262. log("iframe created! " + nameB + ": " + location.href);
  263. }
  264.  
  265. var onceB = 0;
  266. setInterval(function(){
  267. //log("AA: " + auth);
  268. if (onceB === 0 && typeof auth !== "undefined"){
  269. GM_setValue("ready", "true");
  270. GM_setValue("auth", auth);
  271. //$("#iframeHive").remove();
  272. //log("TRUE: " + auth);
  273. }
  274.  
  275. if (onceB === 0 && GM_getValue("ready") == "true"){
  276. onceB = 1;
  277.  
  278. auth = GM_getValue("auth");
  279. log("A: " + auth);
  280.  
  281. $("#iframeHive").remove();
  282. //init();
  283. }
  284. }, 250);
  285.  
  286. $(document).on("click", "a", function(event){ // selector click event
  287. if ($(this).attr('href').indexOf("magnet") !== -1){
  288. mag = $(this).attr('href');
  289. event.preventDefault();
  290. log("WE GOT A MAGNET OVA HERE >" + mag);
  291.  
  292. $('body').prepend('<a href="#" class="hive">Hive</a>');
  293. $(".hive").attr("style", 'display: block;width: 40px;height: 40px;text-indent: -9999px;position: fixed;z-index: 999999;right: 50%;top: 3px;background: url("http://imgh.us/download_4.svg") no-repeat center 50%;border-radius: 30px');
  294. $(".hive").fadeOut("slow");
  295.  
  296. cdReq(mag, torrentInfo(mag), uploadFolderId);
  297. }
  298. });
  299. }
  300. }
  301. else
  302. {
  303. //=========IFRAME WINDOW=========//
  304. try{
  305. auth = unsafeWindow.account.token;
  306. }
  307. catch(err){}
  308. var once = 0;
  309. setInterval(function(){ // EVENT FOR WHEN PAGE IS LOADED // RUNS ONCE
  310. if (once === 0 && $("#username").text().indexOf("My Account") !== -1){
  311. once = 1;
  312. log("ready");
  313. try{
  314. auth = unsafeWindow.account.token;
  315. }
  316. catch(err){}
  317. GM_setValue("auth", unsafeWindow.account.token);
  318. GM_setValue("ready", "true");
  319. }
  320. else if (once === 1 && typeof auth == "undefined"){
  321. GM_setValue("ready", "false");
  322. try{
  323. auth = unsafeWindow.account.token;
  324. }
  325. catch(err){}
  326. }
  327. }, 200);
  328. }
  329.  
  330.