Skip to content

Instantly share code, notes, and snippets.

@leobenkel
Created April 12, 2020 07:35
Show Gist options
  • Save leobenkel/7668898fc05d1931ef8758d6fa815a18 to your computer and use it in GitHub Desktop.
Save leobenkel/7668898fc05d1931ef8758d6fa815a18 to your computer and use it in GitHub Desktop.
Redirect to embed video when you open a youtube video link
// ==UserScript==
// @name Redirect to embed for youtube
// @namespace com.leobenkel
// @version 0.1
// @description Redirect to embed video when you open a youtube video link
// @author Leo Benkel
// @match https://www.youtube.com/watch?v=*
// @grant none
// ==/UserScript==
(function() {
'use strict';
var input = window.location.href;
var getArgs = input.split('?')[1];
var getPairs = getArgs.split("&").map(function(x) {
var pair = x.split("=")
return pair;
})
.reduce(function(map, obj) {
map[obj[0]] = obj[1];
return map;
}, {});
var videoId = getPairs.v;
var validPairs = {'v': videoId, 'feature': "emb_rel_end"};
var validKeys = Object.keys(validPairs);
var values = Object.values(getPairs);
var keys = Object.keys(getPairs);
var myArray = keys.map(function(e, i) {
return [e, values[i]];
});
var filteredValues = myArray.filter(function(pair) {
var key = pair[0];
var value = pair[1];
console.log(key);
console.log(value);
var isValid = validKeys.includes(key) && validPairs[key] == value;
return !isValid;
});
if (filteredValues.length == 0) {
var url = 'https://www.youtube.com/embed/' + videoId
window.location = url
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment