高亮关键词

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

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