Greasy Fork

Greasy Fork is available in English.

TikTok video downloader (no watermark)

Downloads videos from TikTok without the watermark.

目前为 2023-12-07 提交的版本,查看 最新版本

// ==UserScript==
// @name         TikTok video downloader (no watermark)
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Downloads videos from TikTok without the watermark.
// @author       You
// @match        https://www.tiktok.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=tiktok.com
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    setInterval(function(){
        // Extract video ID from the URL or player element
        var videoId = getVideoIdFromUrl() || getVideoIdFromPlayer();

        // Find all elements with the second specified class
        var targetElements = document.querySelectorAll(".css-1cifsuk-DivActionItemContainer.e1whjx9o0, .css-1npmxy5-DivActionItemContainer.er2ywmz0, .css-1d39a26-DivFlexCenterRow.ehlq8k31");

        // Check if any elements are found
        if (targetElements.length > 0 && videoId) {
            // Iterate through the NodeList
            targetElements.forEach(function(targetElement) {
                // Check if the button with our specific identifier is already present
                if (!isOurButtonAlreadyAdded(targetElement)) {
                    // Create a button element with a specific identifier
                    var button = createDownloadButton();

                    // Add a click event listener to the button
                    button.addEventListener("click", function () {
                        downloadVideo(videoId);
                    });

                    // Append the button to the target element
                    targetElement.appendChild(button);
                }
            });
        } else {
            console.error("Elements not found with the specified class or Video ID not found");
        }
    }, 2000);

    function isOurButtonAlreadyAdded(element) {
        // Check if a button with a specific identifier is already present inside the target element
        return element.querySelector("button[data-identifier='downloadButton']") !== null;
    }

    function getVideoIdFromUrl() {
        var videoUrl = window.location.href;
        var videoIdMatch = videoUrl.match(/\/video\/(\d+)/);
        return videoIdMatch ? videoIdMatch[1] : null;
    }

    function getVideoIdFromPlayer() {
        var playerElement = document.querySelector(".tiktok-web-player.no-controls");
        return playerElement ? extractIdFromPlayerId(playerElement.id) : null;
    }

    function extractIdFromPlayerId(playerId) {
        var playerIdMatch = playerId.match(/-(\d+)$/);
        return playerIdMatch ? playerIdMatch[1] : null;
    }

    function createDownloadButton() {
        var button = document.createElement("button");
        var arrowSpan = document.createElement("span");
        var textParagraph = document.createElement("p");

        arrowSpan.innerHTML = "🠳";
        textParagraph.style.fontSize = "12px";
        textParagraph.textContent = "Download";

        button.appendChild(arrowSpan);
        button.appendChild(textParagraph);

        button.setAttribute("data-identifier", "downloadButton"); // Add a specific identifier

        button.style.cssText = `
            border: none;
            background: none;
            outline: none;
            padding: 0px;
            margin-right: 0px;
            position: relative;
            display: flex;
            -webkit-box-align: center;
            align-items: center;
            cursor: pointer;
            margin-bottom: 8px;
            flex-direction: column;
            color: white;
            font-size: 28px;
        `;

        return button;
    }

    function downloadVideo(videoId) {
        fetch(`https://api16-normal-c-useast1a.tiktokv.com/aweme/v1/feed/?aweme_id=${videoId}`)
            .then(response => response.json())
            .then(data => {
                if (data && data.aweme_list && data.aweme_list.length > 0) {
                    var playAddr = data.aweme_list[0].video.play_addr.url_list[0];

                    // Fetch the video content as a Blob
                    return fetch(playAddr);
                } else {
                    console.error("Invalid API response format");
                }
            })
            .then(response => response.blob())
            .then(blob => {
                // Create a Blob URL for the video content
                var blobUrl = URL.createObjectURL(blob);

                // Create a hidden anchor element for downloading
                var downloadLink = document.createElement("a");
                downloadLink.href = blobUrl;
                downloadLink.download = `tiktok_video_${videoId}.mp4`; // Set the file name

                // Trigger a click on the anchor element to start the download
                downloadLink.click();
            })
            .catch(error => console.error("Error fetching data:", error));
    }
})();