Niconico 網址重新導向

自動將類似 acg.tv/sm* 的網址轉換為 nicovideo.jp/watch/sm* (包含網址參數)

// ==UserScript==
// @name              Niconico URL 重定向
// @name:en           Niconico URL redirector
// @name:zh-TW        Niconico 網址重新導向
// @namespace         http://tampermonkey.net/
// @version           0.1
// @description       自动将类似 acg.tv/sm* 的 URL 转换为 nicovideo.jp/watch/sm* (包括 URL 参数)
// @description:en    Redirect URLs like acg.tv/sm* to nicovideo.jp/watch/sm* (including url arguments)
// @description:zh-TW 自動將類似 acg.tv/sm* 的網址轉換為 nicovideo.jp/watch/sm* (包含網址參數)
// @author            はなちゃん with help by Bing Copilot
// @match             *://*/*
// @grant             none
// @license           MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to redirect URLs
    function redirectUrls() {
        // Redirect current window URL
        if (window.location.href.includes("acg.tv/sm")) {
            window.location.href = window.location.href.replace("acg.tv/sm", "nicovideo.jp/watch/sm");
        }

        // Redirect URLs in href of a tags
        document.querySelectorAll('a').forEach(a => {
            if (a.href.includes("acg.tv/sm")) {
                a.href = a.href.replace("acg.tv/sm", "nicovideo.jp/watch/sm");
            }
        });
    }

    // Call the function initially
    redirectUrls();

    // Call the function 1s later, for later-render page.
    setInterval(() => { redirectUrls(); }, 1000);
})();