Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hendrixjoseph/c56645c48c2cacc9fd9866fe91c82abd to your computer and use it in GitHub Desktop.
Save hendrixjoseph/c56645c48c2cacc9fd9866fe91c82abd to your computer and use it in GitHub Desktop.
// ==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