Home | History | Annotate | Download | only in intros
      1 <p>
      2 In the following figure,
      3 the multicolored square
      4 to the right of the address bar
      5 is the icon for a browser action.
      6 A popup is below the icon.
      7 </p>
      8 
      9 <img src="{{static}}/images/browser-action.png"
     10   width="363" height="226" />
     11 
     12 <p>
     13 If you want to create an icon that isn't always visible,
     14 use a <a href="pageAction.html">page action</a>
     15 instead of a browser action.
     16 </p>
     17 
     18 <h2 id="manifest">Manifest</h2>
     19 
     20 <p>
     21 Register your browser action in the
     22 <a href="manifest.html">extension manifest</a>
     23 like this:
     24 </p>
     25 
     26 <pre>{
     27   "name": "My extension",
     28   ...
     29   <b>"browser_action": {
     30     "default_icon": {                    <em>// optional</em>
     31       "19": "images/icon19.png",           <em>// optional</em>
     32       "38": "images/icon38.png"            <em>// optional</em>
     33     },
     34     "default_title": "Google Mail",      <em>// optional; shown in tooltip</em>
     35     "default_popup": "popup.html"        <em>// optional</em>
     36   }</b>,
     37   ...
     38 }</pre>
     39 
     40 <p>
     41 If you only provide one of the 19px or 38px icon size, the extension system will
     42 scale the icon you provide to the pixel density of the user's display, which
     43 can lose detail or make it look fuzzy. The old syntax for registering the
     44 default icon is still supported:
     45 </p>
     46 
     47 <pre>{
     48   "name": "My extension",
     49   ...
     50   <b>"browser_action": {
     51     ...
     52     "default_icon": "images/icon19.png"  <em>// optional</em>
     53     <em>// equivalent to "default_icon": { "19": "images/icon19.png" }</em>
     54   }</b>,
     55   ...
     56 }</pre>
     57 
     58 <h2 id="ui">Parts of the UI</h2>
     59 
     60 <p>
     61 A browser action can have an <a href="#icon">icon</a>,
     62 a <a href="#tooltip">tooltip</a>,
     63 a <a href="#badge">badge</a>,
     64 and a <a href="#popups">popup</a>.
     65 </p>
     66 
     67 <h3 id="icon">Icon</h3>
     68 
     69 <p>Browser action icons can be up to 19 dips (device-independent pixels)
     70   wide and high. Larger icons are resized to fit, but for best results,
     71   use a 19-dip square icon.</p>
     72 
     73 <p>You can set the icon in two ways:
     74   using a static image or using the
     75   HTML5 <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html">canvas element</a>.
     76   Using static images is easier for simple applications,
     77   but you can create more dynamic UIs &mdash;
     78   such as smooth animation &mdash;
     79   using the canvas element.
     80   </p>
     81 
     82 <p>Static images can be in any format WebKit can display,
     83   including BMP, GIF, ICO, JPEG, or PNG.
     84   For unpacked extensions, images must be in the PNG format.
     85   </p>
     86 
     87 <p>To set the icon,
     88   use the <b>default_icon</b> field of <b>browser_action</b>
     89   in the <a href="#manifest">manifest</a>,
     90   or call the $ref:browserAction.setIcon method.
     91   </p>
     92 
     93 <p>To properly display icon when screen pixel density (ratio
     94   <code>size_in_pixel / size_in_dip</code>) is different than 1,
     95   the icon can be defined as set of images with different sizes.
     96   The actual image to display will be selected from the set to
     97   best fit the pixel size of 19 dip icon.
     98   At the moment, the icon set can contain images with pixel sizes 19 and 38.
     99   </p>
    100 
    101 <h3 id="tooltip">Tooltip</h3>
    102 
    103 <p>
    104 To set the tooltip,
    105 use the <b>default_title</b> field of <b>browser_action</b>
    106 in the <a href="#manifest">manifest</a>,
    107 or call the $ref:browserAction.setTitle method.
    108 You can specify locale-specific strings for the <b>default_title</b> field;
    109 see <a href="i18n.html">Internationalization</a> for details.
    110 </p>
    111 
    112 <h3 id="badge">Badge</h3>
    113 
    114 <p>Browser actions can optionally display a <em>badge</em> &mdash;
    115   a bit of text that is layered over the icon.
    116   Badges make it easy to update the browser action
    117   to display a small amount of information
    118   about the state of the extension.</p>
    119 
    120 <p>Because the badge has limited space,
    121    it should have 4 characters or less.
    122   </p>
    123 
    124 <p>
    125 Set the text and color of the badge using
    126 $ref:browserAction.setBadgeText and
    127 $ref:browserAction.setBadgeBackgroundColor,
    128 respectively.
    129 
    130 </p>
    131 
    132 
    133 <h3 id="popups">Popup</h3>
    134 
    135 <p>If a browser action has a popup,
    136   the popup appears when the user clicks the icon.
    137   The popup can contain any HTML contents that you like,
    138   and it's automatically sized to fit its contents.
    139   </p>
    140 
    141 <p>
    142 To add a popup to your browser action,
    143 create an HTML file with the popup's contents.
    144 Specify the HTML file in the <b>default_popup</b> field of <b>browser_action</b>
    145 in the <a href="#manifest">manifest</a>, or call the
    146 $ref:browserAction.setPopup method.
    147 </p>
    148 
    149 <h2 id="tips">Tips</h2>
    150 
    151 <p>For the best visual impact,
    152   follow these guidelines:</p>
    153 
    154 <ul>
    155   <li><b>Do</b> use browser actions for features
    156     that make sense on most pages.
    157   <li><b>Don't</b> use browser actions for features
    158     that make sense for only a few pages.
    159     Use <a href="pageAction.html">page actions</a> instead.
    160   <li><b>Do</b> use big, colorful icons that make the most of
    161     the 19x19-pixel space.
    162     Browser action icons should seem a little bigger
    163     and heavier than page action icons.
    164   <li><b>Don't</b> attempt to mimic
    165     Google Chrome's monochrome menu icon.
    166     That doesn't work well with themes, and anyway,
    167     extensions should stand out a little.
    168   <li><b>Do</b> use alpha transparency
    169     to add soft edges to your icon.
    170     Because many people use themes,
    171     your icon should look nice
    172     on a variety of background colors.
    173   <li><b>Don't</b> constantly animate your icon.
    174     That's just annoying.
    175 </ul>
    176 
    177 <h2 id="examples"> Examples </h2>
    178 
    179 <p>
    180 You can find simple examples of using browser actions in the
    181 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/browserAction/">examples/api/browserAction</a>
    182 directory.
    183 For other examples and for help in viewing the source code, see
    184 <a href="samples.html">Samples</a>.
    185 </p>
    186