Skip to content

Instantly share code, notes, and snippets.

@myfonj
Created May 5, 2024 23:42
Show Gist options
  • Save myfonj/13abb0fa04a2fc1113f8d12a873f5c14 to your computer and use it in GitHub Desktop.
Save myfonj/13abb0fa04a2fc1113f8d12a873f5c14 to your computer and use it in GitHub Desktop.
javascript:/*pub↔edit*/(function (mappings) {
const currentURL = String(document.location.href);
const currentHost = document.location.hostname;
const currentProtocol = (document.location.protocol + '//');
const gi = '.github.io';
const gr = /github.com\/([^\/]+)\/\1.github.io\/edit\/([^\/]+)\//;
let gh_handle = '';
if ( currentHost.endsWith(gi) ) {
gh_handle = currentHost.replace(gi,'');
} else if(gr.test(currentURL)) {
const match = currentURL.match(gr);
if (match && match[1]) {
gh_handle = match[1];
}
}
if( gh_handle ) {
mappings = [[
`github.com/${gh_handle}/${gh_handle}.github.io/edit/master/`,
`${gh_handle}.github.io/`
]];
}
mappings.forEach(prefixes => {
prefixes.forEach((prefix, index) => {
if (currentURL.startsWith(currentProtocol + prefix)) {
const nextPrefix = prefixes[index + 1] || prefixes[0];
const newURL = currentURL.replace(prefix, nextPrefix);
document.location.href = newURL;
}
});
})
})([
/*
Place hostnames/URL prefixes without protocols in here
like so:
['www.prod.ext/','edit.int/'],
['some.prod/', 'our.dev/UAT/', 'our.dev/INT/', 'et.cetera/whatever'],
*/
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment