Douyin embedded videos on voz

Replace Douyin links with embedded videos on Voz forum

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Douyin embedded videos on voz
// @namespace    Douyin on voz
// @version      1.0
// @description  Replace Douyin links with embedded videos on Voz forum
// @icon         https://www.google.com/s2/favicons?sz=64&domain=voz.vn
// @author       kylyte
// @match        https://voz.vn/t/*
// @match        https://voz.vn/conversations/*
// @match        https://voz.vn/whats-new/profile-posts/*
// @match        https://voz.vn/u/*
// @run-at       document-idle
// @license      GPL-3.0
// @grant        GM_xmlhttpRequest
// @connect      douyin.com
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';

    function createIframe(videoId) {
        var iframe = document.createElement('iframe');
        iframe.width = "325";
        iframe.height = "735";
        iframe.frameBorder = "0";
        iframe.src = `https://open.douyin.com/player/video?vid=${videoId}&autoplay=0`;
        iframe.referrerPolicy = "unsafe-url";
        iframe.allowFullscreen = true;
        return iframe;
    }

    function replaceDouyinLinks() {
        document.querySelectorAll('a.link--external').forEach(function(link) {
            var href = link.href;
            if (href) {
                if (href.includes('v.douyin.com')) {
                    GM_xmlhttpRequest({
                        method: "GET",
                        url: href,
                        onload: function(response) {
                            var finalUrl = response.finalUrl;
                            var videoIdMatch = finalUrl.match(/video\/(\d+)/);
                            if (videoIdMatch && videoIdMatch[1]) {
                                var videoId = videoIdMatch[1];
                                var iframe = createIframe(videoId);
                                link.parentNode.replaceChild(iframe, link);
                            }
                        }
                    });
                } else if (href.includes('www.douyin.com/video/')) {
                    var videoIdMatch = href.match(/video\/(\d+)/);
                    if (videoIdMatch && videoIdMatch[1]) {
                        var videoId = videoIdMatch[1];
                        var iframe = createIframe(videoId);
                        link.parentNode.replaceChild(iframe, link);
                    }
                }
            }
        });
    }

    window.addEventListener('load', replaceDouyinLinks);
    const observer = new MutationObserver(replaceDouyinLinks);
    observer.observe(document.body, { childList: true, subtree: true });
})();