Created
May 5, 2019 08:09
-
-
Save scambier/a5e4098b9aa96d6c6c966568216f2d6b to your computer and use it in GitHub Desktop.
Userscript: Twitter Media Download
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Twitter Video Download | |
// @namespace http://scambier.github.io/ | |
// @version 0.1 | |
// @description Adds an "Open video" link for tweets whith an embedded video | |
// @author Simon Cambier | |
// @match https://twitter.com/* | |
// @grant none | |
// @run-at document-idle | |
// ==/UserScript== | |
(function () { | |
'use strict' | |
setInterval(() => { | |
createLink() | |
}, 100) | |
function createLink() { | |
// If our custom link already exists, do nothing | |
if (document.querySelectorAll('[data-media-download]')[0]) return | |
// Find tweet | |
const tweet = document.getElementsByClassName('permalink-inner permalink-tweet-container')[0] | |
if (!tweet) return | |
// Find video element | |
const video = tweet.querySelectorAll('video')[0] | |
if (!video) return | |
const videoSrc = video.getAttribute('src') | |
// Find actions bar | |
const actions = tweet.getElementsByClassName('ProfileTweet-actionList')[0] | |
if (!actions) return | |
// Create link container | |
const newItem = document.createElement('div') | |
newItem.className = 'ProfileTweet-action' | |
// Create the download link | |
const link = document.createElement('a') | |
link.className = 'ProfileTweet-actionButton js-actionButton' | |
link.innerHTML = 'Open video' | |
link.setAttribute('href', videoSrc) | |
link.setAttribute('data-media-download', '') | |
// Append elements | |
newItem.appendChild(link) | |
actions.appendChild(newItem) | |
} | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment