live2d美化Tampermonkey脚本

· · 个人记录

Ctrl+B启用

// ==UserScript==
// @name         Live2d
// @namespace    http://tampermonkey.net/
// @version      2025-05-24
// @description  给你的网站添加live2d人物
// @author       zhouhaoGG
// @match        *://*/*
// @icon         https://www.bilibili.com/favicon.ico
// @grant        none
// ==/UserScript==

function loadExternalResource(url, type) {
    return new Promise((resolve, reject) => {
        let tag;

        if (type === "css") {
            tag = document.createElement("link");
            tag.rel = "stylesheet";
            tag.href = url;
        }
        else if (type === "js") {
            tag = document.createElement("script");
            tag.src = url;
        }
        if (tag) {
            tag.onload = () => resolve(url);
            tag.onerror = () => reject(url);
            document.head.appendChild(tag);
        }
    });
}
function loadLive2d(){
    console.log("\n\n正在加载live2d\n\n");
    const live2d_path = "https://fastly.jsdelivr.net/gh/stevenjoezhang/live2d-widget@latest/";
    if (screen.width >= 768) {
        Promise.all([
            loadExternalResource(live2d_path + "waifu.css", "css"),
            loadExternalResource(live2d_path + "live2d.min.js", "js"),
            loadExternalResource(live2d_path + "waifu-tips.js", "js")
        ]).then(() => {
            initWidget({
                waifuPath: live2d_path + "waifu-tips.json",
                cdnPath: "https://fastly.jsdelivr.net/gh/fghrsh/live2d_api/",
                tools: ["hitokoto", "asteroids", "switch-model", "switch-texture", "photo", "info", "quit"]
            });
        });
    }
}

(function() {
    'use strict';
    const handler = (event) => {
    if (event.ctrlKey && event.key.toLowerCase() === 'b') {
      event.preventDefault();
      document.removeEventListener('keydown', handler);
      loadLive2d();
    }
  };
  document.addEventListener('keydown', handler);
})();

Tampermonkey扩展edge

请打开开发者模式

step1\ \ step2\