Greasy Fork

Greasy Fork is available in English.

TikTok Bio Social Links

Converts tiktok bio socials into links, insta: test > instagram.com/test

目前为 2024-05-14 提交的版本,查看 最新版本

// ==UserScript==
// @name         TikTok Bio Social Links
// @namespace    http://your.namespace
// @version      1.1
// @description  Converts tiktok bio socials into links, insta: test > instagram.com/test
// @match        *://*.tiktok.com/*
// @grant        none
// @license      Unlicense
// ==/UserScript==

(function() {
    'use strict';

    // Wait for the page to load
    window.addEventListener('load', function() {
        setTimeout(() => {
            // Get the element with data-e2e attribute of user-bio
            const userBioElement = document.querySelector('[data-e2e="user-bio"]');
            if (!userBioElement) return;

            // Get the inner text of the element
            const userBioText = userBioElement.innerText;

            // Define an object to map social media names to their URLs
            const socialMediaUrls = {
                "insta": {
                    url: "https://instagram.com/{username}",
                    filter: (username) => username.replaceAll(),
                    removeAtSymbol: true
                },
                "instagram": {
                    url: "https://instagram.com/{username}",
                    filter: (username) => username.replaceAll(),
                    removeAtSymbol: true
                },
                "twitch": {
                    url: "https://twitch.com/{username}",
                    filter: (username) => username.replaceAll(),
                    removeAtSymbol: true
                },
                // Add more social media names and URLs here
                "exampleSocial": {
                    url: "https://testsocial.com/{username}", //The url you want the social to be converted to, the {username} is where you want the username to appear in the url.
                    filter: (username) => username.replaceAll('-', ''), //A simple filter, if you want to replace a char (for example replace A with B) you would replace the ('-', '') with ('A','B') if you want to remove a letter instead of replace it use this ('A','') (replace A with whatever letter you want to replace)
                    removeAtSymbol: false //Set this to true to remove the @ at the start of the username, for example "insta: @example" would become "instagram.com/example" instead of "instagram.com/@example."
                }
            };

            // Create a regex pattern based on the social media names
            const regexPattern = Object.keys(socialMediaUrls).join('|');
            const regex = new RegExp(`(?:${regexPattern})(?::)?\\s*([\\w\\d@_-]+)`, 'gi');

            // Replace the matched strings with links
            const bioWithLinks = userBioText.replace(regex, (match, p1, p2) => {
                const socialMediaName = match.split(/:| /)[0].trim();
                const username = p1;
                const {
                    url,
                    filter,
                    removeAtSymbol
                } = socialMediaUrls[socialMediaName.toLowerCase()];
                if (url) {
                    let processedUsername = filter ? filter(username) : username;
                    if (removeAtSymbol && processedUsername.startsWith('@')) {
                        processedUsername = processedUsername.slice(1);
                    }
                    const finalUrl = url.replace('{username}', processedUsername);
                    return `<a href="${finalUrl}" target="_blank">${socialMediaName}: ${username}</a>`;
                } else {
                    return match; // Return the original string if social media URL is not found
                }
            });

            // Set the inner HTML of the element with the modified bio
            userBioElement.innerHTML = bioWithLinks;
        }, 200)
    });
})();