highlight-my-interest

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

当前为 2017-04-18 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         highlight-my-interest
// @version      0.1.4
// @description  高亮特定网页中感兴趣的关键词
// @author       [email protected]
// @include      https://sspai.com/*
// @include      https://toutiao.io/*
// @include      http://www.inoreader.com/
// @include      https://www.52pojie.com
// @run-at       context-end
// @namespace    https://greasyfork.org/users/34556-jferroal
// ==/UserScript==

const JFInterestedKeywords = [
  /.*书籍.*/,
  /.*效率.*/gi,
  /.*google.*/gi,
  /.*Nexus.*/gi,
  /.*爬虫.*/,
  /.*python.*/gi,
  /.*angular.*/gi,
  /.*node.*/gi,
  /.*javascript.*/gi,
  /.*ukulele.*/gi,
  /.*GTD.*/gi,
  /.*工作流.*/gi,
  /.*日程.*/gi,
  /.*英雄联盟.*/gi,
  /.*VPS.*/gi,
  /.*服务器.*/gi,
  /.*书单.*/gi,
  /.*免费.*/gi,
  /.*限免.*/gi,
  /.*数据分析.*/gi,
  /.*自由职业.*/gi
];

let alreadyLoadedTimes = 0;

function getElements() {
  const targetTagNames = ['h1', 'h2', 'h3', 'h4', 'h5', 'p', 'a', 'pre', 'blockquote', 'summary'];
  return targetTagNames.reduce((res, tagName) => {
    const tags = document.getElementsByTagName(tagName);
    if (tags && tags.length) {
      res.push(...tags);
    }
    return res;
  }, []);
}

function matchKeywordsInElements() {
  const elements = getElements();
  elements.forEach((element) => {
    JFInterestedKeywords.forEach((keyword) => {
      const isMatched = keyword.test(element.innerText);
      if (isMatched) {
        element.style.backgroundColor = '#FFDA5E';
        element.style.color = 'black';
      }
    });
  });
  alreadyLoadedTimes += 1;
}
const autoMatchInterval = setInterval(() => {
  if (alreadyLoadedTimes < 100) {
    matchKeywordsInElements();
  } else {
    clearInterval(autoMatchInterval);
    console.log('will no match any more');
  }
}, 1000);