Guest User

Untitled

a guest
Aug 24th, 2025
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         TikTok Metadata Extractor
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.3
  5. // @description  Extracts video metadata and adds a button with hashtag formatting
  6. // @author       You
  7. // @match        *://www.tiktok.com/*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12.     const tiktokSelectors = {
  13.         creatorName: ['span[data-e2e="browse-username"]'],
  14.         creatorUrl: ['a[data-e2e="browse-user-avatar"]'],
  15.         likes: ['strong[data-e2e="like-count"]'],
  16.         comments: ['strong[data-e2e="comment-count"]'],
  17.         shares: ['strong[data-e2e="share-count"]']
  18.     };
  19.  
  20.     function getElement(selectors) {
  21.         if (typeof selectors === 'string') selectors = [selectors];
  22.         return Array.from(selectors).reduce((found, selector) => found || document.querySelector(selector), null);
  23.     }
  24.  
  25.     function getText(selectors) {
  26.         const element = getElement(selectors);
  27.         return element ? element.textContent.trim() : 'N/A';
  28.     }
  29.  
  30.     function getDescriptionBBCode() {
  31.         const container = document.querySelector('div[data-e2e="browse-video-desc"]');
  32.         if (!container) return 'N/A';
  33.         let parts = [];
  34.         container.childNodes.forEach(node => {
  35.             if (node.nodeType === Node.TEXT_NODE) {
  36.                 parts.push(node.textContent.trim());
  37.             } else if (node.nodeType === Node.ELEMENT_NODE) {
  38.                 if (node.matches('span[data-e2e="new-desc-span"]')) {
  39.                     parts.push(node.textContent.trim());
  40.                 } else if (node.matches('a[data-e2e="search-common-link"]')) {
  41.                     const href = node.href;
  42.                     const text = node.textContent.trim();
  43.                     parts.push(`[u][url=${href}]${text}[/url][/u]`);
  44.                 }
  45.             }
  46.         });
  47.         return parts.join(' ').replace(/\s+/g, ' ').trim();
  48.     }
  49.  
  50.     function getCreatorUrl() {
  51.         const creatorAvatarLink = getElement(tiktokSelectors.creatorUrl);
  52.         return creatorAvatarLink ? creatorAvatarLink.href : window.location.href;
  53.     }
  54.  
  55.     function copyToClipboard(text) {
  56.         const textarea = document.createElement('textarea');
  57.         textarea.value = text;
  58.         document.body.appendChild(textarea);
  59.         textarea.select();
  60.         document.execCommand('copy');
  61.         document.body.removeChild(textarea);
  62.     }
  63.  
  64.     function showToast(message, isError = false) {
  65.         const toast = document.createElement('div');
  66.         Object.assign(toast.style, {
  67.             position: 'fixed',
  68.             top: '10px',
  69.             right: '10px',
  70.             padding: '12px 24px',
  71.             backgroundColor: isError ? '#ff4444' : '#00C851',
  72.             color: 'white',
  73.             borderRadius: '4px',
  74.             fontSize: '14px',
  75.             zIndex: 10000,
  76.             boxShadow: '0 2px 5px rgba(0,0,0,0.2)'
  77.         });
  78.         toast.textContent = message;
  79.         document.body.appendChild(toast);
  80.         setTimeout(() => toast.remove(), 3000);
  81.     }
  82.  
  83.     function createButton() {
  84.         const button = document.createElement('button');
  85.         button.textContent = 'Copy Metadata';
  86.         button.id = 'tiktok-metadata-button';
  87.         button.style.cssText = `
  88.             padding: 8px 16px;
  89.             background: #000;
  90.             color: white;
  91.             border: none;
  92.             border-radius: 4px;
  93.             cursor: pointer;
  94.             font-size: 14px;
  95.             margin: 0 8px;
  96.         `;
  97.  
  98.         button.addEventListener('click', () => {
  99.             const metadata = {
  100.                 description: getDescriptionBBCode(),
  101.                 creator: getText(tiktokSelectors.creatorName),
  102.                 creatorUrl: getCreatorUrl(),
  103.                 likes: getText(tiktokSelectors.likes),
  104.                 comments: getText(tiktokSelectors.comments),
  105.                 shares: getText(tiktokSelectors.shares)
  106.             };
  107.  
  108.             const videoUrl = window.location.href;
  109.             const bbcode = `[b]${metadata.description}[/b]\n\n[url=${videoUrl}]${videoUrl}[/url]\n\n[b]Creator Info[/b]\n[u][url=${metadata.creatorUrl}]${metadata.creator}[/url][/u]\n\n[b]Engagement[/b]\nLikes: ${metadata.likes}\nComments: ${metadata.comments}\nShares: ${metadata.shares}`;
  110.  
  111.             copyToClipboard(bbcode);
  112.             showToast('Metadata copied to clipboard!');
  113.         });
  114.  
  115.         return button;
  116.     }
  117.  
  118.     const observer = new MutationObserver(() => {
  119.         const followButton = document.querySelector('button[data-e2e="browse-follow"]');
  120.         if (followButton && !document.getElementById('tiktok-metadata-button')) {
  121.             const existingContainer = followButton.parentElement;
  122.             const newButton = createButton();
  123.             if (document.body.contains(existingContainer)) {
  124.                 existingContainer.appendChild(newButton);
  125.             }
  126.         }
  127.     });
  128.  
  129.     observer.observe(document.body, { childList: true, subtree: true });
  130. })();
  131.  
Advertisement
Add Comment
Please, Sign In to add comment