Skip to content

Instantly share code, notes, and snippets.

@peerreynders
Last active February 12, 2024 20:56
Show Gist options
  • Save peerreynders/a07dd70dd7cc0018dcd8b7a7a67b74bc to your computer and use it in GitHub Desktop.
Save peerreynders/a07dd70dd7cc0018dcd8b7a7a67b74bc to your computer and use it in GitHub Desktop.
Bookmarklet

Resource: Implementing bookmarklets in JavaScript

  • Implement bookmarklet as an IIFE.
  • Test the IIFE in the console for a relevant page.
  • Minify the finished bookmarklet, e.g. Minify JS Online
  • In the Bookmark Manager (Ctrl+Shift+O) choose “Add New Bookmark” under the “Organise” menu.
  • Specify a name.
  • In URL specify the javascript: scheme and delimeter and paste the minified bookmarklet at the end.

e.g.

javascript:(()=>{const t=document.location.hash;if(t.length<2&&"#"!==t[0])return;const e=document.getElementById(t.slice(1));e&&e.scrollIntoView({behavior:"smooth",block:"start",inline:"nearest"})})();
(() => {
// Force Chrome to go to anchor
const hash = document.location.hash;
// bail if there is no anchor
if (hash.length < 2 && hash[0] !== '#') return;
const target = document.getElementById(hash.slice(1));
// bail if nothing was found
if (!target) return;
target.scrollIntoView({
behavior: 'smooth',
block: 'start',
inline: 'nearest',
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment