tgWebAppData

复制tgWebAppData

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         tgWebAppData
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  复制tgWebAppData
// @match        *://*.telegram.org/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let button = null; // To hold the button element

    // Function to create and insert the copy button
    function createCopyButton(src) {
        // Check if the button already exists
        if (button) {
            return;
        }

        // Create button element
        button = document.createElement('button');
        button.id = 'copyTgWebAppDataBtn';
        button.innerText = '复制 tgWebAppData';
        button.style.position = 'fixed';
        button.style.top = '10px';
        button.style.right = '10px';
        button.style.padding = '10px';
        button.style.backgroundColor = '#007BFF'; // Blue color
        button.style.color = 'white';
        button.style.border = 'none';
        button.style.borderRadius = '5px';
        button.style.cursor = 'pointer';
        button.style.zIndex = '10000'; // High z-index to ensure it's on top
        button.style.fontSize = '16px'; // Font size

        // Append button to the body
        document.body.appendChild(button);

        // Add click event to copy the formatted src value
        button.addEventListener('click', () => {
            // Extract parameters from src and format them
            const formattedData = extractTgWebAppData(src);
            navigator.clipboard.writeText(formattedData).then(() => {
                alert('tgWebAppData 复制成功!');
            }).catch(err => {
                console.error('Failed to copy text: ', err);
            });
        });
    }

    // Function to remove the copy button
    function removeCopyButton() {
        if (button) {
            button.remove();
            button = null;
        }
    }

    // Function to handle iframe visibility and button actions
    function handleIframes() {
        const iframes = document.querySelectorAll('iframe.zA1w1IOq');
        if (iframes.length > 0) {
            // Extract the src attribute value from the first matching iframe
            const src = iframes[0].src;
            const tgWebAppData = extractTgWebAppData(src);

            // Check if tgWebAppData contains "query_id"
            if (tgWebAppData.includes('query_id')) {
                createCopyButton(src);
            } else {
                removeCopyButton();
            }
        } else {
            removeCopyButton();
        }
    }

    // Function to extract tgWebAppData parameter without decoding
    function extractTgWebAppData(src) {
        const params = new URL(src).hash.substring(1); // Remove the leading '#'
        const dataParams = new URLSearchParams(params);
        const tgWebAppData = dataParams.get('tgWebAppData');
        return tgWebAppData ? tgWebAppData : 'No tgWebAppData found';
    }

    // Use MutationObserver to handle dynamically loaded iframes
    const observer = new MutationObserver(() => {
        handleIframes();
    });

    // Start observing the document body for changes
    observer.observe(document.body, { childList: true, subtree: true });

    // Initial check when the page loads
    window.addEventListener('load', handleIframes);
})();