Greasy Fork

自动转换https链接为超链接并高亮显示

将网页中的所有https链接自动转换为可点击的超链接并高亮显示

// ==UserScript==
// @name         自动转换https链接为超链接并高亮显示
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  将网页中的所有https链接自动转换为可点击的超链接并高亮显示
// @author       wuyi
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 更新的正则表达式,匹配包含路径、查询参数、哈希的https链接
    var regex = /https:\/\/[^\s<>"']+(?:[#?][^\s]*)?/g;

    // 遍历页面中所有文本节点
    function convertLinks(node) {
        var child;
        if (node.nodeType === 3) { // 如果是文本节点
            var newText = node.nodeValue.replace(regex, function(match) {
                return `<a href="${match}" target="_blank" class="highlight-link">${match}</a>`;
            });

            if (newText !== node.nodeValue) {
                var span = document.createElement('span');
                span.innerHTML = newText;
                node.parentNode.replaceChild(span, node);
            }
        } else {
            for (child = node.firstChild; child; child = child.nextSibling) {
                convertLinks(child); // 遍历所有子节点
            }
        }
    }

    // 触发转换
    convertLinks(document.body);
})();