Only Show This User Posts

Only show the user posts you just want to read

  1. // ==UserScript==
  2. // @name Only Show This User Posts
  3. // @namespace qixinglu.com
  4. // @description Only show the user posts you just want to read
  5. // @grant GM_info
  6. // @grant GM_addStyle
  7. // @include http:///localhost/*
  8. // @version 0.0.1.20140517140355
  9. // ==/UserScript==
  10.  
  11. /* tools function */
  12.  
  13. var convert2RegExp = function(pattern) {
  14. var firstChar = pattern.charAt(0);
  15. var lastChat = pattern.charAt(pattern.length - 1);
  16. if (firstChar + lastChat === '//') {
  17. return new RegExp(s.substr(1, -1));
  18. } else {
  19. pattern = '^' + pattern.replace(/\*/g, '.*') + '$';
  20. return new RegExp(pattern);
  21. }
  22. };
  23.  
  24. var create_element_from_string = function(text) {
  25. var tag;
  26. if (text.indexOf('<td') === 0) {
  27. tag = 'tr';
  28. } else if (text.indexOf('<li') === 0) {
  29. tag = 'ul';
  30. } else {
  31. tag = 'div';
  32. }
  33. var parent_element = document.createElement(tag);
  34. parent_element.innerHTML = text;
  35. return parent_element.firstChild;
  36. }
  37.  
  38. /* ostup core */
  39.  
  40. var ostup = function(site) {
  41. var setup_site_default = function() {
  42. if (site.showOnlyLabel === undefined) {
  43. site.showOnlyLabel = 'only show this user';
  44. }
  45. if (site.showAllLabel === undefined) {
  46. site.showAllLabel = 'only all users';
  47. }
  48. if (site.positionInsert === undefined) {
  49. site.positionInsert = false;
  50. }
  51. }
  52.  
  53. var runtime = {
  54. hide_index: -1,
  55. post_display: 'block'
  56. }
  57.  
  58. var add_custom_style = function() {
  59. if (site.style !== undefined) {
  60. GM_addStyle(site.style);
  61. }
  62. }
  63.  
  64. var setup_post_display = function() {
  65. post = document.querySelector(site.post);
  66. if (post !== null) {
  67. runtime.post_display = window.getComputedStyle(post).display;
  68. }
  69.  
  70. }
  71.  
  72. var change_display = function(elements, start, length, value) {
  73. var end = start + length;
  74. for (var i = start; i < end; i += 1) {
  75. elements[i].style.display = value;
  76. }
  77. }
  78.  
  79. var do_filter = function() {
  80.  
  81. var posts = document.querySelectorAll(site.post);
  82. var usernames = document.querySelectorAll(site.username);
  83. var multiple = posts.length / usernames.length;
  84.  
  85. var show_only = function() {
  86. var hide_username = usernames[runtime.hide_index].textContent;
  87. for (var i = 0; i < usernames.length; i += 1) {
  88. var username = usernames[i];
  89. if (username.textContent !== hide_username) {
  90. change_display(posts, i * multiple, multiple, 'none');
  91. }
  92. }
  93. };
  94.  
  95. var show_all = function() {
  96. for (var i = 0; i < posts.length; i += 1) {
  97. change_display(posts, i, multiple, runtime.post_display);
  98. }
  99. };
  100.  
  101. if (runtime.hide_index === -1) {
  102. show_all(posts);
  103. } else {
  104. show_only(posts);
  105. }
  106. };
  107.  
  108. var get_button_text = function() {
  109. return runtime.hide_index === -1 ? site.showOnlyLabel : site.showAllLabel;
  110. };
  111.  
  112. var update_buttons = function() {
  113. var button_text = get_button_text();
  114. var buttons = document.querySelectorAll('a.ostup');
  115. for (var i = 0; i < buttons.length; i += 1) {
  116. buttons[i].textContent = button_text;
  117. }
  118. };
  119.  
  120. var add_buttons = function() {
  121.  
  122. var button_html = '<a class="ostup" href="javascript:void(0)"></a>';
  123.  
  124. var create_click_callback = function(i) {
  125. return function() {
  126. runtime.hide_index = runtime.hide_index === -1 ? i : -1;
  127. update_buttons();
  128. do_filter();
  129. }
  130. }
  131.  
  132. var positions = document.querySelectorAll(site.position);
  133. for (var i = 0; i < positions.length; i += 1) {
  134. var position = positions[i];
  135. if (site.positionInsert) {
  136. var button = position.parentNode.querySelector('a.ostup');
  137. } else {
  138. var button = position.querySelector('a.ostup');
  139. }
  140. if (button !== null) {
  141. continue;
  142. }
  143. button = create_element_from_string(button_html);
  144. button.textContent = get_button_text();
  145. button.addEventListener('click', create_click_callback(i));
  146.  
  147. if (site.container !== undefined) {
  148. var container = create_element_from_string(site.container);
  149. container.appendChild(button);
  150. button = container;
  151. }
  152. if (site.positionInsert) {
  153. position.parentNode.insertBefore(container, position.nextSibling);
  154. } else {
  155. position.appendChild(button);
  156. }
  157. }
  158. };
  159.  
  160. var control = {
  161. add_buttons: add_buttons,
  162. do_filter: do_filter
  163. };
  164.  
  165. add_custom_style();
  166. setup_post_display();
  167. add_buttons();
  168. return control;
  169. }
  170.  
  171. /* rule setup functions */
  172.  
  173. var select_site = function(sites) {
  174. var url = location.href;
  175. var i, j, reg, site, urls;
  176. for (i = 0; i < sites.length; i += 1) {
  177. site = sites[i];
  178.  
  179. // if is not a array, convert it to
  180. urls = (typeof(site.url) === 'string') ? [site.url] : site.url;
  181.  
  182. // start to find with site to use
  183. for (j = 0; j < urls.length; j += 1) {
  184. reg = convert2RegExp(urls[j]);
  185. if (reg.test(url)) {
  186. return site;
  187. }
  188. }
  189. }
  190. return null;
  191. };
  192.  
  193. // only run as main file
  194. if (GM_info.script.name === 'Only Show This User Posts') {
  195. var site = select_site(SITES);
  196. if (site !== null) {
  197. var control = ostup(site);
  198. document.addEventListener("LightPagerAppended", function() {
  199. control.add_buttons();
  200. control.do_filter();
  201. });
  202. }
  203. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址