highlight-keywords

高亮关键词,可设置关键词的样式,支持正则匹配,自行修改脚本配置

目前為 2021-06-22 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         highlight-keywords
// @namespace    https://github.com/mudssky/highlight-keywords
// @version      0.1.2
// @description  高亮关键词,可设置关键词的样式,支持正则匹配,自行修改脚本配置
// @author       mudssky
// @include       *://sukebei.nyaa.si/*
// @grant        none
// @homepageURL https://github.com/mudssky/highlight-keywords
// @supportURL        https://github.com/mudssky/highlight-keywords/issues
// ==/UserScript==

(function() {
    'use strict';
var RuleList = [
        {
            keyword: '成年コミック',
            // color: 'yellow',
            styleText: 'background:gold;',
            matchUrl: 'sukebei.nyaa.si',
        },
    ];
    // 筛选匹配当前页面的规则
    var matchedRuleList = RuleList.filter(function (rule) {
        // 检查是否存在matchUrl选项,如果没有直接报错,默认当作匹配所有网站处理,直接通过过滤
        if (!rule.matchUrl) {
            console.error('this rule should have a match url');
            return rule;
        }
        // 存在matchUrl选项,则过滤匹配当前页面的rule
        if (rule.matchUrl) {
            var urlPattern = new RegExp(rule.matchUrl);
            if (urlPattern.test(window.location.href)) {
                return rule;
            }
        }
    });
    // 这个变量存放最后修改后的html
    var lastHtml = document.body.innerHTML;
    // console.log(matchedRuleList)
    // 使用innerhtml 替换的方式,替换关键词为带高亮style的html
    for (var i in matchedRuleList) {
        // 作为中间变量,每次循环从上次的结果基础上进行。
        var currentHtml = lastHtml;
        var htmlPattern = new RegExp("(<[^>]+>[^<>]*?)" + matchedRuleList[i].keyword + "([^<>]*?<[^>]+>)", 'g');
        lastHtml = currentHtml.replaceAll(htmlPattern, "$1<em style=\"" + matchedRuleList[i].styleText + "\">" + matchedRuleList[i].keyword + "</em>$2");
    }
    document.body.innerHTML = lastHtml;

})();