高亮关键词

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

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

  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. let JMUL = window.JMUL || {};
  20.  
  21. const Map = (list, fn) => {
  22. let result = [];
  23. if (list && list.length) {
  24. for (let i = 0; i < list.length; i += 1) {
  25. result.push(fn(list[i]));
  26. }
  27. }
  28. return result;
  29. };
  30.  
  31. class TextElement {
  32. constructor(element) {
  33. this.element = new JMUL.Element(element);
  34. this.innerText = this.element.innerText;
  35. this.shouldHighlight = false;
  36. }
  37.  
  38. detect() {
  39. for (const keyword of TextElement.keywords) {
  40. const keywordPattern = new RegExp(keyword, 'gi');
  41. if (keywordPattern.test(this.innerText)) {
  42. this.shouldHighlight = true;
  43. break;
  44. }
  45. }
  46. return this;
  47. }
  48.  
  49. highlight() {
  50. if (this.shouldHighlight) {
  51. this.element.setCss(TextElement.highlightStyle);
  52. }
  53. }
  54.  
  55. static init(setting) {
  56. TextElement.highlightStyle = {
  57. background: setting.highlightBgColor,
  58. color: setting.highlightTxtColor,
  59. };
  60. }
  61.  
  62. static setKeywords(keywords) {
  63. TextElement.keywords = keywords;
  64. }
  65.  
  66. static findAll() {
  67. return TextElement.targetTagNames.reduce((res, tagName) => {
  68. const tags = document.getElementsByTagName(tagName);
  69. return res.concat(Map(tags, (e) => new TextElement(e)));
  70. }, []);
  71. }
  72. }
  73.  
  74. TextElement.targetTagNames = ['h1', 'h2', 'h3', 'h4', 'h5', 'p', 'a', 'pre', 'blockquote', 'summary'];
  75. module.exports = TextElement;
  76. },{}],2:[function(require,module,exports){
  77. const KeywordService = require('./keyword.service');
  78. const SettingService = require('./setting.service');
  79. console.log('Setting Service', SettingService, SettingService.init);
  80. const TextElement = require('./element');
  81.  
  82. const Config = {};
  83.  
  84. (function () {
  85. let highlightedCount = 0;
  86. loadSetting().then((setting) => {
  87. KeywordService.init(setting);
  88. TextElement.init(setting);
  89. highlight()
  90. });
  91. window.addEventListener('scroll', highlight);
  92.  
  93. function loadSetting() {
  94. SettingService.init(Config);
  95. return SettingService.load();
  96. }
  97.  
  98. function highlight() {
  99. const elements = TextElement.findAll();
  100. if (elements.length === highlightedCount) return;
  101. KeywordService.list().then((keywords) => {
  102. TextElement.setKeywords(keywords);
  103. elements.map((e) => e.detect().highlight());
  104. highlightedCount = elements.length;
  105. });
  106. }
  107. })();
  108.  
  109. },{"./element":1,"./keyword.service":3,"./setting.service":5}],3:[function(require,module,exports){
  110. const DefaultKeywords = [
  111. "书籍",
  112. "效率",
  113. "google.*?",
  114. "nexus.*?",
  115. "爬虫",
  116. "python.*?",
  117. "angular.*?",
  118. "node",
  119. "javascript",
  120. "ukulele",
  121. /gtd.*?/gi,
  122. "工作流",
  123. "日程",
  124. "英雄联盟",
  125. "vps",
  126. "服务器",
  127. "书单",
  128. "免费",
  129. "限免",
  130. "数据分析",
  131. "自由职业",
  132. "lol",
  133. "react",
  134. "mobx",
  135. ];
  136.  
  137. class KeywordService {
  138. static init(setting) {
  139. KeywordService.Setting = setting;
  140. }
  141.  
  142. static list() {
  143. const hasLoadKeywords = KeywordService.Setting.keywords && KeywordService.Setting.keywords.length;
  144. return Promise.resolve(hasLoadKeywords && KeywordService.Setting.keywords || KeywordService.DefaultKeywords);
  145. }
  146. }
  147.  
  148. KeywordService.DefaultKeywords = DefaultKeywords;
  149.  
  150. module.exports = KeywordService;
  151. },{}],4:[function(require,module,exports){
  152. class Setting {
  153. constructor(jsonBody) {
  154. Object.assign(this, jsonBody);
  155. }
  156. }
  157.  
  158. module.exports = {Setting};
  159. },{}],5:[function(require,module,exports){
  160. const Setting = require('./setting').Setting;
  161. const {Request} = window.JMUL || {JMUL: {}};
  162.  
  163. const DefaultResponseHandler = (_response) => {
  164. let response = _response;
  165. if (typeof _response === 'object' && _response.responseText) {
  166. response = _response.responseText;
  167. }
  168. return new Setting(JSON.parse(response));
  169. };
  170.  
  171. class SettingService {
  172. static init(config) {
  173. SettingService.loadUrl = config.loadUrl;
  174. SettingService.method = config.method || 'GET';
  175. SettingService.contentType = config.contentType || 'application/json';
  176. SettingService.data = config.data || {};
  177. SettingService.resHandler = config.resHandler || DefaultResponseHandler;
  178. }
  179. static load() {
  180. if (!SettingService.loadUrl) return Promise.resolve(SettingService.DefaultSetting);
  181. const request = new Request({headers: {'content-type': SettingService.contentType}});
  182. request.setUrl(SettingService.loadUrl);
  183. request.setMethod(SettingService.method);
  184. request.setData(SettingService.data);
  185. return request.send().then((response) => {
  186. return SettingService.resHandler(response.responseText);
  187. });
  188. }
  189. }
  190.  
  191. SettingService.DefaultSetting = {
  192. highlightBgColor: '#FFDA5E',
  193. highlightTxtColor: 'black',
  194. };
  195.  
  196. module.exports = SettingService;
  197. },{"./setting":4}]},{},[2]);