Twitter Video Download

Adds a button to download video from a tweet

От 15.07.2022. Виж последната версия.

  1. // ==UserScript==
  2. // @name Twitter Video Download
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0.5
  5. // @description Adds a button to download video from a tweet
  6. // @run-at document-idle
  7. // @author naileD
  8. // @include https://twitter.com/*
  9. // @include https://mobile.twitter.com/*
  10. // @icon https://www.google.com/s2/favicons?domain=twitter.com
  11. // @grant none
  12. // @license Unlicense
  13. // ==/UserScript==
  14.  
  15. 'use strict';
  16. setInterval(() => {
  17. var main = document.querySelector("main[role='main'] section[role='region']");
  18. if (!main) return;
  19. var react = Object.entries(main.parentElement).find(el => el[0].startsWith("__reactFiber"))[1];
  20. if (!react.memoizedProps.children.length) return;
  21. var tweet = react.memoizedProps.children.filter(el => (el || {})._owner).map(el => el._owner.memoizedProps.focalTweet).filter(el => el)[0];
  22. if (!tweet || !tweet.extended_entities || !tweet.extended_entities.media || !tweet.extended_entities.media[0].video_info) return;
  23. var el = document.querySelector(`a[href*="${tweet.id_str}"]`);
  24. if (!el) return;
  25. while (el.tagName !== "ARTICLE") { el = el.parentElement; }
  26. el = el.querySelector(`[id^="id"][role="group"]`);
  27. if (!el) return;
  28. if (el.lastElementChild.tagName === "A") return;
  29. var video = tweet.extended_entities.media[0].video_info.variants.filter(v => v.content_type == "video/mp4").sort((a,b) => b.bitrate - a.bitrate)[0].url.replace(new RegExp("\\?tag=.*"), "");
  30. var color = el.firstElementChild.style.color || "#536471";
  31. var svg = `<svg width="1.5em" height="1.5em" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="square" stroke-linejoin="arcs">
  32. <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>`;
  33. el.insertAdjacentHTML("beforeend", `<a href="${video}" target="_blank" style="display: flex; place-self: center; color: ${color};" title="Download Video">${svg}</a>`);
  34. }, 1000);