-
-
Save uahim/1bd9f7fcf0816a6ba19c to your computer and use it in GitHub Desktop.
[greasemonkey] Convert embedded YouTube and Vimeo to link
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
// ==UserScript== | |
// @name convert_iframes_to_links | |
// @namespace https://gist.github.com/ihsoy-s/ | |
// @version 0.6 | |
// @description Removes iframe/embed tags and creates links to the content | |
// @include http://*/* | |
// @include https://*/* | |
// @match http://*/* | |
// @match https://*/* | |
// @run-at document-start | |
// @copyright mihau, 2016 (original author: Yoshi-S, 2012) | |
// ==/UserScript== | |
( function(){ | |
function convertFramesToLinks(allArray, type) { | |
var el = allArray, i, el_src, textnodename; | |
// var targetarrayx = eval("years" + targetarray); | |
for (i = el.length - 1; i >= 0; i--) { | |
if( el[i].getAttribute("src") ) { | |
el_src = el[i].getAttribute("src"); | |
if ( el_src.indexOf("www.youtube") != -1 ) { | |
el_src = el_src.replace("www.youtube-nocookie.com", "www.youtube.com"); | |
el_src = el_src.replace(/\?.*/, "") | |
el_src = el_src.replace(/&.*/, "") | |
el_src = el_src.replace("/embed/", "/watch?v="); | |
el_src = el_src.replace("/v/", "/watch?v="); | |
// el_src = el_src.replace(/\?.*/, "") | |
// var replacements = { "www.youtube-nocookie.com": "www.youtube.com", | |
// "/\\?.*/": "", | |
// "/&.*/": "", | |
// "/embed/": "/watch?v=", | |
// "/v/": "/watch?v=" }; | |
// for (var r in replacements) { | |
// if (typeof replacements[r] !== "function") { | |
// el_src = el_src.replace(r,replacements[r]); | |
// } | |
// } | |
textnodename = "[ link to replaced YouTube " + type; | |
textnodesfx = " - dy 22 " + el_src.replace(/.*\?v=/,"") + " ]"; | |
} else if ( el_src.indexOf("player.vimeo.com") != -1 ) { | |
el_src = el_src.replace(/\?.*/, "") | |
el_src = el_src.replace(/&.*/, "") | |
el_src = el_src.replace("/video/", "/"); | |
el_src = el_src.replace("player.vimeo.com", "vimeo.com"); | |
textnodename = "[ link to replaced Vimeo " + type + " ]"; | |
textnodesfx = ""; | |
} else if ( el_src.indexOf("flashservice.xvideos.com") != -1 ) { | |
el_src = el_src.replace("/embedframe/", "/video"); | |
el_src = el_src.replace("flashservice.xvideos.com", "www.xvideos.com"); | |
textnodename = "[ link to replaced xvideos " + type + " ]"; | |
textnodesfx = ""; | |
} else { | |
textnodename = "[ link to replaced " + type + " ]"; | |
textnodesfx = ""; | |
} | |
var | |
linkobject = document.createElement("a"), | |
linkobject_text = document.createTextNode(textnodename + textnodesfx); | |
linkobject.setAttribute("href", el_src); | |
linkobject.setAttribute("style", "font-family:'Courier New';background: green; color: white; border: 2px dotted green; font-size:12px; margin: 4px; padding: 2px; text-decoration:none"); | |
linkobject.appendChild(linkobject_text); | |
el[i].parentNode.replaceChild(linkobject, el[i]); | |
// console.debug("[Converted iframe to link] ", el_src); | |
} | |
} | |
runstate += 1; | |
if (type == "iframe") { | |
if (embed_length > 0) { | |
convertFramesToLinks(embed, "embed"); | |
} else { | |
runstate += 1; | |
} | |
} | |
ieStats(); | |
} | |
function ieStats() { | |
if (runstate > 1) { | |
var ieStatsbuilt; | |
if ((iframe_length != 0) && (embed_length != 0)) { | |
ieStatsbuilt = "replaced\n" + iframe_length + " iframes\n" + embed_length + " embeds"; | |
} else if ((iframe_length > 0) && (embed_length == 0)) { | |
ieStatsbuilt = "replaced " + iframe_length + " iframes"; | |
} else if ((iframe_length == 0) && (embed_length > 0)) { | |
ieStatsbuilt = "replaced " + embed_length + " embeds"; | |
} | |
alert(ieStatsbuilt); | |
} | |
runstate = 0; | |
} | |
var | |
iframe = document.getElementsByTagName("iframe"), | |
embed = document.getElementsByTagName("embed"), | |
iframe_length = iframe.length, | |
embed_length = embed.length, | |
runstate = 0; | |
console.log(iframe_length, embed_length); | |
if (iframe_length > 0) { | |
convertFramesToLinks(iframe, "iframe"); | |
} else if (embed_length > 0) { | |
convertFramesToLinks(embed, "embed"); | |
} | |
}) (); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment