Last active
April 1, 2017 18:35
-
-
Save mrosata/b705f304cf92358e02d65427907063eb to your computer and use it in GitHub Desktop.
Browser Snippet to quickly get a download link for a video displayed on current page. Only tested on a couple sites. Targets 'src' attribute on <video> element or <source> element.
This file contains 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
/** | |
* Create a download link for video displayed on current page and | |
* put the link into either window.SELECTOR if set, or body, or if | |
* site is reconized by script it should add download link under the | |
* video. | |
*/ | |
(function(decoratee) { | |
// Tries to get and display a link | |
attemptToGetAndRenderDownloadLink(); | |
// Creates the download link | |
function createLink(to, download = false) { | |
const a = document.createElement('a'); | |
const s = document.createElement('strong'); | |
a.setAttribute('download', download); | |
a.setAttribute('href', to); | |
s.append( | |
document.createTextNode('-Download Link-') | |
); | |
a.appendChild(s); | |
return a; | |
} | |
// Creates an element that holds all elems as sibling elements. | |
function siblings(...elems) { | |
const frag = document.createDocumentFragment(); | |
elems.map(elem => frag.appendChild(elem)); | |
return frag; | |
} | |
// Where should the download link be displayed? | |
function defaultSelector({location:{origin=''}, SELECTOR:custom}=window) { | |
if ( custom ) { | |
return custom | |
} | |
if (origin.match(/egghead\.io/)) { | |
return '.description-text-holder' | |
} | |
if (origin.match(/(----\.io)|(------\.com)/)) { | |
return '#reader-content .panel p' | |
} | |
// defaultSelector | |
return 'body' | |
} | |
// Tries to get the video link from video element then source element. | |
function getVideoLink() { | |
const video = document.querySelector('video'); | |
let link; | |
if (!video) { | |
console.warn('No <video> element on the page'); | |
return void(link); | |
} | |
link = video.getAttribute('src'); | |
if (video.querySelector && video.querySelector('source')) { | |
const source = video.querySelector('source'); | |
if (source && source.getAttribute('src')) { | |
link = source.getAttribute('src'); | |
} | |
} | |
return link; | |
} | |
// "main" - try to find src, make link, add to current webpage | |
function attemptToGetAndRenderDownloadLink() { | |
const link = getVideoLink(); | |
if (!link) { | |
console.warn('Could not find a link for video.'); | |
return void(link) | |
} | |
else { | |
console.info(`Creating a link to download video @${link}`); | |
} | |
const theDownloadLink = createLink(link, true, 'hello'); | |
const selector = defaultSelector(decoratee); | |
const displayElem = siblings( | |
document.createElement('br') | |
, theDownloadLink | |
); | |
document | |
.querySelector(selector) | |
.appendChild(displayElem); | |
return true | |
} | |
// Add the functions as methods to window or passed in decoratee | |
// object. This won't clobber anything. | |
decoratee.attemptToGetAndRenderDownloadLink = decoratee.attemptToGetAndRenderDownloadLink || attemptToGetAndRenderDownloadLink; | |
decoratee.getVideoLink = decoratee.getVideoLink || getVideoLink; | |
decoratee.siblings = decoratee.siblings || siblings; | |
decoratee.createLink = decoratee.createLink || createLink | |
}(window || {})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment