Douyin Text Extractor

Activate mode to click and get text from elements with a popup copy option, preserving <br> tags

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Douyin Text Extractor
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Activate mode to click and get text from elements with a popup copy option, preserving <br> tags
// @author       korruz
// @match        https://www.douyin.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    var isActive = false; // To track whether selection mode is active

    // Create activation button
    var activationButton = document.createElement('button');
    activationButton.textContent = 'Activate Text Selection';
    activationButton.style.position = 'fixed';
    activationButton.style.bottom = '20px';
    activationButton.style.right = '20px';
    activationButton.style.padding = '10px';
    activationButton.style.fontSize = '16px';
    activationButton.style.zIndex = '10000';

    activationButton.onclick = function() {
        isActive = !isActive; // Toggle activation state
        activationButton.textContent = isActive ? 'Text Selection Active (Click to Deactivate)' : 'Activate Text Selection';
    };

    document.body.appendChild(activationButton);

    // Function to create and show the popup
    function createPopup(text) {
        // Remove existing popup if it exists
        var existingPopup = document.getElementById('custom-popup');
        if (existingPopup) {
            existingPopup.remove();
        }

        // Create the popup window
        var popup = document.createElement('div');
        popup.id = 'custom-popup';
        popup.style.position = 'fixed';
        popup.style.top = '50px';
        popup.style.right = '50px';
        popup.style.padding = '20px';
        popup.style.backgroundColor = 'white';
        popup.style.border = '1px solid black';
        popup.style.zIndex = '10010';

        // Text element
        var textElement = document.createElement('textarea');
        textElement.value = text;
        textElement.style.width = '300px';
        textElement.style.height = '150px';

        // Copy button
        var copyButton = document.createElement('button');
        copyButton.textContent = 'Copy';
        copyButton.style.display = 'block';
        copyButton.style.marginTop = '10px';
        copyButton.onclick = function() {
            navigator.clipboard.writeText(textElement.value).then(() => {
                alert('Text copied successfully!');
            }).catch(err => {
                alert('Failed to copy text: ' + err);
            });
        };

        // Append elements to the popup
        popup.appendChild(textElement);
        popup.appendChild(copyButton);

        // Append the popup to the body
        document.body.appendChild(popup);
    }
    function getTextWithNewLines(element) {
        // 创建一个容器来存储最终的文本
        let text = "";

        // 遍历所有子节点
        for (const node of element.childNodes) {
            if (node.nodeType === Node.TEXT_NODE) {
                // 如果是文本节点,直接添加文本
                text += node.nodeValue;
            } else if (node.nodeType === Node.ELEMENT_NODE) {
                // 如果是元素节点,检查是否为<br>,如果是,添加换行符
                if (node.tagName === "BR") {
                    text += "\n";
                } else {
                    // 对于其他类型的元素,递归调用此函数
                    text += getTextWithNewLines(node);
                }
            }
        }

        return text;
    }

    function extractText(html) {
        // 创建一个新的div元素
        var tempDiv = document.createElement("div");
        // 设置HTML内容
        tempDiv.innerHTML = html;
        // 返回元素的纯文本内容
        return getTextWithNewLines(tempDiv);
    }
    document.addEventListener('click', function(e) {
        if (isActive && !e.target.matches('button')) { // Check if the activation mode is active and the target is not a button
            e.preventDefault();
            e.stopPropagation();

            // Extract text and handle <br> tags
            //var text = e.target.innerHTML.replace(/<br\s*\/?>/gi, '\n');
            var text = extractText(e.target.innerHTML);
            createPopup(text);
            return false;
        }
    }, true); // Use capture phase to handle the event first
})();