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.0
  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. if (votebox_el) {
  116. var icon_width = votebox_el.clientWidth;
  117. votepad_el.style.width = icon_width;
  118. body_pad_el.style.width = '' + (header_el.clientWidth + icon_width) + 'px';
  119. }
  120. header_el.insertBefore(votepad_el, header_el.firstChild);
  121.  
  122. body_br_el = header_el.parentElement.children[1];
  123. body_comment_el = header_el.parentElement.children[2];
  124. header_el.parentElement.appendChild(body_pad_el);
  125. }
  126. var toggle_btn = make_toggle_button();
  127. post_el.fold_self = function () {
  128. post_el.is_folded = true;
  129.  
  130. set_toggle_button_unfold(toggle_btn);
  131. hide(votebox_el);
  132. show_inline_block(votepad_el);
  133. hide(body_br_el);
  134. hide(body_comment_el);
  135. show_block(body_pad_el);
  136. };
  137. post_el.unfold_self = function () {
  138. post_el.is_folded = false;
  139.  
  140. set_toggle_button_fold(toggle_btn);
  141. show_block(votebox_el);
  142. hide(votepad_el);
  143. show_inline(body_br_el);
  144. show_block(body_comment_el);
  145. hide(body_pad_el);
  146. };
  147. post_el.fold_subtree = function () {
  148. each_subtree_post(post_el, function (post) { post.fold_self(); });
  149. };
  150. post_el.unfold_subtree = function () {
  151. each_subtree_post(post_el, function (post) { post.unfold_self(); });
  152. };
  153.  
  154. post_el.fold = function () {
  155. post_el.fold_self();
  156. post_el.fold_subtree();
  157. };
  158. post_el.unfold = function () {
  159. post_el.unfold_self();
  160. post_el.unfold_subtree();
  161. };
  162. post_el.is_folded = false;
  163. post_el.toggle_fold = function () {
  164. if (post_el.is_folded) {
  165. post_el.unfold();
  166. } else {
  167. post_el.fold();
  168. }
  169. };
  170.  
  171. var toggle_event = function (event) {
  172. post_el.toggle_fold();
  173. };
  174. toggle_btn.addEventListener('click', toggle_event, false);
  175.  
  176. var ctl_box = document.createElement('div');
  177. ctl_box.classList.add('foldctlbox');
  178. ctl_box.appendChild(toggle_btn);
  179. show_inline_block(ctl_box);
  180. header_el.appendChild(ctl_box);
  181. foldified_count = foldified_count + 1;
  182. }
  183. function setup_posts () {
  184. var posts = document.querySelectorAll(post_selector);
  185. /*console.log('found ' + posts.length + ' posts');*/
  186. if (posts) {
  187. Array.prototype.forEach.call(posts, foldify_post);
  188. }
  189. }
  190. setup_posts();
  191. /*console.log("foldified " + foldified_count + ' posts');*/
  192. })();