Last active
September 19, 2018 19:47
-
-
Save olmokramer/b4dd182a2e12be816a8344d98f854434 to your computer and use it in GitHub Desktop.
Rewrite/redirect URLs
This file contains hidden or 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 YouTube2HookTube | |
// @namespace https://github.com/olmokramer | |
// @description Automatically redirect YouTube to HookTube | |
// @include * | |
// @run-at document-start | |
// @version 3 | |
// @author Olmo Kramer | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
if (window.location.host.match(/youtu(\.be|be\.com)/)) { | |
window.location.host = 'hooktube.com'; | |
} | |
function replaceLinkType(type, domain) { | |
const elements = document.querySelectorAll(`[${type}*="${domain}"]`); | |
for (let i = 0; i < elements.length; i++) { | |
elements[i][type] = elements[i][type].replace(domain, 'hooktube.com'); | |
} | |
} | |
function replaceURLs() { | |
const domains = [ | |
'youtube.com', | |
'youtu.be', | |
'youtube-nocookie.com', | |
]; | |
for (let domain of domains) { | |
replaceLinkType('href', domain); | |
replaceLinkType('src', domain); | |
} | |
} | |
document.addEventListener('DOMContentLoaded', function() { | |
if (MutationObserver) { | |
const observer = new MutationObserver(replaceURLs); | |
observer.observe(document.body, { | |
childList: true, | |
subtree: true, | |
}); | |
} | |
replaceURLs(); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment