Created
February 28, 2022 18:47
-
-
Save hendrixjoseph/c56645c48c2cacc9fd9866fe91c82abd to your computer and use it in GitHub Desktop.
This file contains 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 Ancestry.com/AncestryLibrary.com Switcheroo | |
// @namespace https://www.joehxblog.com/ | |
// @version 0.1 | |
// @description switch between Ancestry.com & AncestryLibary.com while maintaining URL structure | |
// @author JoeHx | |
// @match https://www.ancestry.com/* | |
// @match https://www.ancestrylibrary.com/* | |
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== | |
// @grant none | |
// ==/UserScript== | |
(() => { | |
'use strict'; | |
const ancestryDomain = 'ancestry.com'; | |
const ancestryLibraryDomain = 'ancestrylibrary.com'; | |
document.addEventListener('click', toggleLinkedSite); | |
document.addEventListener('keydown', toggleThisSite); | |
document.addEventListener('keydown', toggleAllATags); | |
function toggleThisSite(e) { | |
if (e.ctrlKey && e.code === 'ShiftLeft') { | |
let currentHref = window.location.href; | |
let newHref = switchDomain(currentHref); | |
window.location.replace(newHref); | |
} | |
} | |
function toggleAllATags(e) { | |
if (e.ctrlKey && e.code === 'ShiftRight') { | |
Array.from(document.getElementsByTagName('a')) | |
.forEach(a => { | |
a.href = switchDomain(a.href); | |
}); | |
} | |
} | |
function toggleLinkedSite(e) { | |
if (e.ctrlKey) { | |
let node = e.target; | |
while (node !== undefined && node.tagName.toLowerCase() !== 'a') { | |
node = node.parentNode; | |
} | |
if (node) { | |
let newHref = switchDomain(node.href); | |
window.location.assign(newHref); | |
} | |
} | |
} | |
function switchDomain(href) { | |
return href.includes(ancestryLibraryDomain) ? href.replace(ancestryLibraryDomain, ancestryDomain) : href.replace(ancestryDomain, ancestryLibraryDomain); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment