抢救本家直链

把B站视频简介sm号超链接使用的失效短链域名 acg.tv 替换为 nicovideo

当前为 2024-08-18 提交的版本,查看 最新版本

// ==UserScript==
// @name         抢救本家直链
// @namespace    https://memo329.jimdofree.com
// @version      0.1.7
// @description  把B站视频简介sm号超链接使用的失效短链域名 acg.tv 替换为 nicovideo
// @author       永咲みつき
// @author       HIMlaoS_Misa
// @match        *://*.bilibili.com/video/*
// @match        *://*.bilibili.com/list/*
// @grant        none
// @run-at       document-start
// @license      GPLv3
// @supportURL   mailto:[email protected]
// @supportURL   mailto:[email protected]
// ==/UserScript==

(function() {
    'use strict';

    const printLog = e => console.log('[抢救本家直链]', e);
    const doDescLinkReplace = () => {
        const oldNicoReg = /acg\.tv/i;
        const linkDoms = [ ...document.querySelectorAll('#app .desc-info-text a') ].filter(e => oldNicoReg.test(e.href));

        if (linkDoms.length <= 0) {
            printLog('简介中无任何链接,将跳过运行');
            return;
        }

        printLog('发现 ' + linkDoms.length + ' 个链接,开始替换...');
        for (const linkDom of linkDoms) {
            linkDom.href = linkDom.href.replace(oldNicoReg, 'www.nicovideo.jp/watch');
            printLog('已替换 ' + linkDom.innerText + ' 的链接');
        }
    };

    printLog('脚本成功被激活,开始检测页面加载...');
    let failedTime = 0;
    const findDescClock = setInterval(() => {
        if (failedTime >= 60) {
            printLog('页面加载超时,脚本将停止运行');
            clearInterval(findDescClock);
            return;
        }
    
        if (!document.querySelector('#app .bpx-player-ctrl-time-duration')) {
            failedTime++;
            return;
        }

        printLog('检测到页面已加载,正在执行替换操作...');
        clearInterval(findDescClock);
        doDescLinkReplace();
    }, 500);
})();