高亮关键词

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

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name                highlight-my-interest
// @name:zh-CN          高亮关键词
// @description         highlight keywords in my favorites
// @description:zh-CN   高亮特定网页中感兴趣的关键词
// @version             0.1.6
// @author              jferroal
// @namespace           https://jferroal.com
// @license             GPL-3.0
// @grant               none
// @include             https://sspai.com/*
// @include             https://toutiao.io/*
// @include             http://www.inoreader.com/
// @include             http://www.52pojie.cn/
// @run-at              document-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);