Advertisement
Guest User

Twitter Video Download with error massage

a guest
Aug 5th, 2024
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Twitter Video Download
  3. // @namespace    http://tampermonkey.net/
  4. // @version      1.0.8
  5. // @description  Adds a button to download video from a tweet
  6. // @run-at       document-idle
  7. // @author       naileD
  8. // @match        https://x.com/*
  9. // @match        https://mobile.x.com/*
  10. // @match        https://twitter.com/*
  11. // @match        https://mobile.twitter.com/*
  12. // @icon         https://www.google.com/s2/favicons?domain=x.com
  13. // @grant        none
  14. // @license      Unlicense
  15. // @downloadURL https://update.greasyfork.org/scripts/434883/Twitter%20Video%20Download.user.js
  16. // @updateURL https://update.greasyfork.org/scripts/434883/Twitter%20Video%20Download.meta.js
  17. // ==/UserScript==
  18.  
  19. 'use strict';
  20. setInterval(() => {
  21.   try {
  22.     var main = document.querySelector("main[role='main'] section[role='region']");
  23.     if (!main) throw new Error("Main section not found.");
  24.  
  25.     var react = Object.entries(main.parentElement).find(el => el[0].startsWith("__reactFiber"));
  26.     if (!react || !react[1] || !react[1].memoizedProps.children.length) throw new Error("React component not found.");
  27.  
  28.     var tweet = react[1].memoizedProps.children.filter(el => (el || {})._owner).map(el => el._owner.memoizedProps.focalTweet).filter(el => el)[0];
  29.     if (!tweet || !tweet.extended_entities || !tweet.extended_entities.media || !tweet.extended_entities.media.find(el => el.video_info)) {
  30.       throw new Error("No video found in the tweet.");
  31.     }
  32.  
  33.     var el = document.querySelector(`a[href*="${tweet.id_str}"]`);
  34.     if (!el) throw new Error("Tweet link not found.");
  35.  
  36.     while (el.tagName !== "ARTICLE") { el = el.parentElement; }
  37.     el = el.querySelector(`[id^="id"][role="group"]`);
  38.     if (!el) throw new Error("Media group not found.");
  39.  
  40.     if (el.lastElementChild.tagName === "A") return;
  41.  
  42.     var videos = tweet.extended_entities.media.filter(el => el.video_info).map(el => el.video_info.variants.filter(v => v.content_type == "video/mp4").sort((a,b) => b.bitrate - a.bitrate)[0].url.replace(new RegExp("\\?tag=.*"), ""));
  43.     var color = el.firstElementChild.style.color || "#536471";
  44.     var svg = `<svg width="1.5em" height="1.5em" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="square" stroke-linejoin="arcs">
  45.           <g><path d="M18 14v5a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8c0-1.1.9-2 2-2h5M15 3h6v6M10 14L20.2 3.8"/></g></svg>`;
  46.  
  47.     videos.forEach(video => el.insertAdjacentHTML("beforeend", `<a href="${video}" target="_blank" style="display: flex; place-self: center;  color: ${color};" title="Download Video">${svg}</a>`));
  48.   } catch (error) {
  49.     console.error("Error: " + error.message); // Log error message to the console
  50.   }
  51. }, 1000);
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement