Skip to content

Instantly share code, notes, and snippets.

@sardisson
Last active July 8, 2017 20:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sardisson/ba3646b1d30b847b6ad4a523573093e1 to your computer and use it in GitHub Desktop.
Save sardisson/ba3646b1d30b847b6ad4a523573093e1 to your computer and use it in GitHub Desktop.
Turns javascript: links (bookmarklets) into links that are bookmarkable in Mobile Safari (which can then be edited back into working bookmarklets)
(function() {
var i, elms = document.getElementsByTagName("a");
var bH = window.location.href.split(location.search||location.hash||/[?#]/)[0];
for (i=0; i<elms.length; i++) {
if (elms[i].href.lastIndexOf("javascript:", 0) === 0) {
elms[i].href = elms[i].href.replace("javascript:", bH+"?bookmarklet#javascript:");
}
}
})();
@sardisson
Copy link
Author

sardisson commented Jul 7, 2017

This is a simple bookmarklet that will turn any javascript: link into a link that can be clicked, loaded, and bookmarked in Mobile Safari. Because Mobile Safari does not support adding bookmarklets directly (only bookmarking the page that is currently loaded, or syncing bookmarks from a desktop browser), adding a bookmarklet on iOS is a cumbersome, multi-step process—especially if the author of the bookmarklet hasn't made a Mobile Safari-friendly link. The process generally involves bookmarking something else and then editing in the bookmarklet code. With this JavaScript installed as a bookmarklet, the steps involved are "reduced" to:

  1. On a page with a bookmarklet you want to add (and which does not have an iOS-friendly link already), open your Mobile Safari bookmarks and tap iOSify Bookmarklets.
  2. Tap the in-page link to the bookmarklet you want to add to Mobile Safari. N.B. It may appear that nothing happens, but if you tap the location bar and finger-scrub, you should see the page's URL has been changed to include the bookmarklet code after a ?bookmarklet# string. This is the query-string-and-fragment method I learned about from Digital Inspiration.
  3. Tap the Share icon and add the current page as a bookmark; you can't edit the URL at this point, so just choose Done.
  4. Tap the Bookmarks icon, then Edit, then the bookmark you just added. Edit the URL and delete everything before the javascript: in the URL. (Tap Done when finished, and Done again to exit editing mode.)

Your bookmarklet is now ready for use.

iOS-friendly bookmarkable version of iOSify Bookmarklets (tap the link, then start at step 3 above to add this to Mobile Safari): iOSify Bookmarklets

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