Skip to content

Instantly share code, notes, and snippets.

@Ketchup901
Created May 9, 2019 13:55
Show Gist options
  • Save Ketchup901/aee71a29453ee0ef59406e20e3d994b1 to your computer and use it in GitHub Desktop.
Save Ketchup901/aee71a29453ee0ef59406e20e3d994b1 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Change Youtube to English
// @version 1.2.3
// @description Sets a Cookie which sets the Youtube language to English
// @author Ketchup901
// @match *://www.youtube.com/*
// @exclude *://www.youtube.com/tv*
// @exclude *://www.youtube.com/embed/*
// @exclude *://www.youtube.com/live_chat*
// @exclude *://www.youtube.com/feed/subscriptions?flow=2*
// @run-at document-start
// @grant none
// ==/UserScript==
(function() {
'use strict';
function start() {
var cookie = getPref(),
pref = "hl=en";
if(cookie === "fIsAlreadySet") {
return;
} else if(cookie !== "noPref"){
for(var i = 0; i < cookie.length; ++i) {
pref = pref + "&" + cookie[i].key + "=" + cookie[i].value;
}
}
changePref(pref);
}
function changePref(values) {
var d = new Date();
d.setTime(d.getTime() + (100*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = "PREF=" + values + ";" + expires + ";domain=.youtube.com;hostonly=false;path=/";
location.reload();
}
function getPref() {
var cookie = document.cookie,
splitC = cookie.split(";");
for(var i = 0; i < splitC.length; ++i) {
if(splitC[i].trim().indexOf("PREF") === 0) {
if(splitC[i].trim().indexOf("hl=en") > -1) {
return "fIsAlreadySet";
}
var c = [],
splitValues = splitC[i].substring(5).split("&");
for(var k = 0; k < splitValues.length; ++k) {
var splitV = splitValues[k].split("=");
if(splitV[0] !== "hl") {
var kv = {};
kv.key = splitV[0];
kv.value = splitV[1];
c.push(kv);
}
}
return c;
}
}
return "noPref";
}
start();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment