优化网页显示的标题和图片

用来自定义浏览器标签页显示的标题和图片

目前為 2024-06-07 提交的版本,檢視 最新版本

// ==UserScript==
// @name        优化网页显示的标题和图片
// @name:en     Optimize the display of the title and images
// @namespace   Violentmonkey Scripts
// @match        *://*/*
// @grant       none
// @version     1.6.6
// @author      hoorn
// @icon        https://s2.loli.net/2024/05/29/m5LcD7ZblIrQHXV.png
// @description 用来自定义浏览器标签页显示的标题和图片
// @description:en Used to customize the title and image displayed in the browser tab
// @license     MIT
// ==/UserScript==
(function () {
    'use strict';
    if (navigator.userAgent.indexOf("Edg") !== -1) {
        var currentHost = window.location.host;

        //Custom the title
        var pageTitle = document.querySelector('title');
        pageTitle.textContent = '.';

        //Custom the image
        var imageUrl = 'https://s2.loli.net/2024/05/29/m5LcD7ZblIrQHXV.png';
        if (currentHost.includes('reddit.com')) {
            var existingIcons = document.querySelectorAll('link[rel="icon"], link[rel="shortcut icon"]');
            existingIcons.forEach(function (icon) {
                icon.parentNode.removeChild(icon);
            });

            var icons = [
                { href: imageUrl, sizes: "64x64" },
                { href: imageUrl, sizes: "128x128" },
                { href: imageUrl, sizes: "192x192" }
            ];
            icons.forEach(function (icon) {
                var link = document.createElement('link');
                link.setAttribute('rel', 'icon shortcut');
                link.setAttribute('sizes', icon.sizes);
                link.setAttribute('href', icon.href);
                document.head.appendChild(link);
            });
        }
        else {
            var icon = document.querySelector('link[rel="icon"]');
            if (icon) {
                icon.href = imageUrl;
            }
            var alternateIcon = document.querySelector('link[rel="alternate icon"]');
            if (alternateIcon) {
                alternateIcon.href = imageUrl
            }
            var shortIcon = document.querySelector('link[rel="shortcut icon"]');
            if (shortIcon) {
                shortIcon.href = imageUrl;
            }
        }
    }
})();