高亮关键词

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

当前为 2017-08-03 提交的版本,查看 最新版本

  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.2.1
  7. // @author jferroal
  8. // @license GPL-3.0
  9. // @grant none
  10. // @require https://greasyfork.org/scripts/31793-jmul/code/JMUL.js?version=209567
  11. // @include https://sspai.com/*
  12. // @include https://toutiao.io/*
  13. // @include http://www.inoreader.com/*
  14. // @run-at document-end
  15. // @namespace https://greasyfork.org/users/34556-jferroal
  16. // ==/UserScript==
  17.  
  18. (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  19. const KeywordService = require('./keyword.service');
  20. const TextElement = require('./text-element');
  21. const MaxDetectTime = 10000;
  22. const DetectIntervalTime = 2000;
  23.  
  24. (function () {
  25. let highlightedCount = 0;
  26. runHighlight();
  27. window.addEventListener('scroll', () => {
  28. runHighlight();
  29. });
  30. function runHighlight() {
  31. const elements = TextElement.findAll();
  32. if (elements.length === highlightedCount) return;
  33. KeywordService.list().then((keywords) => {
  34. TextElement.setKeywords(keywords);
  35. elements.map((e) => e.detect().highlight());
  36. highlightedCount = elements.length;
  37. });
  38. }
  39. })();
  40.  
  41. },{"./keyword.service":2,"./text-element":3}],2:[function(require,module,exports){
  42. const InterestedKeywords = [
  43. "书籍",
  44. "效率",
  45. "google.*?",
  46. "nexus.*?",
  47. "爬虫",
  48. "python.*?",
  49. "angular.*?",
  50. "node",
  51. "javascript",
  52. "ukulele",
  53. /gtd.*?/gi,
  54. "工作流",
  55. "日程",
  56. "英雄联盟",
  57. "vps",
  58. "服务器",
  59. "书单",
  60. "免费",
  61. "限免",
  62. "数据分析",
  63. "自由职业",
  64. "lol",
  65. "react",
  66. "mobx",
  67. ];
  68.  
  69. class KeywordService {
  70. static list(forceLoad = false) {
  71. return new Promise((resolve) => {
  72. if (forceLoad) {
  73. // load from server;
  74. }
  75. resolve(KeywordService.InterestedKeywords);
  76. });
  77.  
  78. }
  79. }
  80. KeywordService.InterestedKeywords = InterestedKeywords;
  81.  
  82. module.exports = KeywordService;
  83. },{}],3:[function(require,module,exports){
  84. let JMUL = window.JMUL || {};
  85. const Map = (list, fn) => {
  86. let result = [];
  87. if (list && list.length) {
  88. for (let i = 0; i < list.length; i += 1) {
  89. result.push(fn(list[i]));
  90. }
  91. }
  92. return result;
  93. };
  94.  
  95. class TextElement {
  96. constructor(element) {
  97. this.element = new JMUL.Element(element);
  98. this.innerText = this.element.innerText;
  99. this.shouldHighlight = false;
  100. }
  101.  
  102. detect() {
  103. for (const keyword of TextElement.keywords) {
  104. const keywordPattern = new RegExp(keyword, 'gi');
  105. if (keywordPattern.test(this.innerText)) {
  106. this.shouldHighlight = true;
  107. break;
  108. }
  109. }
  110. return this;
  111. }
  112.  
  113. highlight() {
  114. if (this.shouldHighlight) {
  115. this.element.setCss({
  116. backgroundColor: '#FFDA5E',
  117. color: 'black',
  118. });
  119. }
  120. }
  121.  
  122. static setKeywords(keywords) {
  123. TextElement.keywords = keywords;
  124. }
  125.  
  126. static findAll() {
  127. return TextElement.targetTagNames.reduce((res, tagName) => {
  128. const tags = document.getElementsByTagName(tagName);
  129. return res.concat(Map(tags, (e) => new TextElement(e)));
  130. }, []);
  131. }
  132. }
  133.  
  134. TextElement.targetTagNames = ['h1', 'h2', 'h3', 'h4', 'h5', 'p', 'a', 'pre', 'blockquote', 'summary'];
  135. module.exports = TextElement;
  136. },{}]},{},[1]);