AutoReblog2Queue

Make your Tumblr queue in a single key press.

当前为 2018-09-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name AutoReblog2Queue
  3. // @namespace https://github.com/KeyWeeUsr/Userscripts
  4. // @version 1.0
  5. // @description Make your Tumblr queue in a single key press.
  6. // @author Peter Badida
  7. // @copyright 2018+, Peter Badida
  8. // @license GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
  9. // @homepage https://github.com/KeyWeeUsr/Userscripts/tree/master/AutoReblog2Queue
  10. // @supportURL https://github.com/KeyWeeUsr/Userscripts/issues
  11. // @icon https://assets.tumblr.com/images/favicons/favicon.ico
  12. // @include https://www.tumblr.com/dashboard*
  13. // @include https://www.tumblr.com/dashboard/*
  14. // @grant unsafeWindow
  15. // @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=ACVM74AYCXVWQ
  16. // ==/UserScript==
  17. 'use strict';
  18.  
  19. /**
  20. * Autoreblog to Queue
  21. *
  22. * Userscript for Tumblr Dashboard which automatically changes the reblogging
  23. * option to "Add to queue" and reblogs it immediately.
  24. *
  25. * Basically this little script takes an advantage of the already available
  26. * behavior of Tumblr keyboard shortcuts i.e. navigating via J and K keys
  27. * together with R for reblogging. The script works in the background, checks
  28. * whether the modal window is present (waits if not), changes the reblogging
  29. * options and then presses the "Queue" button (changed "Reblog" button).
  30. *
  31. * Just use these navigation keys: J (up), K (down), L (heart), R (reblog).
  32. *
  33. * You can toggle the autoreblogging script on/off with the Ctrl + Q keys.
  34. * It's turned on by default.
  35. *
  36. * Also, if you deactivated the script and pressed R (reblog), you can always
  37. * activate it back with Ctrl + Q keys and the script will automatically add
  38. * the current post you are reblogging to the queue.
  39. *
  40. * Notify me if there's something missing/undesirable.
  41. */
  42.  
  43. var DEBUG = false;
  44.  
  45. /* how often to check for reblog modal */
  46. var INTERVAL = 500;
  47.  
  48.  
  49. function sleep(ms) {
  50. var i;
  51. var start = new Date().getTime();
  52.  
  53. while (i < 1e7) {
  54. if ((new Date().getTime() - start) > ms) {
  55. break;
  56. }
  57. i++;
  58. }
  59. }
  60.  
  61.  
  62. function reblog_to_queue() {
  63. var modal;
  64. var button;
  65. var arrow;
  66. var menu;
  67. var items;
  68. var i;
  69.  
  70. /* find the modal window and wait if not available */
  71. modal = document.getElementsByClassName("post-forms-modal")[0];
  72. button = modal.getElementsByClassName("post-form--save-button");
  73. if (!button.length) {
  74. return;
  75. }
  76.  
  77. /* modal window is available, reblog button is found, open the options */
  78. button = button[0];
  79. arrow = button.getElementsByClassName("icon_arrow_carrot_down")[0];
  80. arrow.click();
  81.  
  82. /* find the options menu element and pull the items out */
  83. menu = modal.getElementsByClassName("pop-menu")[0];
  84. items = menu.getElementsByClassName("item-option");
  85.  
  86. /* find the "Add to queue" option in the reblog options and select it */
  87. for (i = 0; i < items.length; i++) {
  88. if (items[i].innerText !== "Add to queue") {
  89. continue;
  90. }
  91. items[i].click();
  92. break;
  93. }
  94.  
  95. /* press the reblog button */
  96. button.getElementsByTagName("button")[0].click();
  97.  
  98. /**
  99. * and remove it from parent to prevent duplicates in the queue
  100. * from pressing the button multiple times via interval
  101. */
  102. button.parentNode.removeChild(button);
  103. }
  104.  
  105.  
  106. function bind_CtrlQ_keys() {
  107. var key;
  108.  
  109. document.body.addEventListener("keydown", function(eve) {
  110. if (DEBUG) {
  111. console.log(window.autoreblogscript);
  112. console.log(eve);
  113. }
  114.  
  115. /* get keycode from event */
  116. key = eve.which || eve.keyCode;
  117.  
  118. /* check if Q and Ctrl are pressed */
  119. if (eve.ctrlKey && key == 81) {
  120. if (window.autoreblogscript) {
  121. /* toggle off */
  122. clearInterval(window.autoreblogscript);
  123. window.autoreblogscript = null;
  124. /* make sure the interval is really stopped */
  125. console.log("[AutoReblog2Queue] off");
  126. } else {
  127. /* toggle on */
  128. window.autoreblogscript = setInterval(
  129. reblog_to_queue, INTERVAL
  130. );
  131. console.log("[AutoReblog2Queue] on");
  132. }
  133. }
  134. });
  135. }
  136.  
  137.  
  138. bind_CtrlQ_keys();
  139. window.autoreblogscript = setInterval(reblog_to_queue, INTERVAL);