Skip to content

Instantly share code, notes, and snippets.

@scambier
Created May 5, 2019 08:09
Show Gist options
  • Save scambier/a5e4098b9aa96d6c6c966568216f2d6b to your computer and use it in GitHub Desktop.
Save scambier/a5e4098b9aa96d6c6c966568216f2d6b to your computer and use it in GitHub Desktop.
Userscript: Twitter Media Download
// ==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