TikTok Video Downloader

Adds a download button to tiktok.com/@profile/video pages. Click a video, the icon is added by the social media share buttons. Right click the icon, and save as. Left click takes you to the video in your browser.

< Σχολιασμός για τον κώδικα TikTok Video Downloader

Αναφορά: Εντάξει - ο κώδικας λειτουργεί αλλά έχει σφάλματα

§
Δημοσιεύτηκε: 29/07/2024

My Improvement:

// ==UserScript==
// @name Tiktok Video Downloader🎬
// @namespace none
// @version 1.0
// @description Download TikTok Videos Without Watermark
// @author altaireh
// @match *://*.tiktok.com/*
// @icon https://miro.medium.com/v2/resize:fit:512/1*KX6NTUUHWlCP4sCXz28TBA.png
// @grant none
// @downloadURL none
// @updateURL none
// ==/UserScript==

(function() {
'use strict';

// Function to create and style the download button
function createDownloadButton() {
const button = document.createElement('button');
button.textContent = '⬇️';
button.className = 'download-btn';
button.style.position = 'absolute';
button.style.left = '10px';
button.style.bottom = '50%';
button.style.zIndex = '1000';
button.style.padding = '10px';
button.style.color = '#fff';
button.style.background = '#007bff';
button.style.border = 'none';
button.style.borderRadius = '50px';
return button;
}

// Function to add the download button to the video element
function addDownloadButton(video) {
if (video.nextElementSibling && video.nextElementSibling.classList.contains('download-btn')) {
return; // Button already exists
}

const downloadBtn = createDownloadButton();
downloadBtn.addEventListener('click', () => {
const videoUrl = video.src || video.querySelector('source').src;
if (videoUrl) {
const newTabUrl = `${window.location.href.split('?')[0]}?videoUrl=${encodeURIComponent(videoUrl)}&autoDownload=true`;
window.open(newTabUrl, '_blank');
} else {
console.error('Video URL not found');
}
});

video.parentNode.insertBefore(downloadBtn, video.nextSibling);
}

// Function to trigger automatic download in a new tab
function triggerAutoDownload() {
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.get('autoDownload') === 'true') {
const videoUrl = urlParams.get('videoUrl');
if (videoUrl) {
const filename = videoUrl.split('/').pop();
const a = document.createElement('a');
a.href = videoUrl;
a.download = filename.endsWith('.mp4') ? filename : `${filename}.mp4`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} else {
console.error('Video URL not found');
}
}
}

// Function to check if the current page is a TikTok video download page
function isDownloadPage() {
const downloadUrlPattern = /^https:\/\/v\d+-webapp-\w+\.tiktok\.com\/video\//;
return downloadUrlPattern.test(window.location.href);
}

// Initialize the script
function initialize() {
if (!window.location.search.includes('autoDownload=true') && !isDownloadPage()) {
// Add download buttons only if not in auto download mode and not on a download page
document.querySelectorAll('video').forEach(addDownloadButton);

new MutationObserver(() => {
document.querySelectorAll('video').forEach(addDownloadButton);
}).observe(document.body, { childList: true, subtree: true });
} else {
// Trigger automatic download if in the new tab
triggerAutoDownload();
}
}

initialize();
})();

Δημοσίευση απάντησης

Συνδεθείτε για να δημοσιεύσετε μια απάντηση.