reddit-saved-category

Change the category of saved Reddit posts more easily

  1. // ==UserScript==
  2. // @name reddit-saved-category
  3. // @namespace http://mindofthomas.com/
  4. // @version 1.0.1
  5. // @description Change the category of saved Reddit posts more easily
  6. // @author MindOfThomas
  7. // @grant none
  8. // @match http://www.reddit.com/user/*/saved*
  9. // @match https://www.reddit.com/user/*/saved*
  10. // ==/UserScript==
  11. 'use strict';
  12. if(!$) {
  13. console.log("reddit-saved-category: can't find jQuery");
  14. return;
  15. }
  16. var saveQueue = [];
  17. var selectedCategory = '';
  18. var categories = ['no category'];
  19. var modhash = '';
  20. if(r.config.gold) {
  21. init();
  22. }
  23.  
  24. function init() {
  25. jQuery.ajax({
  26. type: 'GET',
  27. url: '/api/me.json',
  28. dataType: 'json',
  29. contentType: 'application/x-www-form-urlencoded'
  30. }).done(function(response) {
  31. modhash = response.data.modhash;
  32. getCategories();
  33. });
  34. }
  35. function makeSaveBox() {
  36. var container = document.createElement('div');
  37. container.classList.add('spacer');
  38.  
  39. var select = document.createElement('select');
  40. select.id = 'rsc-select';
  41. select.addEventListener('change', selectChange);
  42.  
  43. var option, optionText;
  44. for(var i = 0; i < categories.length; i++) {
  45. option = document.createElement('option');
  46. optionText = document.createTextNode(categories[i]);
  47. option.appendChild(optionText);
  48. select.appendChild(option);
  49. }
  50.  
  51. var button = document.createElement('input');
  52. button.type = 'submit';
  53. button.value = 'save post(s)';
  54. button.addEventListener('click', saveButton);
  55.  
  56. container.appendChild(select);
  57. container.appendChild(button);
  58. $('div.spacer').has('span:contains("filter by category")').after(container);
  59. }
  60. function addCheckboxes() {
  61. var postId, likesContainer, div, input, self;
  62. $('div.thing.saved').each(function() {
  63. postId = $(this).attr('id').replace('thing_', '');
  64. likesContainer = $(this).find('div.midcol').has('div.arrow');
  65. div = document.createElement('div');
  66. div.classList.add('midcol');
  67. div.setAttribute('style', 'margin-left: 7px !important; margin-right: 7px !important;');
  68. div.style.textAlign = 'center';
  69. input = document.createElement('input');
  70. input.type = 'checkbox';
  71. input.id = postId;
  72. input.addEventListener('change', checkboxChange);
  73. div.appendChild(input);
  74. likesContainer.before(div);
  75. });
  76. }
  77. function checkboxChange(e) {
  78. if(e.target.checked && saveQueue.indexOf(e.target.id) <= -1) {
  79. saveQueue.push(e.target.id);
  80. } else if(!e.target.checked && saveQueue.indexOf(e.target.id) >= 0) {
  81. saveQueue.splice(saveQueue.indexOf(e.target.id), 1);
  82. }
  83. }
  84. function saveButton(e) {
  85. e.stopPropagation();
  86. e.preventDefault();
  87. save();
  88. }
  89. function save() {
  90. if(saveQueue.length <= 0) {
  91. return;
  92. }
  93. var theData = {'id': saveQueue[0]};
  94. if(selectedCategory !== 'no category') {
  95. theData['category'] = selectedCategory;
  96. }
  97.  
  98. jQuery.ajax({
  99. type: 'POST',
  100. url: '/api/save',
  101. headers: {'x-modhash': modhash},
  102. data: theData,
  103. success: save
  104. });
  105. saveQueue.shift();
  106. }
  107. function getCategories() {
  108. jQuery.ajax({
  109. type: 'GET',
  110. url: '/api/saved_categories.json',
  111. dataType: 'json'
  112. }).done(function(response) {
  113. for(var i = 0; i < response.categories.length; i++) {
  114. categories.push(response.categories[i].category);
  115. }
  116. makeSaveBox();
  117. addCheckboxes();
  118. });
  119. }
  120. function selectChange() {
  121. var select = document.getElementById('rsc-select');
  122. selectedCategory = select.options[select.selectedIndex].text;
  123. }