Greasy Fork

Greasy Fork is available in English.

TikTok Video Save As (Full HD) + photo link redirect

Save TikTok video as a file with the video ID and username as the file name when middle-clicked or Alt+Right Clicked

目前为 2024-06-18 提交的版本,查看 最新版本

// ==UserScript==
// @name         TikTok Video Save As (Full HD) + photo link redirect
// @namespace    http://tampermonkey.net/
// @version      0.9.4
// @description  Save TikTok video as a file with the video ID and username as the file name when middle-clicked or Alt+Right Clicked
// @match        https://www.tiktok.com/@*
// @match        tiktok.com/@*
// @match        https://discord.com/channels/*
// @grant        GM_xmlhttpRequest
// @grant        GM_openInTab
// @icon         https://tikwm.com/favicon.ico
// ==/UserScript==

(function() {
  'use strict';
  var tikTokVideoIdRegex = /\/video\/(\d+)(\/|$)/;
  var discordTikTokUrlRegex = /https:\/\/www.tiktok.com\/(\w+)\/video\/(\d+)/;
  var tikTokPhotoIdRegex = /\/@[\w.]+\/photo\/(\d+)/;

  document.addEventListener('auxclick', function(event) {
    if (event.button === 1 || (event.button === 2 && event.altKey)) { // middle click or Alt+Right Click
      var link = event.target.closest('a');
      if (link && link.href) {
        var tikTokVideoIdMatch = link.href.match(tikTokVideoIdRegex);
        var discordTikTokUrlMatch = link.href.match(discordTikTokUrlRegex);
        var tikTokPhotoIdMatch = link.href.match(tikTokPhotoIdRegex);
        if (tikTokVideoIdMatch) {
          var videoId = tikTokVideoIdMatch[1];
          var username = link.href.match(/\/@([\w.]+)/)[1];
          var newUrl = `https://tikwm.com/video/media/hdplay/${videoId}.mp4`; // Replace with your new link
          var fileName = `${videoId} @${username}.mp4`; // Change made here
          downloadFile(newUrl, fileName);
          event.stopPropagation();
          event.preventDefault();
          return false;
        } else if (discordTikTokUrlMatch) {
          // Handle Discord's URL pattern
          var videoId = discordTikTokUrlMatch[2];
          var username = discordTikTokUrlMatch[1];
          var newUrl = `https://tikwm.com/video/media/hdplay/${videoId}.mp4`; // Replace with your new link
          var fileName = `${videoId} @${username}.mp4`; // Change made here
          downloadFile(newUrl, fileName);
          event.stopPropagation();
          event.preventDefault();
          return false;
        } else if (tikTokPhotoIdMatch) {
          var photoId = tikTokPhotoIdMatch[1];
          var tikwmUrl = `https://tikwm.com/video/${photoId}.html`;
          GM_openInTab(tikwmUrl, {active: false});
          event.stopPropagation();
          event.preventDefault();
          return false;
        }
      }
    }
  });

  function downloadFile(url, fileName) {
    GM_xmlhttpRequest({
      method: 'GET',
      url: url,
      responseType: 'arraybuffer',
      onload: function(response) {
        var blob = new Blob([response.response], {type: 'video/mp4'});
        var url = URL.createObjectURL(blob);
        var a = document.createElement('a');
        a.href = url;
        a.download = fileName;
        a.click();
      },
      onerror: function(response) {
        console.error('Error downloading file:', response.status, response.statusText);
      }
    });
  }
})();