Skip to content

Instantly share code, notes, and snippets.

@olmokramer
Last active September 19, 2018 19:47

Revisions

  1. olmokramer revised this gist Sep 19, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion url-rewriter.user.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    // @description Rewrite current url or urls on the page
    // @match *://*/*
    // @run-at document-start
    // @version 1
    // @version 2
    // @author Olmo Kramer
    // ==/UserScript==

  2. olmokramer revised this gist Sep 19, 2018. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion url-rewriter.user.js
    Original file line number Diff line number Diff line change
    @@ -77,7 +77,11 @@

    for (let el of document.querySelectorAll(selector)) {
    const old_url = el.getAttribute(attr);
    el.setAttribute(attr, rewriteURL(old_url, transform));
    const new_url = rewriteURL(old_url, transform);

    if (old_url != new_url) {
    el.setAttribute(attr, rewriteURL(old_url, transform));
    }
    }
    }

  3. olmokramer revised this gist Sep 19, 2018. 2 changed files with 108 additions and 79 deletions.
    108 changes: 108 additions & 0 deletions url-rewriter.user.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,108 @@
    // ==UserScript==
    // @name url-rewriter
    // @namespace https://github.com/olmokramer
    // @description Rewrite current url or urls on the page
    // @match *://*/*
    // @run-at document-start
    // @version 1
    // @author Olmo Kramer
    // ==/UserScript==

    (function() {
    'use strict';

    function youtube2hooktube(url) {
    url.host = 'hooktube.com';

    if (url.pathname.slice(0, 7) == '/embed/') {
    return url;
    }

    if (url.pathname.slice(0, 6) != '/watch') {
    const vid_id = url.pathname.slice(1);
    const search = url.search;

    url.pathname = '/watch'
    url.search = `?v=${vid_id}`;

    if (search) {
    url.search += `&${search.slice(1)}`;
    }
    }

    return url;
    }

    function wikiwand2wikipedia(url) {
    const [, lang, topic] = url.pathname.split('/');

    if (lang) {
    url.host = `${lang}.wikipedia.org`;
    } else {
    url.host = `en.wikipedia.org`;
    }

    if (topic) {
    url.pathname = `/wiki/${topic}`;

    if (url.hash) {
    url.hash = url.hash.slice(2);
    }
    } else {
    url.pathname = '';
    }

    return url;
    }

    const domains = {
    'www.youtube.com': youtube2hooktube,
    'youtu.be': youtube2hooktube,
    'youtube-nocookie.com': youtube2hooktube,
    'www.wikiwand.com': wikiwand2wikipedia,
    };

    const seen_urls = {};

    function rewriteURL(url, transform) {
    if (!(url in seen_urls)) {
    seen_urls[url] = transform(new URL(url)).href;
    }

    return seen_urls[url];
    }

    function rewriteAttr(attr, domain, transform) {
    const selector = `[${attr}*="${domain}"]`;

    for (let el of document.querySelectorAll(selector)) {
    const old_url = el.getAttribute(attr);
    el.setAttribute(attr, rewriteURL(old_url, transform));
    }
    }

    function rewriteURLs() {
    for (let attr of ['href', 'src']) {
    for (let [domain, transform] of Object.entries(domains)) {
    rewriteAttr(attr, domain, transform);
    }
    }
    }

    if (window.location.host in domains) {
    window.location.href = rewriteURL(window.location.href, domains[window.location.host]);
    return;
    }

    document.addEventListener('DOMContentLoaded', function() {
    new MutationObserver(function OnMutate() {
    rewriteURLs(domains);
    }).observe(document.body, {
    attributes: true,
    childList: true,
    subtree: true,
    });

    rewriteURLs(domains);
    });
    })();
    79 changes: 0 additions & 79 deletions youtube2hooktube.user.js
    Original file line number Diff line number Diff line change
    @@ -1,79 +0,0 @@
    // ==UserScript==
    // @name YouTube2HookTube
    // @namespace https://github.com/olmokramer
    // @description Automatically redirect YouTube to HookTube
    // @include *
    // @run-at document-start
    // @version 4
    // @author Olmo Kramer
    // ==/UserScript==

    (function() {
    'use strict';

    const seen_urls = {};

    function rewriteURL(url) {
    if (url in seen_urls) {
    return seen_urls[url];
    }

    url = new URL(url);
    url.host = 'hooktube.com';

    if (url.pathname.slice(0, 7) == '/embed/') {
    return url;
    }

    if (url.pathname.slice(0, 6) != '/watch') {
    const vid_id = url.pathname.slice(1);
    const search = url.search;

    url.pathname = '/watch'
    url.search = `?v=${vid_id}`;

    if (search) {
    url.search += `&${search.slice(1)}`;
    }
    }

    return seen_urls[url] = url.href;
    }

    if (window.location.host.match(/youtu(\.be|be\.com)/)) {
    window.location.href = rewriteURL(window.location);
    return;
    }

    function replaceURLs() {
    const domains = [
    'youtube.com',
    'youtu.be',
    'youtube-nocookie.com',
    ];

    const attrs = ['href', 'src'];

    for (let domain of domains) {
    for (let attr of attrs) {
    for (let el of document.querySelectorAll(`[${attr}*="${domain}"]`)) {
    const old_url = el.getAttribute(attr);
    const new_url = rewriteURL(old_url);
    if (new_url != old_url) {
    el.setAttribute(attr, rewriteURL(el.getAttribute(attr)));
    }
    }
    }
    }
    }

    document.addEventListener('DOMContentLoaded', function() {
    new MutationObserver(replaceURLs).observe(document.body, {
    attributes: true,
    childList: true,
    subtree: true,
    });

    replaceURLs();
    });
    })();
  4. olmokramer revised this gist Sep 4, 2018. 1 changed file with 49 additions and 19 deletions.
    68 changes: 49 additions & 19 deletions youtube2hooktube.user.js
    Original file line number Diff line number Diff line change
    @@ -4,22 +4,45 @@
    // @description Automatically redirect YouTube to HookTube
    // @include *
    // @run-at document-start
    // @version 3
    // @version 4
    // @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');
    const seen_urls = {};

    function rewriteURL(url) {
    if (url in seen_urls) {
    return seen_urls[url];
    }

    url = new URL(url);
    url.host = 'hooktube.com';

    if (url.pathname.slice(0, 7) == '/embed/') {
    return url;
    }

    if (url.pathname.slice(0, 6) != '/watch') {
    const vid_id = url.pathname.slice(1);
    const search = url.search;

    url.pathname = '/watch'
    url.search = `?v=${vid_id}`;

    if (search) {
    url.search += `&${search.slice(1)}`;
    }
    }

    return seen_urls[url] = url.href;
    }

    if (window.location.host.match(/youtu(\.be|be\.com)/)) {
    window.location.href = rewriteURL(window.location);
    return;
    }

    function replaceURLs() {
    @@ -29,21 +52,28 @@
    'youtube-nocookie.com',
    ];

    const attrs = ['href', 'src'];

    for (let domain of domains) {
    replaceLinkType('href', domain);
    replaceLinkType('src', domain);
    for (let attr of attrs) {
    for (let el of document.querySelectorAll(`[${attr}*="${domain}"]`)) {
    const old_url = el.getAttribute(attr);
    const new_url = rewriteURL(old_url);
    if (new_url != old_url) {
    el.setAttribute(attr, rewriteURL(el.getAttribute(attr)));
    }
    }
    }
    }
    }

    document.addEventListener('DOMContentLoaded', function() {
    if (MutationObserver) {
    const observer = new MutationObserver(replaceURLs);
    observer.observe(document.body, {
    childList: true,
    subtree: true,
    });
    }
    new MutationObserver(replaceURLs).observe(document.body, {
    attributes: true,
    childList: true,
    subtree: true,
    });

    replaceURLs();
    });
    })();
    })();
  5. olmokramer revised this gist Apr 13, 2018. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion youtube2hooktube.user.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,9 @@
    // ==/UserScript==

    (function() {
    'use strict'; if (window.location.host.match(/youtu(\.be|be\.com)/)) {
    'use strict';

    if (window.location.host.match(/youtu(\.be|be\.com)/)) {
    window.location.host = 'hooktube.com';
    }

  6. olmokramer created this gist Apr 13, 2018.
    47 changes: 47 additions & 0 deletions youtube2hooktube.user.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    // ==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();
    });
    })();