Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name TikTok Metadata Extractor
- // @namespace http://tampermonkey.net/
- // @version 0.3
- // @description Extracts video metadata and adds a button with hashtag formatting
- // @author You
- // @match *://www.tiktok.com/*
- // @grant none
- // ==/UserScript==
- (function() {
- const tiktokSelectors = {
- creatorName: ['span[data-e2e="browse-username"]'],
- creatorUrl: ['a[data-e2e="browse-user-avatar"]'],
- likes: ['strong[data-e2e="like-count"]'],
- comments: ['strong[data-e2e="comment-count"]'],
- shares: ['strong[data-e2e="share-count"]']
- };
- function getElement(selectors) {
- if (typeof selectors === 'string') selectors = [selectors];
- return Array.from(selectors).reduce((found, selector) => found || document.querySelector(selector), null);
- }
- function getText(selectors) {
- const element = getElement(selectors);
- return element ? element.textContent.trim() : 'N/A';
- }
- function getDescriptionBBCode() {
- const container = document.querySelector('div[data-e2e="browse-video-desc"]');
- if (!container) return 'N/A';
- let parts = [];
- container.childNodes.forEach(node => {
- if (node.nodeType === Node.TEXT_NODE) {
- parts.push(node.textContent.trim());
- } else if (node.nodeType === Node.ELEMENT_NODE) {
- if (node.matches('span[data-e2e="new-desc-span"]')) {
- parts.push(node.textContent.trim());
- } else if (node.matches('a[data-e2e="search-common-link"]')) {
- const href = node.href;
- const text = node.textContent.trim();
- parts.push(`[u][url=${href}]${text}[/url][/u]`);
- }
- }
- });
- return parts.join(' ').replace(/\s+/g, ' ').trim();
- }
- function getCreatorUrl() {
- const creatorAvatarLink = getElement(tiktokSelectors.creatorUrl);
- return creatorAvatarLink ? creatorAvatarLink.href : window.location.href;
- }
- function copyToClipboard(text) {
- const textarea = document.createElement('textarea');
- textarea.value = text;
- document.body.appendChild(textarea);
- textarea.select();
- document.execCommand('copy');
- document.body.removeChild(textarea);
- }
- function showToast(message, isError = false) {
- const toast = document.createElement('div');
- Object.assign(toast.style, {
- position: 'fixed',
- top: '10px',
- right: '10px',
- padding: '12px 24px',
- backgroundColor: isError ? '#ff4444' : '#00C851',
- color: 'white',
- borderRadius: '4px',
- fontSize: '14px',
- zIndex: 10000,
- boxShadow: '0 2px 5px rgba(0,0,0,0.2)'
- });
- toast.textContent = message;
- document.body.appendChild(toast);
- setTimeout(() => toast.remove(), 3000);
- }
- function createButton() {
- const button = document.createElement('button');
- button.textContent = 'Copy Metadata';
- button.id = 'tiktok-metadata-button';
- button.style.cssText = `
- padding: 8px 16px;
- background: #000;
- color: white;
- border: none;
- border-radius: 4px;
- cursor: pointer;
- font-size: 14px;
- margin: 0 8px;
- `;
- button.addEventListener('click', () => {
- const metadata = {
- description: getDescriptionBBCode(),
- creator: getText(tiktokSelectors.creatorName),
- creatorUrl: getCreatorUrl(),
- likes: getText(tiktokSelectors.likes),
- comments: getText(tiktokSelectors.comments),
- shares: getText(tiktokSelectors.shares)
- };
- const videoUrl = window.location.href;
- 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}`;
- copyToClipboard(bbcode);
- showToast('Metadata copied to clipboard!');
- });
- return button;
- }
- const observer = new MutationObserver(() => {
- const followButton = document.querySelector('button[data-e2e="browse-follow"]');
- if (followButton && !document.getElementById('tiktok-metadata-button')) {
- const existingContainer = followButton.parentElement;
- const newButton = createButton();
- if (document.body.contains(existingContainer)) {
- existingContainer.appendChild(newButton);
- }
- }
- });
- observer.observe(document.body, { childList: true, subtree: true });
- })();
Advertisement
Add Comment
Please, Sign In to add comment