Skip to content

Instantly share code, notes, and snippets.

@123jimin
Created December 3, 2015 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 123jimin/f5fdc3ae99e5a3e0197e to your computer and use it in GitHub Desktop.
Save 123jimin/f5fdc3ae99e5a3e0197e to your computer and use it in GitHub Desktop.
Embedded Youtube videos to iframe-based Youtube videos
// ==UserScript==
// @name YouTube Flash Player Replacer
// @namespace http://0xF.kr/script/
// @version 0.1
// @description try to take over the world!
// @author JiminP
// @match https://namu.wiki/w/*
// @grant none
// ==/UserScript==
/* jshint -W097 */
'use strict';
// Your code here...
// <iframe width="420" height="315" src="https://www.youtube.com/embed/VEAy700YGuU" frameborder="0" allowfullscreen></iframe>
var embed_regex = /(?:[\/.]|^)youtube\.[^\/]+\/v\/([A-Za-z0-9_\-]{11})/i;
var assign_nonempty = function(f, t, n){
if(f.hasAttribute(n)) t.setAttribute(n, f.getAttribute(n));
};
var truthy = function(v){
if(!v) return false;
if(v === 'false' || v === 'False' || v === 'FALSE') return false;
if(v === '0') return false;
return true;
};
window.addEventListener('load', function(){
var replaced = [];
[].forEach.call(window.document.getElementsByTagName('object'), function(root){
var children = [].slice.call(root.children),
params = {};
var old_video_url, matches,
new_video_url = "";
var new_video_elem = null;
children.forEach(function(param){
if(param.tagName.toUpperCase() === "PARAM")
params[param.name] = param.value;
});
if(!('movie' in params)) return;
old_video_url = params['movie'];
if(old_video_url.indexOf("youtube.") === -1) return;
matches = embed_regex.exec(old_video_url);
if(!matches) return;
new_video_url = "https://www.youtube.com/embed/" + matches[1];
new_video_elem = window.document.createElement('iframe');
new_video_elem.src = new_video_url;
new_video_elem.setAttribute('frameborder', '0');
assign_nonempty(root, new_video_elem, 'width');
assign_nonempty(root, new_video_elem, 'height');
if(truthy(params['allowFullScreen']))
new_video_elem.setAttribute('allowfullscreen', 'true');
root.parentNode.insertBefore(new_video_elem, root.nextSibling);
root.parentNode.removeChild(root);
replaced.push(matches[1]);
});
[].forEach.call(window.document.getElementsByTagName('embed'), function(root){
var old_video_url, matches,
new_video_url = "";
var new_video_elem = null;
if(!('src' in root)) return;
old_video_url = root.src;
if(old_video_url.indexOf("youtube.") === -1) return;
matches = embed_regex.exec(old_video_url);
if(!matches) return;
new_video_url = "https://www.youtube.com/embed/" + matches[1];
new_video_elem = window.document.createElement('iframe');
new_video_elem.src = new_video_url;
new_video_elem.setAttribute('frameborder', '0');
assign_nonempty(root, new_video_elem, 'width');
assign_nonempty(root, new_video_elem, 'height');
assign_nonempty(root, new_video_elem, 'allowfullscreen');
root.parentNode.insertBefore(new_video_elem, root.nextSibling);
root.parentNode.removeChild(root);
replaced.push(matches[1]);
});
replaced.length && console.info(replaced.length+" YouTube video"+(replaced.length==1 ? " was" : "s were")+" replaced from Flash-based to HTML5-based.");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment