Hacker News - Folding Subtrees

Fold and unfold arbitrary comment subtrees.

当前为 2015-09-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Hacker News - Folding Subtrees
  3. // @namespace https://github.com/pdkl95/user_scripts
  4. // @description Fold and unfold arbitrary comment subtrees.
  5. // @include https://news.ycombinator.com/item*
  6. // @version 1.1
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. /*
  11. hacker_news-folding_subtrees.user.js
  12. A user script to fold Hacker News.comments.
  13. Copyright (C) 2015 Brent Sanders
  14.  
  15. This program is free software: you can redistribute it and/or modify
  16. it under the terms of the GNU General Public License as published by
  17. the Free Software Foundation, either version 3 of the License, or
  18. (at your option) any later version.
  19.  
  20. This program is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. GNU General Public License for more details.
  24.  
  25. You should have received a copy of the GNU General Public License
  26. along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. */
  28.  
  29. (function () {
  30. var nesting_width = 40;
  31. var post_selector = '.athing';
  32. var indent_selector = '.ind';
  33. var header_selector = '.default div:nth-child(1)';
  34. var votebox_selector = 'tr td:nth-child(2)';
  35. var style_r90 = ".rotate90 { transform: rotate(90deg); }";
  36. var style_r270 = ".rotate270 { transform: rotate(270deg); }";
  37. var style_foldunfold = ".foldunfoldbtn { position: relative; top: -1px; }";
  38. var style_fold = '.foldbtn { opacity: 0.5; }';
  39. var style_unfold = '.unfoldbtn { opacity: 0.7; }';
  40. var style_ctlbox = '.foldctlbox { position: absolute; padding-left: 0.5em; }';
  41. var style_list = [style_r90, style_r270, style_foldunfold, style_fold, style_unfold, style_ctlbox];
  42. var extra_style_sheet = document.createElement('style');
  43. extra_style_sheet.type = 'text/css';
  44. extra_style_sheet.appendChild(document.createTextNode(style_list.join("\n")));
  45. var head = document.querySelector('head');
  46. head.appendChild(extra_style_sheet);
  47. var foldified_count = 0;
  48. function hide (el) {
  49. el.style.display = 'none';
  50. }
  51.  
  52. function show_element (el, mode) {
  53. if (el) {
  54. el.style.display = mode;
  55. }
  56. }
  57.  
  58. function show_block (el) { show_element(el, 'block' ); }
  59. function show_inline (el) { show_element(el, 'inline' ); }
  60. function show_inline_block (el) { show_element(el, 'inline-block'); }
  61.  
  62. function set_toggle_button_fold (btn) {
  63. btn.classList.add('foldbtn');
  64. btn.classList.add('rotate90');
  65. btn.classList.remove('unfoldbtn');
  66. btn.classList.remove('rotate270');
  67. }
  68. function set_toggle_button_unfold (btn) {
  69. btn.classList.remove('foldbtn');
  70. btn.classList.remove('rotate90');
  71. btn.classList.add('unfoldbtn');
  72. btn.classList.add('rotate270');
  73. }
  74.  
  75. function make_toggle_button (btn_type, rotation_class) {
  76. var btn = document.createElement('div');
  77. btn.classList.add('votearrow');
  78. btn.classList.add('foldunfoldbtn');
  79. set_toggle_button_fold(btn);
  80. show_inline_block(btn);
  81. return btn;
  82. }
  83. function each_subtree_post (post, func) {
  84. if (!post.hasOwnProperty('indent_width')) { return; }
  85.  
  86. var width = post.indent_width || 0;
  87. post = post.nextElementSibling;
  88.  
  89. while (post && post.hasOwnProperty('indent_width') && (post.indent_width > width)) {
  90. func(post);
  91. post = post.nextElementSibling;
  92. }
  93. }
  94.  
  95. function foldify_post(post_el, idx) {
  96. var ind_el = post_el.querySelector(indent_selector);
  97. if (!ind_el) { return; }
  98.  
  99. var spacer_img = ind_el.firstChild;
  100. if (!spacer_img) { return; }
  101. post_el.indent_width = spacer_img.width;
  102. var header_el = post_el.querySelector(header_selector);
  103. var votebox_el = post_el.querySelector(votebox_selector);
  104. var votepad_el = null;
  105. var body_br_el = null;
  106. var body_comment_el = null;
  107. var body_pad_el = null;
  108. if (header_el) {
  109. votepad_el = document.createElement('span');
  110. votepad_el.style.display = 'none';
  111.  
  112. body_pad_el = document.createElement('div');
  113. body_pad_el.classList.add('comment');
  114.  
  115. var comhead_el = header_el.querySelector('.comhead');
  116.  
  117. if (votebox_el) {
  118. var icon_width = votebox_el.clientWidth;
  119. votepad_el.style.width = icon_width;
  120.  
  121. if (comhead_el) {
  122. var comhead_width = comhead_el.clientWidth;
  123. body_pad_el.style.width = '' + (comhead_width + icon_width) + 'px';
  124. }
  125. }
  126. header_el.insertBefore(votepad_el, header_el.firstChild);
  127.  
  128. body_br_el = header_el.parentElement.children[1];
  129. body_comment_el = header_el.parentElement.children[2];
  130. header_el.parentElement.appendChild(body_pad_el);
  131. }
  132. var toggle_btn = make_toggle_button();
  133. post_el.fold_self = function () {
  134. post_el.is_folded = true;
  135.  
  136. set_toggle_button_unfold(toggle_btn);
  137. hide(votebox_el);
  138. show_inline_block(votepad_el);
  139. hide(body_br_el);
  140. hide(body_comment_el);
  141. show_block(body_pad_el);
  142. };
  143. post_el.unfold_self = function () {
  144. post_el.is_folded = false;
  145.  
  146. set_toggle_button_fold(toggle_btn);
  147. show_block(votebox_el);
  148. hide(votepad_el);
  149. show_inline(body_br_el);
  150. show_block(body_comment_el);
  151. hide(body_pad_el);
  152. };
  153. post_el.fold_subtree = function () {
  154. each_subtree_post(post_el, function (post) { post.fold_self(); });
  155. };
  156. post_el.unfold_subtree = function () {
  157. each_subtree_post(post_el, function (post) { post.unfold_self(); });
  158. };
  159.  
  160. post_el.fold = function () {
  161. post_el.fold_self();
  162. post_el.fold_subtree();
  163. };
  164. post_el.unfold = function () {
  165. post_el.unfold_self();
  166. post_el.unfold_subtree();
  167. };
  168. post_el.is_folded = false;
  169. post_el.toggle_fold = function () {
  170. if (post_el.is_folded) {
  171. post_el.unfold();
  172. } else {
  173. post_el.fold();
  174. }
  175. };
  176.  
  177. var toggle_event = function (event) {
  178. post_el.toggle_fold();
  179. };
  180. toggle_btn.addEventListener('click', toggle_event, false);
  181.  
  182. var ctl_box = document.createElement('div');
  183. ctl_box.classList.add('foldctlbox');
  184. ctl_box.appendChild(toggle_btn);
  185. show_inline_block(ctl_box);
  186. header_el.appendChild(ctl_box);
  187. foldified_count = foldified_count + 1;
  188. }
  189. function setup_posts () {
  190. var posts = document.querySelectorAll(post_selector);
  191. /*console.log('found ' + posts.length + ' posts');*/
  192. if (posts) {
  193. Array.prototype.forEach.call(posts, foldify_post);
  194. }
  195. }
  196. setup_posts();
  197. /*console.log("foldified " + foldified_count + ' posts');*/
  198. })();