Skip to content

Instantly share code, notes, and snippets.

@thomasjbradley
Last active February 23, 2017 11:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thomasjbradley/5f6a9fbe9a8847c5299d to your computer and use it in GitHub Desktop.
Save thomasjbradley/5f6a9fbe9a8847c5299d to your computer and use it in GitHub Desktop.
GitHub Pages Switcher Bookmarklet
/**
* Detects the GitHub URL and redirects:
* - From Repo to Pages
* - or from Pages to Repo
*/
javascript:(function () {
var url = document.location.href,
username,
repo,
finalUrl
;
if (!url.match(/github/)) { return; }
url = url.replace(/https?\:\/\//, '');
if (url.match(/github\.io/)) {
username = url.match(/.+\.github/)[0].replace(/\.github/, '');
repo = url.match(/\.github\.io\/([^\/]+)/)[1];
finalUrl = 'https://github.com/' + username + '/' + repo;
} else {
username = url.match(/github.com\/([^\/]+)/)[1];
repo = url.match(new RegExp('github\.com\/' + username +'/([^\/]+)'))[1];
finalUrl = 'http://' + username + '.github.io/' + repo;
}
window.open(finalUrl);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment