Update: This doesn’t work as intended anymore. A new article offering an alternative method that works for modern Firefox versions has been posted here.
Note: this is tested only in Firefox. It works perfectly there, but in other browsers you may either get the icon only temporary (if the bookmark icon cache is part of the normal cache) or no icon at all. However it won’t break anything anywhere, so that’s no reason to not use it.
I investigated this a while back and it’s really quite simple, but apparently few people are aware of it:
The problem is that apparently very few Bookmarklet developers know how to provide icons. Hopefully my little post can change that at least a bit
It’s really quite simple: Bookmarklets usually have this form:
Go ahead, click it . You’ll see a nice little play button as icon. Drag the title bar onto the bookmarks toolbar and you’ll see that it retains the icon and as an added bonus the title. Don’t worry about the HTML code bloating up your Bookmarklet, we won’t have to include it directly in the final version, but it’s easier to understand this way.
So, now we need a branch so that it will show the content only when clicked on the original page. Again, there are different ways, for now we’ll just stick with the easiest one of checking for a variable that we are reasonably sure will only exist on the original page:
Now, we only need the document content if the variable exists, so we can use it to transport the content instead of including it literally in the bookmarklet, bringing the size of the Bookmarklet down dramatically and freeing us from the requirement to have really compact HTML:
You could of course also introduce some randomness into the id if you’re afraid websites will try to block it, I’ll leave that up to you.
At the end of the day, you’re whole bookmarklet could then look something like this:
window[id]='<!DOCTYPE html><html><head><title>Next</title><link rel=”icon” type=”image/png” href=”http://www.tapper-ware.net/devel/js/JS.Bookmarklets/icons/next.png” /></head><body>This is a bookmarklet. Drag its tab to your bookmarks toolbar to add it. Click the resulting button on any page to automatically find links containing the word next</body></html>';
There are numerous way to automate this or make it prettier, but I’ll leave that to you. So long