高亮关键词

高亮特定网页中感兴趣的关键词

当前为 2017-04-18 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name highlight-my-interest
  3. // @name:zh-CN 高亮关键词
  4. // @description highlight keywords in my favorites
  5. // @description:zh-CN 高亮特定网页中感兴趣的关键词
  6. // @version 0.1.7
  7. // @author jferroal
  8. // @license GPL-3.0
  9. // @grant none
  10. // @include https://sspai.com/*
  11. // @include https://toutiao.io/*
  12. // @include http://www.inoreader.com/*
  13. // @include http://www.52pojie.cn/*
  14. // @run-at document-end
  15. // @namespace https://greasyfork.org/users/34556-jferroal
  16. // ==/UserScript==
  17.  
  18. const JFInterestedKeywords = [
  19. /.*书籍.*/,
  20. /.*效率.*/gi,
  21. /.*google.*/gi,
  22. /.*Nexus.*/gi,
  23. /.*爬虫.*/,
  24. /.*python.*/gi,
  25. /.*angular.*/gi,
  26. /.*node.*/gi,
  27. /.*javascript.*/gi,
  28. /.*ukulele.*/gi,
  29. /.*GTD.*/gi,
  30. /.*工作流.*/gi,
  31. /.*日程.*/gi,
  32. /.*英雄联盟.*/gi,
  33. /.*VPS.*/gi,
  34. /.*服务器.*/gi,
  35. /.*书单.*/gi,
  36. /.*免费.*/gi,
  37. /.*限免.*/gi,
  38. /.*数据分析.*/gi,
  39. /.*自由职业.*/gi
  40. ];
  41.  
  42. let alreadyLoadedTimes = 0;
  43.  
  44. function getElements() {
  45. const targetTagNames = ['h1', 'h2', 'h3', 'h4', 'h5', 'p', 'a', 'pre', 'blockquote', 'summary'];
  46. return targetTagNames.reduce((res, tagName) => {
  47. const tags = document.getElementsByTagName(tagName);
  48. if (tags && tags.length) {
  49. res.push(...tags);
  50. }
  51. return res;
  52. }, []);
  53. }
  54.  
  55. function matchKeywordsInElements() {
  56. const elements = getElements();
  57. elements.forEach((element) => {
  58. JFInterestedKeywords.forEach((keyword) => {
  59. const isMatched = keyword.test(element.innerText);
  60. if (isMatched) {
  61. element.style.backgroundColor = '#FFDA5E';
  62. element.style.color = 'black';
  63. }
  64. });
  65. });
  66. alreadyLoadedTimes += 1;
  67. }
  68. const autoMatchInterval = setInterval(() => {
  69. if (alreadyLoadedTimes < 100) {
  70. matchKeywordsInElements();
  71. } else {
  72. clearInterval(autoMatchInterval);
  73. console.log('will no match any more');
  74. }
  75. }, 1000);