highlight-my-interest

highlight keywords in my favorites

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

  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 GM_xmlhttpRequest
  10. // @require https://greasyfork.org/scripts/31793-jmul/code/JMUL.js?version=209567
  11. // @include http://*
  12. // @include https://*
  13. // @run-at document-end
  14. // @namespace https://greasyfork.org/users/34556-jferroal
  15. // ==/UserScript==
  16.  
  17. (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){
  18. let JMUL = window.JMUL || {};
  19.  
  20. const Map = (list, fn) => {
  21. let result = [];
  22. if (list && list.length) {
  23. for (let i = 0; i < list.length; i += 1) {
  24. result.push(fn(list[ i ]));
  25. }
  26. }
  27. return result;
  28. };
  29.  
  30. class TextElement {
  31. constructor (element) {
  32. this.element = new JMUL.Element(element);
  33. this.innerText = this.element.innerText;
  34. this.shouldHighlight = false;
  35. }
  36.  
  37. detect () {
  38. for (const keyword of TextElement.keywords) {
  39. const keywordPattern = new RegExp(keyword, 'gi');
  40. if (keywordPattern.test(this.innerText)) {
  41. this.shouldHighlight = true;
  42. break;
  43. }
  44. }
  45. return this;
  46. }
  47.  
  48. highlight () {
  49. if (this.shouldHighlight) {
  50. this.element.setCss(TextElement.highlightStyle);
  51. }
  52. }
  53.  
  54. static init (setting) {
  55. TextElement.highlightStyle = {
  56. background: setting.highlightBgColor,
  57. color: setting.highlightTxtColor,
  58. };
  59. }
  60.  
  61. static setKeywords (keywords) {
  62. TextElement.keywords = keywords;
  63. }
  64.  
  65. static findAll () {
  66. return TextElement.targetTagNames.reduce((res, tagName) => {
  67. const tags = document.getElementsByTagName(tagName);
  68. return res.concat(Map(tags, (e) => new TextElement(e)));
  69. }, []);
  70. }
  71. }
  72.  
  73. TextElement.targetTagNames = [ 'h1', 'h2', 'h3', 'h4', 'h5', 'p', 'a', 'pre', 'blockquote', 'summary' ];
  74. module.exports = TextElement;
  75.  
  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. loadSetting().then((setting) => {
  87. KeywordService.init(setting, href);
  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. class KeywordService {
  111. static init (setting, href) {
  112. KeywordService.Setting = setting;
  113. KeywordService.keywords = [];
  114. const sites = Object.keys(KeywordService.Setting.keywords);
  115. if (!sites || !sites.length) return;
  116. sites.forEach((site) => {
  117. const sitePattern = new RegExp(site, 'gi');
  118. if (sitePattern.test(href)) {
  119. KeywordService.keywords.push(...KeywordService.Setting.keywords[ site ]);
  120. }
  121. });
  122. }
  123.  
  124. static list () {
  125. return Promise.resolve(KeywordService.keywords);
  126. }
  127. }
  128.  
  129. module.exports = KeywordService;
  130.  
  131. },{}],4:[function(require,module,exports){
  132. class Setting {
  133. constructor (jsonBody) {
  134. Object.assign(this, jsonBody);
  135. }
  136. }
  137.  
  138. module.exports = { Setting };
  139.  
  140. },{}],5:[function(require,module,exports){
  141. const Setting = require('./setting').Setting;
  142. const { Request } = window.JMUL || { JMUL: {} };
  143.  
  144. const DefaultKeywords = [
  145. '书籍',
  146. '效率',
  147. 'google.*?',
  148. 'nexus.*?',
  149. '爬虫',
  150. 'python.*?',
  151. 'angular.*?',
  152. 'node',
  153. 'javascript',
  154. 'ukulele',
  155. /gtd.*?/gi,
  156. '工作流',
  157. '日程',
  158. '英雄联盟',
  159. 'vps',
  160. '服务器',
  161. '书单',
  162. '免费',
  163. '限免',
  164. '数据分析',
  165. '自由职业',
  166. 'lol',
  167. 'react',
  168. 'mobx',
  169. ];
  170.  
  171. const DefaultResponseHandler = (_response) => {
  172. let response = _response;
  173. if (typeof _response === 'object' && _response.responseText) {
  174. response = _response.responseText;
  175. }
  176. return new Setting(JSON.parse(response));
  177. };
  178.  
  179. class SettingService {
  180. static init (config) {
  181. SettingService.loadUrl = config.loadUrl;
  182. SettingService.method = config.method || 'GET';
  183. SettingService.contentType = config.contentType || 'application/json';
  184. SettingService.data = config.data || {};
  185. SettingService.resHandler = config.resHandler || DefaultResponseHandler;
  186. }
  187.  
  188. static load () {
  189. if (!SettingService.loadUrl) return Promise.resolve(SettingService.DefaultSetting);
  190. const request = new Request({ headers: { 'content-type': SettingService.contentType } });
  191. request.setUrl(SettingService.loadUrl);
  192. request.setMethod(SettingService.method);
  193. request.setData(SettingService.data);
  194. return request.send().then((response) => {
  195. return SettingService.resHandler(response.responseText);
  196. });
  197. }
  198. }
  199.  
  200. SettingService.DefaultSetting = {
  201. highlightBgColor: '#FFDA5E',
  202. highlightTxtColor: 'black',
  203. keywords: {
  204. 'https://sspai.com/*': DefaultKeywords,
  205. 'https://toutiao.io/*': DefaultKeywords,
  206. 'http://www.inoreader.com/*': DefaultKeywords,
  207. },
  208. };
  209.  
  210. module.exports = SettingService;
  211.  
  212. },{"./setting":4}]},{},[2]);