121

I've made myself a bookmarklet, and it functions just fine, but when added to a toolbar in Opera or Firefox, it just takes on the default bookmark icon for the browser (a globe and a star, respectively). My site has a favicon, and the window, tab and even [site] bookmark uses the favicon I've specified. Just not my bookmarklet.

How can I code my site or bookmarklet so that the bookmarklet gets the favicon, too?

I'm aware of various manual hackery techniques users can use to set the favicon after the fact, but those are undesirable solutions.

Pistos
  • 23,070
  • 14
  • 64
  • 77

9 Answers9

35

Here is how you can do this:

  1. Drag you bookmarklet to Bookmarks Bar.
  2. Next to it create a bookmark of a site which favicon you want to use for your bookmarklet.
  3. Open Bookmarks Manager, click Organize dropdown, and select Export, save your bookmarks as html file.
  4. Open that html file in text editor.
  5. Find the bookmark you just created, lets say its Gmail bookmark, you should have an html code for it, that looks like this:

<DT><A HREF="http://mail.google.com/mail/u/0/#inbox" ICON="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABV0lEQVQ4jdWQzUoCYRiFnxl/0plso0IKX7mqXWCLIlq0qEW4d19Qi6BLCELwEgpvQbyAVrroCrSNUJFGAyrkEEEMNs5PCxtRHGsZneX7nedwzgd/LQngObfnykIQOj9Disd/BFxdZ3hVwtE0Mje3kuw9OJqGWSji1BtzYafeGHk0jTdzCIA8aXANA/O6hFWuzMBWuYJ5XcI1DF6MAY8fxmxAYHdnZK7WMAtFXF3H1XXMQhGrWsN2XR5WM/QGn2MmOBkQOj5CFoJhuTKe5DUzbJvW1jbWZhbqd/4BAIGDfSQhGH7XBehLMlruEGlFzEyTZy6AvL5G+PICWQja6iJaPu8L+zbw9B4MYpyeEB4MkF7782z+AZ1OD0WNkk4vA7AUi/HUav8eYNsOnW6XZCJBJLIwvieTcVQ1SrN5j2XbUwFTf9DpdkmnUlOwJ0VRyGY3UBVl7px/qi+cdYQvZvKCUwAAAABJRU5ErkJggg==">Gmail</A>

  1. Copy the entire ICON tag
  2. In the same file find the bookmarklet you created, and insert the ICON tag you copied into your bookmarklet tag:

<DT><A HREF="javascript:(function(){... bookmarklet JS code...})();" ICON="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABV0lEQVQ4jdWQzUoCYRiFnxl/0plso0IKX7mqXWCLIlq0qEW4d19Qi6BLCELwEgpvQbyAVrroCrSNUJFGAyrkEEEMNs5PCxtRHGsZneX7nedwzgd/LQngObfnykIQOj9Disd/BFxdZ3hVwtE0Mje3kuw9OJqGWSji1BtzYafeGHk0jTdzCIA8aXANA/O6hFWuzMBWuYJ5XcI1DF6MAY8fxmxAYHdnZK7WMAtFXF3H1XXMQhGrWsN2XR5WM/QGn2MmOBkQOj5CFoJhuTKe5DUzbJvW1jbWZhbqd/4BAIGDfSQhGH7XBehLMlruEGlFzEyTZy6AvL5G+PICWQja6iJaPu8L+zbw9B4MYpyeEB4MkF7782z+AZ1OD0WNkk4vA7AUi/HUav8eYNsOnW6XZCJBJLIwvieTcVQ1SrN5j2XbUwFTf9DpdkmnUlOwJ0VRyGY3UBVl7px/qi+cdYQvZvKCUwAAAABJRU5ErkJggg==">MyBookmarklet</A>

  1. Save this file
  2. Return to Chrom Bookmarks Manager, click again Organize, and select Import
  3. Import HTML file you just edited, your bookmarklet now has a favicon.

Basically the procedure is to get ICON attribute of a bookmark tag and insert it into bookmarklet tag

enter image description here

monstro
  • 6,254
  • 10
  • 65
  • 111
  • 6
    (This is a very old question, but...) I was looking for a solution which didn't need any end-user action at all (other than adding the bookmarklet to the toolbar). i.e. what can a site owner do to specify the icon. – Pistos Nov 15 '16 at 16:04
  • 3
    For me, this is the best answer. It works and it's explained comprehensively. Thank you very much. – tsuma534 Feb 01 '18 at 14:35
  • 1
    6+ years later and this still works like a charm. Thank you @monstro! – Gui Ambros Mar 12 '22 at 23:38
  • Thank you so much this works like a charm! – Tony Xu Jul 16 '23 at 19:16
  • You are welcome :) For myself, I just created an HTML file that contains all the bookmarklets I need with an ICON tag. Whenever I need some of my bookmarklets, I just import it back into Chrome and move desired bookmarklets onto the Bookmarks bar (from the Imported folder). – monstro Jul 17 '23 at 00:35
22

A bookmarklet uses the javascript:// schema and thus do not have a domain from which a favicon may be loaded.

So, currently there is no way for you to provide a favicon for a bookmarklet. Think about it like this: remember the whole Javascript sandbox thing - where Javascript may not access anything outside the domain of the web page where it is running? Well a bookmarklet that needs to be tied in to whatever domain for the current page you are watching, cannot be also tied in to a favicon on your own web site.

Update: According to Hans Schmucker's answer, there is a possibility to create a bookmarklet that when loaded by the browser into the bookmark menu it will generate an HTML document that has a favicon. The reasoning seems like it may work but I have yet to see something like this in action and my tests have came back negative.

Guss
  • 30,470
  • 17
  • 104
  • 128
  • 1
    The missing `domain` (e.g. http-scheme) is not the cause for icon-less bookmarklet. You can open a local HTML-file (i.e. file-scheme) that __supplies a browser-readable source__ for a _favicon_ which will then appear as icon to the bookmark. Yet this *offline persisted image* must usually be _configured manually_ for most bookmarklets. That is dependent on their source – hc_dev Feb 10 '21 at 18:30
  • @hc_dev - by "configured manually" you mean something like monstro's answer? Then yes - sure, its a neat solution - but this is not what the OP asked. The problem with comparing the domain issue with a local HTML file, is that a bookmarklet does not load a file and there is not any other document which the browser can load. If you are hinting at the answers by Hans Schmucker (that I mentioned) and Brian McAllister - then yes, there is a document, but you can either have a document or javascript running, but not both and the icon, even if set (which I didn't manage to), isn't persisted. – Guss Feb 11 '21 at 09:56
13

After reading the tapper[ware] and Restafarian site, here's the simplest solution I could come up with:

<a href="javascript:

var title = window.location.href;

if (title.indexOf('http://yourwebsite/bookmarklet/') == 0) {
    '<head><link rel=\'shortcut icon\' href=\'favicon.ico\'></head>Bookmarklet';
} else {
    (function(){document.body.appendChild(document.createElement('script')).src='http://yourwebsite/bookmarklet.js';})();
}">Click Me!</a>

Works great in Chrome and FF, but FF4 is the only browser that will save the icon in the bookmarks bar. Here's what it looks like: http://cl.ly/5WNR

  • I am trying to learn more about bookmarklets, could you explain the code a little to newbies? Thanks – Andrew Mackenzie Oct 06 '11 at 11:52
  • Sure. When you click it, it checks the current page's URL. If you're on the page where your bookmarklet is 'hosted' (line 4), it writes a link element to the page pointing to your desired favicon. Your browser will see this and download the favicon. The browser caches this and uses it the icon in your bookmark bar. If you're on any other page, it'll just do whatever your bookmarklet is supposed to do. – Brian McAllister Feb 24 '12 at 18:25
  • 5
    I don't get this. When you drag a bookmarklet to your favourites bar (like one does with the Delicious bookmarklet [here](http://www.delicious.com/tools)) what runs this code? I don't see why the bookmarklet would be clicked (and the code run) until after it's saved to the favourites bar, i.e. after the browser has settled on its icon? – dumbledad May 10 '12 at 16:23
  • @dumbledad, browsers will often change the icon of an existing bookmark in response to some events - like the bookmark being clicked, so that's fine. The main issue I have with this answer is that normally a bookmarklet *is* the Javascript that you want executed, but this example code have the bookmarklet in a downloaded file - which causes some problems: the file must be available on the web; there must be an existing document already loaded; there can't be cross-domain issues. Also - I couldn't get this to work. – Guss Feb 11 '21 at 09:45
12

That's not quite right: A bookmarklet has no domain, but it has a location (which is the bookmarklet itself) and you can assign an icon to that. After that it's a matter of how the browser saves icons (Firefox saves a bookmark's icon permanently, you may not be so lucky with other browsers).

P.S. Security doesn't even play into it, icons can come from anywhere. There is no restriction.

See http://www.tapper-ware.net/blog/?p=97

Hans Schmucker
  • 145
  • 1
  • 3
  • 3
    While the reasoning sound like it might work, the examples provided in the blog you linked at either just generate a document with an icon that Firefox may or may not show for the bookmark, or are actually bookmarklets that run Javascript on arbitrary pages - but not both (tested on Firefox 7). I'm still not convinced this work. – Guss Oct 20 '11 at 21:58
9

Based on Wizek's suggestion you can put your code into a data-uri.

data:text/html;charset=utf-8,
<html>
<link rel="shortcut icon" href="https://stackoverflow.com/favicon.ico">
<script type="text/javascript">
    alert('It works!')
</script></html>

And save all of that as a bookmark. (Try it! drag the code into your tabs bar)

Unfortunately it only works for certain cases (more below).

How It Works:

(At least in Chrome) It's similar to a bookmarklet using the format javascript: "<html>...your html code here, including a javascript tag that will run when loaded...</html>" like other solutions have suggested. In that case, the html from the page you are on will be replaced by the html from the bookmarklet, but the location remains the same and the bookmarklet itself will still not have a location so Chrome can't save a favicon for it.

In contrast, with a data-uri bookmark we go to the other page, it has it's own location, and the browser can save a favicon for it. Think of it as "Hosting a website in your browser", which you would be able to access in other computers if you sync your bookmarks. You can also use a base64 image for the favicon instead of a url if you want to keep everything local.

It has limitations.

  • When you click it, it leaves the current page and loads the page in the data. Therefore you won't be able to use it for bookmarlets that interact with the current page, only for code that you can execute in a different page.

  • Don't use // for comments. Since it will all be saved in one line, wrap them in /**/ and don't forget your semicolons

  • In FF it saved the favicon, but I was not able to set it to always open popup windows if I want to use window.open() because it doesn't allow me to save a default behaviour for data urls


As an example:

Using this technique I created a small Bookmarklet With Icon Generator. You can drag this code into your URL bar (or save it as a bookmark) to use it. It's a simple page with an input area for code, and for an icon, and then generates a bookmarklet with the icon

data:text/html;charset=utf-8,<html><head>
    <title>Bookmarklet With Icon Generator</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" ></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
    <link rel="shortcut icon" href="https://www.freefavicon.com/freefavicons/objects/plain-thumbs-up-152-237293.png">
</head>
<body>
    <div class="container">
        <div class="page-header">
            <h2>Write your javascript  and specify a favicon, then drag the button to your bookmarks bar</div>
                </h2>
        <a id="bookmarkbutton" href='' ondragend='this.click()' draggable="true" class="btn btn-primary btn-lg" role="button"> 
            Drag me to your bookmarks bar! </a><br /><br />
        <div> 
            <label for="fav_href">Favicon:</label>
            <input id="fav_href" value='https://stackoverflow.com/favicon.ico' style='width:100%'></input> </div><br />
        <div>
            <label for='ta'>Write your JavaScript below</label>
            <textarea id="ta" style="width:100%;height:50%">
setTimeout(()=>{            &%2313
    alert('hello world');   /*Use this format for comments, use %2523 instead of hash (number sign)*/ &%2313
    window.history.back();  &%2313
},200);
            </textarea></div>
  </div>
    <script type = "text/javascript">
        fav_href.onchange = ta.onchange = function(){
            bookmarkbutton.href = 'data:text/html;charset=utf-8,<html><head>'+
                '<link rel="shortcut icon" href="'+ fav_href.value +'">'+
                '<script type="text/javascript"> '+ ta.value +'<\/script>';
            };
        ta.onchange();
    </script>
</body>

Another Example: Bookmarklet to open Facebook messenger in it's own small window (might not work if your browser blocks popups by default)

data:text/html;charset=utf-8,
<html>
    <link rel="shortcut icon" href="https://facebook.com/images/messengerdotcom/favicon/favicon.ico">
    <script type="text/javascript">
        url = 'https://www.messenger.com/';
        w = 740; h = 700;
        x = parseInt( screen.availWidth/2 - w/2 );
        y = parseInt( screen.availHeight/2 - h/2 );
        nw = window.open(url,'', 'width='+ w +',height='+ h +',top='+ y +',left='+ x);
        nw.focus();
        setTimeout(()=>{ 
          window.history.back();
          window.close();  
        },200);
    </script>

Other Chrome workarounds for getting bookmarklet icons:

aljgom
  • 7,879
  • 3
  • 33
  • 28
8

Heres a nice technique that almost does what you want.

Works great on my Mac✅, but I couldn't get it to work on Windows 7⃣.❌

Use "Emoji". They are Unicode characters, that happen to also look like colorful icons. You only get to choose from a predefined list of images, but actually its not bad if all you are trying to do is give the user something to look at to remind them of what the bookmarklet does.

For example, I'm making some "security key" bookmarklet. So I use in my bookmarklet name!

So basically you see the image in the bookmarks bar

Use this site to help you find an Emoji that works for your bookmarklet: http://emojipedia.org/symbols/

Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106
Theo
  • 299
  • 2
  • 9
  • 1
    I just get a whole bunch of "font has no glyph for this codepoint" marks. Given that I'm using Firefox on Linux, that means that the fallback behaviour couldn't even find them in **any** font on the system. Am I correct in guessing that those are the Mac-specific emoji glyphs that bloggers have been telling web designers to avoid? – ssokolow Aug 15 '12 at 05:46
  • Yep. However, if you know your client is running on a computer capable of displaying emoji, is it so bad to display them? For my own personal use, I will allow the users to decide if they want to output emoji or not. If not, I Don't output any, they only get plain text. – Theo Aug 17 '12 at 00:24
  • Aside from that being a solution that could trip over countless failings in the web designer using it, I wouldn't risk the bad impression I could make when (not if) someone inevitably loads something I build on a non-mac system. – ssokolow Aug 20 '12 at 01:05
  • 1
    Upvoting this partly for practical purposes and partly for entertainment value. Emojis are the answer to all of life's problems! – Sridhar Sarnobat Feb 24 '17 at 01:01
  • Tip: use this site to copy and paste emojis you need: http://emojipedia.org/upwards-black-arrow/ . – Sridhar Sarnobat Feb 24 '17 at 01:07
  • It works fine in Windows, Firefox and Chrome. Good solution! Although it's a workaround that isn't directly answering the question it gets 90% there. – Chamilyan Nov 14 '18 at 23:37
3

It's possible to assign and modify favicon the favicon using javascript and canvas (see the amazing favicon game Defender of the Favicon). The source code of the game will help you do that (it basically rely on use of toDataUrl() function on canvas as seen on line 554 of the source).

// set favicon
if( !stupidBrowser && useIcon )
{
     var    icon=$('favicon');
     (newIcon = icon.cloneNode(true)).setAttribute('href',ctx.canvas.toDataURL());
     icon.parentNode.replaceChild(newIcon,icon);
}

What happens when a bookmarlet setting the favicon this way is clicked or saved ? I don't know but it could be nice to give it a try. Browser may save it ?

Arnaud Rinquin
  • 4,296
  • 1
  • 30
  • 51
  • 2
    (Tested in Chrome) No solution. When you change a favicon using a bookmarlet, you're still in the same location. The browser might change the icon of a bookmark of the current page, but it will not assign one to the bookmarlet. – aljgom Mar 01 '17 at 21:59
1

I think that a possible way is using unicode char in bookmarklet anchor like your icon:

http://unicode-table.com/en/#cyrillic

sifting through all the possible symbols you might find the character that resembles more to the icon you want to

stefcud
  • 2,220
  • 4
  • 27
  • 37
1

So, this is not a full solution yet, but may be a step to a working direction.

data: encoding of an icon in a data:-uri encoded html works, to my surprise.

data:text/html;charset=utf-8, <title>Separator Tab</title><link rel="shortcut icon" href="data:image/png;base64,AAABAAEAEBAAAAEAIAAoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/wAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP8AAAD/lpaW/wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/5aWlv8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACWlpb/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" /> Separator Tab

Since this is <html>, we can run <script type="javascript"> in there too.

For some bookmarklets this may already be more than enough. For others that'd like to modify the current page or at least get some info from it before opening a new tab, tough luck yet. I'll update this answer if I find a way to do that.

Wizek
  • 4,854
  • 2
  • 25
  • 52