自定义网站图标

尝试在特定网站上更改图标

// ==UserScript==
// @name         自定义网站图标
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  尝试在特定网站上更改图标
// @author       哒哒伽
// @match        *://*.*.*/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 自定义图标的URL
    var newFaviconUrl = 'https://i.cnki.net/favicon.ico';

    // 创建一个新的<link>元素
    var link = document.createElement('link');
    link.type = 'image/x-icon';
    link.rel = 'shortcut icon';
    link.href = newFaviconUrl;

    // 移除现有的所有图标
    var head = document.getElementsByTagName('head')[0];
    var links = head.getElementsByTagName('link');
    for (var i = 0; i < links.length; i++) {
        if (links[i].rel === 'shortcut icon' || links[i].rel === 'icon') {
            head.removeChild(links[i]);
        }
    }

    // 添加新的图标
    head.appendChild(link);
})();