高亮关键词

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

当前为 2017-07-28 提交的版本,查看 最新版本

  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.8
  7. // @author jferroal
  8. // @license GPL-3.0
  9. // @grant none
  10. // @require https://greasyfork.org/scripts/31793-jmul/code/JMUL.js?version=208363
  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 alreadyLoadedTimes = 0;
  26. let handledCount = 0;
  27. const autoMatchInterval = setInterval(() => {
  28. if (alreadyLoadedTimes < MaxDetectTime) {
  29. alreadyLoadedTimes += 1;
  30. const elements = TextElement.findAll();
  31. if (elements.length === handledCount) return;
  32. KeywordService.list().then(function (keywords) {
  33. TextElement.setKeywords(keywords);
  34. handledCount += elements.length;
  35. elements.map((e) => e.detect().highlight());
  36. });
  37. } else {
  38. clearInterval(autoMatchInterval);
  39. console.log('will no match any more');
  40. }
  41. }, DetectIntervalTime);
  42. })();
  43.  
  44. },{"./keyword.service":2,"./text-element":3}],2:[function(require,module,exports){
  45. const InterestedKeywords = [
  46. "书籍",
  47. "效率",
  48. "google",
  49. "nexus",
  50. "爬虫",
  51. "python",
  52. "angular",
  53. "node",
  54. "javascript",
  55. "ukulele",
  56. "gtd",
  57. "工作流",
  58. "日程",
  59. "英雄联盟",
  60. "vps",
  61. "服务器",
  62. "书单",
  63. "免费",
  64. "限免",
  65. "数据分析",
  66. "自由职业",
  67. "lol",
  68. "react",
  69. "mobx",
  70. ];
  71.  
  72. class KeywordService {
  73. static list(forceLoad = false) {
  74. return new Promise((resolve) => {
  75. if (forceLoad) {
  76. // load from server;
  77. }
  78. resolve(KeywordService.InterestedKeywords);
  79. });
  80.  
  81. }
  82. }
  83. KeywordService.InterestedKeywords = InterestedKeywords;
  84.  
  85. module.exports = KeywordService;
  86. },{}],3:[function(require,module,exports){
  87. let JMUL = window.JMUL || {};
  88. const Map = (list, fn) => {
  89. let result = [];
  90. if (list && list.length) {
  91. for (let i = 0; i < list.length; i += 1) {
  92. result.push(fn(list[i]));
  93. }
  94. }
  95. return result;
  96. };
  97.  
  98. class TextElement {
  99. constructor(element) {
  100. this.element = new JMUL.Element(element);
  101. this.shouldHighlight = false;
  102. }
  103.  
  104. detect() {
  105. for (const keyword of TextElement.keywords) {
  106. const innerText = this.element.element.innerText.toLocaleLowerCase();
  107. if (innerText.indexOf(keyword) > -1) {
  108. this.shouldHighlight = true;
  109. break;
  110. }
  111. }
  112. return this;
  113. }
  114.  
  115. highlight() {
  116. if (this.shouldHighlight) {
  117. this.element.setCss({
  118. backgroundColor: '#FFDA5E',
  119. color: 'black',
  120. });
  121. }
  122. }
  123.  
  124. static setKeywords(keywords) {
  125. TextElement.keywords = keywords;
  126. }
  127.  
  128. static findAll() {
  129. return TextElement.targetTagNames.reduce((res, tagName) => {
  130. const tags = document.getElementsByTagName(tagName);
  131. return res.concat(Map(tags, (e) => new TextElement(e)));
  132. }, []);
  133. }
  134. }
  135.  
  136. TextElement.targetTagNames = ['h1', 'h2', 'h3', 'h4', 'h5', 'p', 'a', 'pre', 'blockquote', 'summary'];
  137. module.exports = TextElement;
  138. },{}]},{},[1]);