Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save StephaneTy-Pro/7f7e4cb4eaa12f74390d2076ff7dc768 to your computer and use it in GitHub Desktop.
Save StephaneTy-Pro/7f7e4cb4eaa12f74390d2076ff7dc768 to your computer and use it in GitHub Desktop.
[javascript] bookmarklet - the missing way to search only your own gists, including sorting by stars or forks

TL;DR

  • base URL is: https://gist.github.com/search
  • use q=user:your_username+your_search_term
  • include s=stars and o=desc to sort by "most stars" (most stars on top, least stars at the end).
  • include s=stars and o=asc to sort by "least stars" (least stars on top, most stars at the end).
  • same goes for s=forks .
  • same goes for s=updated .
  • if you do not include s=, it will sort by "best match" .

2022-10-22_195313

for example: https://gist.github.com/search?&s=stars&o=desc&q=user%3Aeladkarako+ffmpeg
will search for my gists for ffmpeg, sorting them so the ones with the most stars will be on top.

making a bookmarklet so the search term will be dynamic (using JavaScript's native inputbox - prompt) is very easy,
use this "template", change the username and optionally the default value for the prompt, currently ffmpeg:
javascript:top.location.assign("https://gist.github.com/search?&s=stars&o=desc&q=user%3Aeladkarako+"+encodeURIComponent(prompt("ffmpeg")));

make sure to properly escape the search term using encodeURIComponent,

  • the current version will search the exact term, replacing whitespace with %20,
    a better way is to also replace any whitespace with + using something like this encodeURIComponent(prompt("ffmpeg")).replace(/%20/gm,"+") or
    encodeURIComponent(prompt("ffmpeg").replace(/\s+/gm,"+")).replace(/%2B/gmi,"+")

make sure to do not include whitespaces,
(this is still a URL, and it will get encoded to a pretty ugly %20).

note:
a best practice for a bookmarklet is to always explicitly specify the top frame location,
instead of just calling location, or window.location,
although it should probably work anyway.

keep in-mind a bookmarklet is kind of a protocol,
old browsers supported it,
but newer browsers, as well as websites with Content-Security-Policy header with a strict javascript limitation,
might break bookmarklets, luckily it works fine in GitHub...

finally,
add a new bookmark to your bookmarks toolbar,
give it a nice title, I usually add some emoji on bookmarklets.

2022-10-22_195018

you can use Win+. on Windows to open the emoji toolbar,
or copy one from https://eladkarako.github.io/umoji/index.html
note: all the ones from there are having an additional \uFE0E which makes them use the black/white variation of the emoji,
you can paste it into a notepad2/++ and place your cursor at the end, and press one time on the BackSpace to remove that character,
it will get the default colorful variation..

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