Skip to content

Instantly share code, notes, and snippets.

@samthecodingman
Last active August 22, 2020 19:15
Show Gist options
  • Save samthecodingman/9632247610b64df573d3c0595bac78c9 to your computer and use it in GitHub Desktop.
Save samthecodingman/9632247610b64df573d3c0595bac78c9 to your computer and use it in GitHub Desktop.
Switch between the current GitHub repo & it's GitHub pages URL [Javascript Bookmarklet] [CC-BY License]
javascript:{let regex;window.location.href.indexOf("/github.com")>-1?(regex=new RegExp("/(\\w+)/([^/?]+)").exec(window.location.pathname))!=null?window.location.assign("https://"+regex[1]+".github.io/"+regex[2]):alert("Error: RegExp couldn't identify user and repo."):window.location.href.indexOf("github.io")>-1?(regex=new RegExp("/(\\w+).github.io/([^/?]+)").exec(window.location.href))!=null?window.location.assign("https://github.com/"+regex[1]+"/"+regex[2]):alert("Error: RegExp couldn't identify user and repo."):alert("Error: Not a GitHub page or repo.");}
@samthecodingman
Copy link
Author

samthecodingman commented Aug 22, 2020

GitHub Pages Switch Bookmarklet

How to use

To use this bookmarklet, add it as a Bookmark to your favourites bar of your browser under the 'URL' or 'Location' field. When clicked, the Javascript function will run and switch between the current repo and it's GitHub pages URL. Clicking the bookmarklet again, will swap back.

Using the bookmarklet on the following pages would switch between them.

  • https://github.com/samthecodingman/app-bottom-nav
  • https://samthecodingman.github.io/app-bottom-nav

Why use a bookmarklet?

Well, in comparison to a full plugin, a bookmarklet consists of a small amount of text that just performs the function you need. It may not be as pretty as a plugin, but it doesn't require updating, doesn't have version incompatibilities and most importantly won't actually read any of the page's contents. So there is zero snooping/logging/tracking risk.

How it works

On the surface...
The bookmarklet is actually quite primitive and simplistic, consisting of some simple error checking using the ternary operator:

isThisAGitHubRepo() ? isSoSwitchToGitHubPagesSite() : (notSoButIsThisAGitHubPagesSite() ? isSoSwitchToGitHubRepo() : alsoNotSoShowErrorMsg())

A more in depth look...
Using that analogy, isThisAGitHubRepo() checks if the current page is from the Google Web Cache by searching for the substring "github.com" in the page URL.

/* isThisAGitHubRepo() */
window.location.href.indexOf("github.com") > -1

When that check is true, the repo's username and name are pulled from the current URL and rearranged to the GitHub pages URL.

/* isSoSwitchToGitHubPagesSite() */
(regex = new RegExp("/(\\w+)/([^/?]+)").exec(window.location.pathname)) != null
    ? window.location.assign("https://" + regex[1] + ".github.io/" + regex[2])
    : alert("Error: RegExp couldn't identify user and repo.")

When that check fails, the script checks if the page is instead hosted on GitHub pages by searching for the substring "github.io" in the page URL.

/* notSoButIsThisAGitHubPagesSite() */
window.location.href.indexOf("github.io") > -1

When that check is true, the repo's username and name are pulled from the current URL and rearranged to the GitHub repo URL.

/* isSoSwitchToGitHubRepo() */
(regex = new RegExp("/(\\w+).github.io/([^/?]+)").exec(window.location.href)) != null
    ? window.location.assign("https://github.com/" + regex[1] + "/" + regex[2])
    : alert("Error: RegExp couldn't identify user and repo.")

If this check also fails, an error message is shown.

/* alsoNotSoShowErrorMsg() */
alert("Error: Not a GitHub page or repo.");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment