Greasy Fork 还支持 简体中文。

highlight-my-interest

highlight keywords in my favorites

目前為 2017-08-01 提交的版本,檢視 最新版本

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