高亮关键词

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

当前为 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.2
  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. const TextElement = require('./element');
  80.  
  81. const Config = {};
  82.  
  83. (function () {
  84. let highlightedCount = 0;
  85. // const href = window.location.href;
  86. const href = 'https://sspai.com';
  87. loadSetting().then((setting) => {
  88. KeywordService.init(setting, href);
  89. TextElement.init(setting);
  90. highlight()
  91. });
  92. window.addEventListener('scroll', highlight);
  93.  
  94. function loadSetting() {
  95. SettingService.init(Config);
  96. return SettingService.load();
  97. }
  98.  
  99. function highlight() {
  100. const elements = TextElement.findAll();
  101. if (elements.length === highlightedCount) return;
  102. KeywordService.list().then((keywords) => {
  103. TextElement.setKeywords(keywords);
  104. elements.map((e) => e.detect().highlight());
  105. highlightedCount = elements.length;
  106. });
  107. }
  108. })();
  109.  
  110. },{"./element":1,"./keyword.service":3,"./setting.service":5}],3:[function(require,module,exports){
  111. class KeywordService {
  112. static init(setting, href) {
  113. KeywordService.Setting = setting;
  114. const sites = Object.keys(KeywordService.Setting.keywords);
  115. if (sites && sites.length) {
  116. KeywordService.keywords = sites.reduce((res, site) => {
  117. const sitePattern = new RegExp(site, 'gi');
  118. return res.concat(sitePattern.test(href) && KeywordService.Setting.keywords[site] || [])}, []);
  119. }
  120. }
  121.  
  122. static list() {
  123. return Promise.resolve(KeywordService.keywords);
  124. }
  125. }
  126.  
  127. module.exports = KeywordService;
  128. },{}],4:[function(require,module,exports){
  129. class Setting {
  130. constructor(jsonBody) {
  131. Object.assign(this, jsonBody);
  132. }
  133. }
  134.  
  135. module.exports = {Setting};
  136. },{}],5:[function(require,module,exports){
  137. const Setting = require('./setting').Setting;
  138. const {Request} = window.JMUL || {JMUL: {}};
  139.  
  140. const DefaultKeywords = [
  141. "书籍",
  142. "效率",
  143. "google.*?",
  144. "nexus.*?",
  145. "爬虫",
  146. "python.*?",
  147. "angular.*?",
  148. "node",
  149. "javascript",
  150. "ukulele",
  151. /gtd.*?/gi,
  152. "工作流",
  153. "日程",
  154. "英雄联盟",
  155. "vps",
  156. "服务器",
  157. "书单",
  158. "免费",
  159. "限免",
  160. "数据分析",
  161. "自由职业",
  162. "lol",
  163. "react",
  164. "mobx",
  165. ];
  166.  
  167. const DefaultResponseHandler = (_response) => {
  168. let response = _response;
  169. if (typeof _response === 'object' && _response.responseText) {
  170. response = _response.responseText;
  171. }
  172. return new Setting(JSON.parse(response));
  173. };
  174.  
  175. class SettingService {
  176. static init(config) {
  177. SettingService.loadUrl = config.loadUrl;
  178. SettingService.method = config.method || 'GET';
  179. SettingService.contentType = config.contentType || 'application/json';
  180. SettingService.data = config.data || {};
  181. SettingService.resHandler = config.resHandler || DefaultResponseHandler;
  182. }
  183. static load() {
  184. if (!SettingService.loadUrl) return Promise.resolve(SettingService.DefaultSetting);
  185. const request = new Request({headers: {'content-type': SettingService.contentType}});
  186. request.setUrl(SettingService.loadUrl);
  187. request.setMethod(SettingService.method);
  188. request.setData(SettingService.data);
  189. return request.send().then((response) => {
  190. return SettingService.resHandler(response.responseText);
  191. });
  192. }
  193. }
  194.  
  195. SettingService.DefaultSetting = {
  196. highlightBgColor: '#FFDA5E',
  197. highlightTxtColor: 'black',
  198. keywords: {
  199. "https://sspai.com/*": DefaultKeywords,
  200. "https://toutiao.io/*": DefaultKeywords,
  201. "http://www.inoreader.com/*": DefaultKeywords
  202. }
  203. };
  204.  
  205. module.exports = SettingService;
  206. },{"./setting":4}]},{},[2]);