Skip to content

Instantly share code, notes, and snippets.

@osteele
Last active June 9, 2021 11:01
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 osteele/9014804352b06ed3b2bfc21241903563 to your computer and use it in GitHub Desktop.
Save osteele/9014804352b06ed3b2bfc21241903563 to your computer and use it in GitHub Desktop.
GitHub Pages -> GitHub Repo bookmarklet

Toggle between GitHub Repo and GitHub Pages

If the current page is a GitHub Pages page, open the corresponding repo. if the current page is a repo, open the GitHub Pages page.

For example, if the current page is https://osteele.github.io/p5.layers, clicking the bookmarklet navigates https://github.com/osteele/p5.layers, and vice versa.

Limitations:

  • Does not respect custom domains (CNAME)
  • Does not work for organization repos

How to Use

  1. Add a new bookmark. (For example, bookmark this page.)

  2. Rename the bookmark. For example, GHP↔︎GH.

  3. Edit the bookmark address. Replace it with this:

    javascript:window.location.href%3Dwindow.location.href.replace%28/https%3F%3A%5C/%5C/%28%5B%5E%5C/%5D%2B%29%5C.github%5C.io%5C/%28%5B%5E%5C/%5D%2A%29.%2A/%2C%27%3Agithub.com/%241/%242%27%29.replace%28/%5Ehttps%3A%5C/%5C/github%5C.com%5C/%28.%2B%3F%29%5C/%28%5B%5E%5C/%5D%2B%29.%2A/%2C%27%3A%241.github.io/%242%27%29.replace%28/%5E%3A/%2C%27https%3A%5C/%5C/%27%29

<a href="javascript:window.location.href%3Dwindow.location.href.replace%28/^https%3F%3A%5C/%5C/%28%5B%5E%5C/%5D%2B%29%5C.github%5C.io%5C/%28%5B%5E%5C/%5D%2A%29.%2A/%2C%27%3Agithub.com/%241/%242%27%29.replace%28/%5Ehttps%3A%5C/%5C/github%5C.com%5C/%28.%2B%3F%29%5C/%28%5B%5E%5C/%5D%2B%29.%2A/%2C%27%3A%241.github.io/%242%27%29.replace%28/%5E%3A/%2C%27https%3A%5C/%5C/%27%29">G2H</a>
window.location.href = window.location.href
.replace(/^https?:\/\/([^\/]+)\.github\.io\/([^\/]*).*/, ":github.com/$1/$2")
.replace(/^https:\/\/github\.com\/(.+?)\/([^\/]+).*/, ":$1.github.io/$2")
.replace(/^:/, "https://")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment