Hive - Send to Hive Index Page

Allows sending multiple items from index pages to hive

目前为 2015-06-23 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Hive - Send to Hive Index Page
  3. // @namespace https://openuserjs.org/users/DefSoul/scripts
  4. // @description Allows sending multiple items from index pages to hive
  5. // @version 2.4 > Transfers now go into Videos/# Index # (Can be changed in source config)
  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. // @grant GM_getValue
  16. // @grant GM_setValue
  17. // @grant GM_deleteValue
  18. // @grant GM_listValues
  19. // @grant GM_log
  20. // @grant GM_xmlhttpRequest
  21. // @grant GM_addStyle
  22. // @grant GM_getResourceText
  23. // @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
  24. // @resource toastrCss http://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css
  25. // @require http://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js
  26. // @require https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js
  27. // @resource jQueryUICSS http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css
  28. // ==/UserScript==
  29. /*jshint multistr: true */
  30.  
  31. // CONFIG //
  32. var detectIndexPage = true; // FALSE MEANS IT WILL RUN ON EVERY PAGE
  33. var folderName = "# Index #"; // CASE SENSITIVE
  34. // END CONFIG //
  35.  
  36. // GLOBALS //
  37. var nameB = "Send to Hive Index Page: Test ";
  38. GM_log(nameB + location.href);
  39. var auth;
  40. var bA;
  41. var postMag = [];
  42. var a = [];
  43. var s;
  44. var origHref = [];
  45. var uploadFolderId;
  46.  
  47. //auth = "";
  48. //GM_deleteValue("auth");
  49.  
  50. auth = GM_getValue("auth");
  51. var link;
  52. GM_setValue("ready", "false");
  53.  
  54. toastr.options = {
  55. "closeButton": false,
  56. "debug": false,
  57. "newestOnTop": false,
  58. "progressBar": false,
  59. "positionClass": "toast-bottom-right",
  60. "preventDuplicates": true,
  61. "onclick": null,
  62. "showDuration": "300",
  63. "hideDuration": "1000",
  64. "timeOut": "5000",
  65. "extendedTimeOut": "1000",
  66. "showEasing": "swing",
  67. "hideEasing": "linear",
  68. "showMethod": "fadeIn",
  69. "hideMethod": "fadeOut"
  70. };
  71.  
  72. //=========
  73. function log(str, colour){console.log('%c dbg> ' + str, 'background: #D3D3D3; color: ' + colour);} // CUSTOM LOG
  74.  
  75. var newCSS = GM_getResourceText ("toastrCss");
  76. GM_addStyle(newCSS);
  77.  
  78. var newCSS2 = GM_getResourceText ("jQueryUICSS");
  79. GM_addStyle(newCSS2);
  80.  
  81. //=========UNSAFEWINDOW=========//
  82. var bGreasemonkeyServiceDefined = false;
  83.  
  84. try {if (typeof Components.interfaces.gmIGreasemonkeyService === "object") {bGreasemonkeyServiceDefined = true;}}
  85. catch (err) {}
  86.  
  87. if (typeof unsafeWindow === "undefined" || !bGreasemonkeyServiceDefined){
  88. unsafeWindow = (function(){
  89. var a = document.createElement('p');
  90. a.setAttribute ('onclick', 'return window;');
  91. return a.onclick ();
  92. })();
  93. }
  94. //
  95.  
  96. function createFolder(uploadFolderName){
  97. GM_xmlhttpRequest({ //CROSS DOMAIN POST REQUEST
  98. "method": "get",
  99. "url": "https://api.hive.im/api/hive/get/",
  100. "headers": {
  101. 'Content-Type': 'application/x-www-form-urlencoded;',
  102. 'Authorization': auth,
  103. 'Client-Type': 'Browser',
  104. 'Client-Version': '0.1',
  105. 'Referer': 'https://touch.hive.im/myfiles/videos',
  106. 'Origin': 'https://touch.hive.im/'
  107. },
  108. "onload": function(data){
  109. var r = data.responseText;
  110. var json = JSON.parse(r);
  111. for (var i = 0; i < json.data.length; i++){
  112. var id;
  113. if (json.data[i].title === "Videos"){ // FINDS INITIAL VIDEOS FOLDER ID
  114. //log("we got a video ova here", "green");
  115. parentId = json.data[i].parentId;
  116. id = json.data[i].id;
  117. GM_xmlhttpRequest({ //CROSS DOMAIN POST REQUEST
  118. "method": "post",
  119. "url": "https://api.hive.im/api/hive/get-children/",
  120. "data": "&parentId=" + id + "&limit=1000",
  121. "headers": {
  122. 'Content-Type': 'application/x-www-form-urlencoded;',
  123. 'Authorization': auth,
  124. 'Client-Type': 'Browser',
  125. 'Client-Version': '0.1',
  126. 'Referer': 'https://touch.hive.im/',
  127. 'Origin': 'https://touch.hive.im/'
  128. },
  129. "onload": function(data){
  130. var r = data.responseText;
  131. var json = JSON.parse(r);
  132. var hasFolderIndex;
  133. Object.keys(json.data).forEach(function(key) {
  134. //log(json.data[key].title, "blue");
  135. hasFolderIndex += json.data[key].title;
  136. if (json.data[key].title === uploadFolderName){
  137. uploadFolderId = json.data[key].id;
  138. log("<" + uploadFolderName + "> Already exists. " + uploadFolderId, "green");
  139. //return json.data[key].id;
  140. }
  141. });
  142. if (hasFolderIndex.indexOf(uploadFolderName) == -1){ // SEARCHES VIDEOS FOLDER TO SEE IF uploadFolderName EXISTS
  143. log("does not contain: " + uploadFolderName, "red");
  144. GM_xmlhttpRequest({ //CROSS DOMAIN POST REQUEST
  145. "method": "post",
  146. "url": "https://api.hive.im/api/hive/create/",
  147. "data": "filename=" + uploadFolderName + "&parent=" + id + "&locked=false",
  148. "headers": {
  149. 'Content-Type': 'application/x-www-form-urlencoded;',
  150. 'Authorization': auth,
  151. 'Client-Type': 'Browser',
  152. 'Client-Version': '0.1',
  153. 'Referer': 'https://touch.hive.im/',
  154. 'Origin': 'https://touch.hive.im/'
  155. },
  156. "onload": function(data){
  157. var r = data.responseText;
  158. var json = JSON.parse(r);
  159. uploadFolderId = json.data.id;
  160.  
  161. log("Create folder <" + uploadFolderName + "> " + json.data.id);
  162. return json.data.id;
  163. }
  164. });
  165. }
  166. else{
  167. //log("does contain: " + uploadFolderName, "green");
  168. }
  169. }
  170. });
  171. //log(parentId + "\n" + currentId);
  172. }
  173. //log(item, "blue");
  174. }
  175. //log(r, "blue");
  176. }
  177. });
  178. }
  179.  
  180. function cdReq(href, nameT, folderId){
  181. log("cdReq start: " + href);
  182. GM_xmlhttpRequest({ //CROSS DOMAIN POST REQUEST
  183. "method": "post",
  184. "url": "https://api.hive.im/api/transfer/add/",
  185. "data": "remoteUrl=" + window.btoa(href) + "&parentId=" + folderId,
  186. //"data": "remoteUrl=" + window.btoa(href),
  187. "headers": {
  188. 'Content-Type': 'application/x-www-form-urlencoded;',
  189. 'Authorization': GM_getValue("auth"),
  190. 'Client-Type': 'Browser',
  191. 'Client-Version': '0.1',
  192. 'Referer': 'https://touch.hive.im/',
  193. 'Origin': 'https://touch.hive.im/'
  194. },
  195. "onload": function(data){
  196. var r = data.responseText;
  197. var json = JSON.parse(r);
  198. if (json.status === "success"){
  199. toastr.success(nameT, "Status: " + json.data.status);
  200. log("========= " + nameT + " success =========", "green");
  201. log("Job ID: " + json.data.jobId, "blue");
  202. log("Data Status: " + json.data.status, "blue");
  203. log("Folder Id: " + folderId, "blue");
  204. log("", "red");
  205. }
  206. else{
  207. if (json.message === "quotaExceeded"){
  208. toastr.warning(nameT, "Quota Exceeded");
  209. }
  210. else if (json.message === "securityViolation"){
  211. toastr.error(nameT, "Security Violation");
  212. }
  213.  
  214. log("========= " + nameT + " error =========", "green");
  215. log("Message: " + json.message, "blue");
  216. log("", "red");
  217. }
  218. //log("cdReq >" + data.responseText);
  219. //transferItemsList(); // GO GET ITEMS IN CURRENT TRANSFER LIST
  220. }
  221. });
  222. };
  223.  
  224. $(document).on("click", "#bntDAll", function(){ // selector click event
  225. log("bntDALL clicked begin ");
  226. log("btnDALL: begin " + $(".ui-selected").attr("href"));
  227. postMag.length = 0;
  228. s = "";
  229. a = $('.ui-selected').map(function(){ // puts all un-encoded videos ids into an array
  230. return document.location.href + $(this).attr("href"); // returns an int array of videos id values
  231. });
  232. origHref = $('.ui-selected').map(function(){ // puts all un-encoded videos ids into an array
  233. return $(this).attr("href"); // returns an int array of videos id values
  234. });
  235.  
  236. for (var i = 0; i < a.length; i++){
  237. s = a[i];
  238. if (s.indexOf(".avi") !== -1 ||
  239. s.indexOf(".mp4") !== -1 ||
  240. s.indexOf(".flp") !== -1 ||
  241. s.indexOf(".mp3") !== -1 ||
  242. s.indexOf(".mpg") !== -1 ||
  243. s.indexOf(".mov") !== -1 ||
  244. s.indexOf(".mpeg") !== -1 ||
  245. s.indexOf(".jpg") !== -1 ||
  246. s.indexOf(".mkv") !== -1 ||
  247. s.indexOf(".png") !== -1 ||
  248. s.indexOf(".jpeg") !== -1 ||
  249. s.indexOf(".wmv") !== -1){
  250. log("bntDALL: " + a[i]);
  251. cdReq(a[i], origHref[i], uploadFolderId);
  252. //log(a[i]);
  253. }
  254. }
  255. log("bntDALL clicked end");
  256. });
  257.  
  258. function addGlobalStyle(css) {
  259. var head, style;
  260. head = document.getElementsByTagName('head')[0];
  261. if (!head) { return; }
  262. style = document.createElement('style');
  263. style.type = 'text/css';
  264. style.innerHTML = css;
  265. head.appendChild(style);
  266. }
  267.  
  268.  
  269. if (window.top === window.self) {
  270. //=========MAIN WINDOW=========//
  271. try{
  272. createFolder(folderName);
  273. bA = $("body h1").html();
  274. log(bA + nameB + " >body h1");
  275. }
  276. catch(err){bA = "";}
  277. if (!$("#iframeHive").length){
  278. var iframe = document.createElement('iframe');
  279. iframe.id = "iframeHive";
  280. iframe.src = "https://touch.hive.im/account/?3";
  281. iframe.style = "height: 0px; width: 0px; display: none; overflow:hidden";
  282. document.body.appendChild(iframe);
  283. $("#iframeHive").attr("style", "height: 0px; width: 0px; display: none; overflow:hidden");
  284. //$("#iframeHive").attr("style", "height: 400px; width: 600px; display: block; overflow:hidden");
  285. log("iframe created! " + nameB + ": " + location.href);
  286. }
  287. if (detectIndexPage === true && typeof bA !== "undefined" && bA.indexOf("Index of") !== -1){ // START HERE
  288. //toastr.info("Connecting to Hive....");
  289. $("body").append('<button type="button" id="bntDAll" title="Send Selected oo Hive" style="height: 2em; width: 12em;">Send Selected to Hive </button>');
  290. $("pre").attr("id", "selectable");
  291. $("pre").selectable({
  292. filter: ":not(:contains('/'))",
  293. });
  294. $("pre a:contains('/')").hover(function() { // HOVER OVER FOLDER
  295. $( "pre" ).selectable( "option", "distance", 50000 );
  296. });
  297. $("pre a:not(:contains('/'))").hover(function() {
  298. //$("pre").attr("id", "selectable");
  299. $( "pre" ).selectable( "option", "distance", 0 );
  300. });
  301. addGlobalStyle("#feedback { font-size: 1.4em; }\
  302. .ui-selecting { background: #FECA40; }\
  303. .ui-selected{ background: #F39814; color: white; }\
  304. #selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; }\
  305. #selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; }");
  306. var onceB = 0;
  307. var onceD = 0;
  308. setInterval(function(){
  309. //log("AA: " + auth);
  310. //log("AA: " + onceB);
  311. //log("AA: " + GM_getValue("auth"));
  312. if (onceD === 0 && typeof auth !== "undefined"){
  313. //GM_setValue("ready", "true");
  314. //GM_setValue("auth", auth);
  315. log("OLD: " + auth);
  316. onceD = 1;
  317. }
  318. if (onceB === 0 && GM_getValue("ready") == "true"){
  319. onceB = 1;
  320. auth = GM_getValue("auth");
  321. log("TRUE: " + GM_getValue("auth"));
  322. $("#iframeHive").remove();
  323. log("#iframeHive removed");
  324. //toastr.success("Hive Connected!");
  325. }
  326. }, 250);
  327.  
  328. $(document).on("click", "a", function(e){ // selector click event
  329. var linkA = $(this).attr("href");
  330. link = document.location.href + $(this).attr("href");
  331. log(linkA + nameB + " >link");
  332. if (linkA.indexOf(".avi") !== -1 ||
  333. linkA.indexOf(".mp4") !== -1 ||
  334. linkA.indexOf(".flp") !== -1 ||
  335. linkA.indexOf(".mp3") !== -1 ||
  336. linkA.indexOf(".mpg") !== -1 ||
  337. linkA.indexOf(".mov") !== -1 ||
  338. linkA.indexOf(".mpeg") !== -1 ||
  339. linkA.indexOf(".jpg") !== -1 ||
  340. linkA.indexOf(".mkv") !== -1 ||
  341. linkA.indexOf(".png") !== -1 ||
  342. linkA.indexOf(".jpeg") !== -1 ||
  343. linkA.indexOf(".wmv") !== -1) {
  344. e.preventDefault();
  345. log(linkA + nameB + " >link");
  346.  
  347. log("test >" + link);
  348.  
  349. $('body').prepend('<a href="#" class="hive">Hive</a>');
  350. $(".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');
  351. $(".hive").fadeOut("slow");
  352. cdReq(link, linkA, uploadFolderId);
  353. }
  354. });
  355. }
  356. else{
  357. try{
  358. $("#bntDAll").remove();
  359. }
  360. catch(err){
  361. log("Could not remove btnDall");
  362. }
  363. }
  364. }
  365. else
  366. {
  367. //=========IFRAME WINDOW=========//
  368. //GM_deleteValue("auth");
  369. try{
  370. auth = unsafeWindow.account.token;
  371. }
  372. catch(err){}
  373. var once = 0;
  374. setInterval(function(){ // EVENT FOR WHEN PAGE IS LOADED // RUNS ONCE
  375. //log($("#username").text() + nameB) + " >username";
  376. if (once === 0 && $("#username").text().indexOf("My Account") !== -1){
  377. once = 1;
  378. log("Iframe ready auth: " + auth);
  379. log("Iframe ready unsafeWindow.account.token: " + unsafeWindow.account.token);
  380. //if (auth !== unsafeWindow.account.token){
  381. log("auth !== unsafeWindow.account.token");
  382. //GM_deleteValue("auth");
  383. GM_setValue("auth", unsafeWindow.account.token);
  384. GM_setValue("ready", "true");
  385. //}
  386. //else{
  387. //auth = unsafeWindow.account.token;
  388. //GM_setValue("auth", unsafeWindow.account.token);
  389. //GM_setValue("ready", "true");
  390. //log("Iframe Post: ready");
  391. //}
  392. }
  393. else if (once === 1 && auth == "undefined"){
  394. GM_setValue("ready", "false");
  395. try{
  396. //auth = unsafeWindow.account.token;
  397. }
  398. catch(err){
  399. log("iframe: " + err);
  400. }
  401. }
  402. }, 200);
  403. }
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.