List Filter

Highlights, Lowlights, or Deletes page elements based on their text.

当前为 2019-06-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name List Filter
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Highlights, Lowlights, or Deletes page elements based on their text.
  6. // @author listfilterEric
  7. // @match *://news.google.com/*
  8. // @match *://www.youtube.com/*
  9. // @match *://www.reddit.com/r/*
  10. // @grant none
  11. // @require http://code.jquery.com/jquery-1.12.4.min.js
  12. // @require https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
  13. // @licence CC-BY-NC-SA-4.0; https://creativecommons.org/licenses/by-nc-sa/4.0/
  14. // @licence GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
  15. // ==/UserScript==
  16. /*jshint esversion: 6 */
  17.  
  18. /* Last update: 6-05-2019 */
  19.  
  20. /*
  21. Search for these markers for locations where you need to add new selectors for new sites.
  22. markA (select the text block)
  23. markB (select the block you want to apply css to)
  24. markC (css of each class)
  25. */
  26.  
  27. (function() {
  28. 'use strict';
  29.  
  30. const lfShowFilterButton = 0; // set to 1 to show a button to manually run this script.
  31. const lfDynamicChecking = 1; // set to 1 to run the script automatically when new block elements are detected.
  32. const lfShowConsoleMsg = 0; // set to 1 to show console messages about how the filter checked each block.
  33.  
  34. function consolelog(text) {
  35. if (lfShowConsoleMsg) {
  36. console.log(text);
  37. }
  38. }
  39. consolelog("list filter script start.");
  40.  
  41. // ==== filter lists ====
  42.  
  43. //strong highlight
  44. var highlight1 = [
  45. /apple/i,
  46. ];
  47. //weak highlight
  48. var highlight2 = [
  49. /gamestop/i,
  50. ];
  51. //weak lowlight
  52. var lowlight1 = [
  53. /goodbye/i,
  54. ];
  55. //strong lowlight
  56. var lowlight2 = [
  57. /\btv\b/i,
  58. ];
  59. //delete block
  60. var delete1 = [
  61. /throne|dany|\bgot\b/i,
  62. ];
  63.  
  64. var domainName = window.location.hostname; //href might be less limiting
  65. var hrefString = window.location.href;
  66.  
  67. // ==== function definitions ====
  68.  
  69. // checks string against given list of regular expressions.
  70. function checkReg(string, regList) {
  71. for (var i = 0; i < regList.length; i++) {
  72. if (regList[i].test(string)) {
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78.  
  79. var elementCounter = 0;
  80.  
  81. // check attributes of a block to choose what class to add to it.
  82. function checkBlock(index, element) {
  83.  
  84. var titleText;
  85.  
  86. // markA start
  87. if (/news\.google\.com/gi.test(domainName)) {
  88. titleText = jQuery(this).find("div>article>h3").eq(0).text().trim();
  89. }else if (/www\.youtube\.com/gi.test(domainName)) {
  90. titleText = jQuery(this).find("span#video-title").text().trim();
  91. }else if (/www\.reddit\.com/gi.test(domainName)) {
  92. titleText = jQuery(this).find("span>a>h2").text().trim();
  93. }
  94. // markA end
  95.  
  96. // class list identifies what was checked in past iterations
  97. var classList = "";
  98. if ( jQuery(this).attr("class") ) {
  99. classList = jQuery(this).attr("class");
  100. //consolelog(classList);
  101. }
  102.  
  103. if ( titleText && (!classList || !/\blf-/.test(classList)) ) {
  104. //if ( titleText ) {
  105. elementCounter++;
  106. var matchCode = "n1";
  107.  
  108. // delete and lowlight lists are checked before highlight lists
  109. if (checkReg(titleText,delete1)) {
  110. matchCode = "d1";
  111. jQuery(this).remove();
  112. }else if (checkReg(titleText,lowlight2)) {
  113. matchCode = "l2";
  114. jQuery(this).addClass("lf-lowlight2");
  115. }else if (checkReg(titleText,lowlight1)) {
  116. matchCode = "l1";
  117. jQuery(this).addClass("lf-lowlight1");
  118. }else if (checkReg(titleText,highlight1)) {
  119. matchCode = "h1";
  120. jQuery(this).addClass("lf-highlight1");
  121. }else if (checkReg(titleText,highlight2)) {
  122. matchCode = "h2";
  123. jQuery(this).addClass("lf-highlight2");
  124. }else {
  125. jQuery(this).addClass("lf-checked");
  126. }
  127.  
  128. // prints to console the outcome of what the block matched.
  129. //if (1) {
  130. if (/d|l/gi.test(matchCode)) {
  131. var printMsg = elementCounter;
  132. printMsg += " "+ matchCode;
  133. printMsg += ": "+ titleText;
  134. consolelog(printMsg);
  135. }
  136. //console.log(elementCounter +" "+ matchCode +": "+ titleText +"("+ vidLength +")");
  137. }
  138.  
  139. } // end function checkBlock()
  140.  
  141. // ==== adds reset button for highlight/lowlight and runs initial execution. ====
  142.  
  143. // markB start
  144. var blockSelector = "article";
  145. if(/news\.google\.com/gi.test(domainName)) {
  146. blockSelector = "main>c-wiz>div>div";
  147. checkBlocks();
  148. }else if (/www\.youtube\.com/gi.test(domainName)) {
  149. blockSelector = "ytd-compact-video-renderer";
  150. checkBlocks();
  151. }else if (/www\.reddit\.com/gi.test(domainName)) {
  152. blockSelector = ".scrollerItem>div>div:nth-child(2)";
  153. checkBlocks();
  154. }
  155. // markB end
  156.  
  157. function checkBlocks() {
  158. jQuery(blockSelector).each(checkBlock);
  159. consolelog("end checkBlocks().");
  160. }
  161.  
  162. jQuery("#lf-reset").click(function() {
  163. consolelog("highlight/lowlight reset began.");
  164. elementCounter = 0;
  165. checkBlocks();
  166. consolelog("highlight/lowlight reset finished.");
  167. });
  168.  
  169. if (lfDynamicChecking) {
  170. waitForKeyElements( blockSelector, checkBlocks );
  171. }else {
  172. checkBlocks();
  173. }
  174.  
  175. const listFilterCss =
  176. `<style>
  177. #lf-reset {
  178. display: block !important;
  179. background: #177e14;
  180. padding: 5px;
  181. border-radius: 5px;
  182. position: fixed;
  183. top: 5px;
  184. right: 5px;
  185. z-index: 999;
  186. color: white;
  187. font-weight: bold;
  188. cursor: pointer;
  189. }
  190. </style>`;
  191. if (lfShowFilterButton) {
  192. jQuery("body").prepend('<div id="lf-reset" hidden>H/L</div>');
  193. jQuery(document.body).append(listFilterCss);
  194. }
  195.  
  196. // markC start
  197. const googleFilterCss =
  198. `<style>
  199. main>c-wiz>div>div.lf-highlight1 {
  200. background: #baffc9;/*green*/
  201. }
  202. main>c-wiz>div>div.lf-highlight2 {
  203. background: #ffffba;/*yellow*/
  204. }
  205. main>c-wiz>div>div.lf-lowlight1 {
  206. background: #ffdfba;/*orange*/
  207. opacity: .3;
  208. }
  209. main>c-wiz>div>div.lf-lowlight2 {
  210. background: #ffccca;/*red*/
  211. opacity: .5;
  212. }
  213. </style>`;
  214. const youtubeFilterCss =
  215. `<style>
  216. ytd-compact-video-renderer.lf-highlight1 {
  217. background: #baffc9;/*green*/
  218. }
  219. ytd-compact-video-renderer.lf-highlight2 {
  220. background: #ffffba;/*yellow*/
  221. }
  222. ytd-compact-video-renderer.lf-lowlight1 {
  223. background: #ffdfba;/*orange*/
  224. opacity: .3;
  225. }
  226. ytd-compact-video-renderer.lf-lowlight2 {
  227. background: #ffccca;/*red*/
  228. opacity: .5;
  229. }
  230. </style>`;
  231. const redditFilterCss =
  232. `<style>
  233. .scrollerItem>div>div:nth-child(2).lf-highlight1 {
  234. background: #baffc9;/*green*/
  235. }
  236. .scrollerItem>div>div:nth-child(2).lf-highlight2 {
  237. background: #ffffba;/*yellow*/
  238. }
  239. .scrollerItem>div>div:nth-child(2).lf-lowlight1 {
  240. background: #ffdfba;/*orange*/
  241. opacity: .3;
  242. }
  243. .scrollerItem>div>div:nth-child(2).lf-lowlight2 {
  244. background: #ffccca;/*red*/
  245. opacity: .5;
  246. }
  247. </style>`;
  248. if(/news\.google\.com/gi.test(domainName)) {
  249. jQuery(document.body).append(googleFilterCss);
  250. }else if (/www\.youtube\.com/gi.test(domainName)) {
  251. jQuery(document.body).append(youtubeFilterCss);
  252. }else if (/www\.reddit\.com/gi.test(domainName)) {
  253. jQuery(document.body).append(redditFilterCss);
  254. }
  255. // markC end
  256.  
  257. consolelog("list filter script finished.");
  258. })();