Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1081398 of Add a button to the toolbar

  • Revision slug: Mozilla/Add-ons/WebExtensions/Add_a_button_to_the_toolbar
  • Revision title: Add a button to the toolbar
  • Revision id: 1081398
  • Created:
  • Creator: wbamberg
  • Is current revision? No
  • Comment

Revision Content

{{AddonSidebar}}

Toolbar buttons are one of the main UI components available to WebExtensions. Toolbar buttons live in the main browser toolbar and contain an icon. When the user clicks the icon, one of two things can happen:

  • if you have specified a popup for the icon, the popup is shown. Popups are transient dialogs specified using HTML, CSS, and JavaScript.
  • if you have not shown a popup, a click event is generated, that you can listen for in your code.

In WebExtensions these kinds of buttons are called "browser actions".

  • the manifest.json key browser_action is used to define the button
  • the JavaScript API browserAction is used to listen for clicks and to change the button from your code.

A simple button

In this section we'll create a WebExtension that adds a button to the toolbar. When the user clicks the button, we'll open https://developer.mozilla.org in a new tab.

First, create a new directory, "button", and create a file called manifest.json with the following contents:

{

  "description": "Demonstrating toolbar buttons",
  "manifest_version": 2,
  "name": "button-demo",
  "version": "1.0",

  "background": {
    "scripts": ["background.js"]
  },
 
  "browser_action": {
    "default_icon": {
      "16": "icons/page-16.png",
      "32": "icons/page-32.png"
    }
  }

}

This specifies that we'll have a background script named "background.js", and a browser action whose icons will live in the "icons" directory.

These icons are from the bitsies! iconset created by Recep Kütük.

So let's create the "icons" directory, and copy two icons there:

  • "page-16.png" ()
  • "page-32.png" ().
 

Next, create "background.js" in the add-on's root directory, and give it the following contents:

function openPage() {
  chrome.tabs.create({
    url: "https://developer.mozilla.org"
  });
}

chrome.browserAction.onClicked.addListener(openPage);

This just listens for the browser action's click event, and opens the page in the event listener, using the tabs API.

At this point the complete add-on should look like this:

button/

    icons/
        page-16.png
        page-32.png

    background.js
    manifest.json

Now install the WebExtension and click the button:

{{EmbedYouTube("kwwTowgT-Ys")}}

Adding a popup

Let's try adding a popup to the button. Replace manifest.json with this:

{

  "description": "Demonstrating toolbar buttons",
  "manifest_version": 2,
  "name": "button-demo",
  "version": "1.0",
 
  "browser_action": {
    "browser_style": true,
    "default_popup": "popup/choose_page.html",
    "default_icon": {
      "16": "icons/page-16.png",
      "32": "icons/page-32.png"
    }
  }

}

We've made three changes from the original:

  • We've removed "background.js", because we're now handling the logic in the popup's script (you are allowed background.js as well as a popup, it's just that we don't need it in this case).
  • We've added "browser_style": true, which will help the styling of our popup look more like part of the browser.
  • Finally, we've added "default_popup": "popup/choose_page.html", which is telling the browser that this browser action now wants a popup, the document for which it can find at "popup/choose_page.html".

So now create a directory called "popup" and in that directory, create a file called "choose_page.html". Give it the following contents:

<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="choose_page.css"/>
  </head>

<body>
  <div class="page-choice">developer.mozilla.org</div>
  <div class="page-choice">support.mozilla.org</div>
  <div class="page-choice">addons.mozilla.org</div>
  <script src="choose_page.js"></script>
</body>

</html>

You can see that this is a normal HTML page, containing three div elements, each with the name of a Mozilla site. It also includes a CSS file and a JavaScript file, which we'll add next.

Create a file called "choose_page.css", and give it these contents:

html, body {
  width: 300px;
}

.page-choice {
  width: 100%;
  padding: 4px;
  font-size: 1.5em;
  text-align: center;
  cursor: pointer;
}

.page-choice:hover {
  background-color: #CFF2F2;
}

This is just a bit of styling for our choices.

Next, create "choose_page.js", and give it these contents:

document.addEventListener("click", function(e) {
  if (!e.target.classList.contains("page-choice")) {
    return;
  }

  var chosenPage = "https://" + e.target.textContent;
  chrome.tabs.create({
    url: chosenPage
  });

});

We listen for clicks on the choices, and open a new tab containing the corresponding page. Note that we can use the WebExtension APIs in the popup's script, just as we can in the background script.

Now the add-on should look like this:

button/

    icons/
        page-16.png
        page-32.png

    popup/
        choose_page.css
        choose_page.html
        choose_page.js

    manifest.json

Now reload the add-on, click the button again, and try clicking on the choices in the popup:

{{EmbedYouTube("QPEh1L1xq0Y")}}

Page actions

Page actions are just like browser actions, except that they are for actions which are relevant only for particular pages, rather than the browser as a whole.

While browser actions are always shown, page actions are only shown in tabs where they are relevant. Page action button are displayed in the URL bar, rather than the browser toolbar.

Learn more

Revision Source

<div>{{AddonSidebar}}</div>

<p>Toolbar buttons are one of the main UI components available to WebExtensions. Toolbar buttons live in the main browser toolbar and contain an icon. When the user clicks the icon, one of two things can happen:</p>

<ul>
 <li>if you have specified a popup for the icon, the popup is shown. Popups are transient dialogs specified using HTML, CSS, and JavaScript.</li>
 <li>if you have not shown a popup, a click event is generated, that you can listen for in your code.</li>
</ul>

<p>In WebExtensions these kinds of buttons are called "browser actions".</p>

<ul>
 <li>the manifest.json key <code><a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/browser_action">browser_action</a></code> is used to define the button</li>
 <li>the JavaScript API <code><a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/API/browserAction">browserAction</a></code> is used to listen for clicks and to change the button from your code.</li>
</ul>

<h2 id="A_simple_button">A simple button</h2>

<p>In this section we'll create a WebExtension that adds a button to the toolbar. When the user clicks the button, we'll open <a href="https://developer.mozilla.org">https://developer.mozilla.org</a> in a new tab.</p>

<p>First, create a new directory, "button", and create a file called manifest.json with the following contents:</p>

<pre class="brush: json">
{

  "description": "Demonstrating toolbar buttons",
  "manifest_version": 2,
  "name": "button-demo",
  "version": "1.0",

  "background": {
    "scripts": ["background.js"]
  },
 
  "browser_action": {
    "default_icon": {
      "16": "icons/page-16.png",
      "32": "icons/page-32.png"
    }
  }

}</pre>

<p>This specifies that we'll have a <a href="/en-US/Add-ons/WebExtensions/Anatomy_of_a_WebExtension#Background_scripts">background script</a> named "background.js", and a browser action whose icons will live in the "icons" directory.</p>

<div class="pull-aside">
<div class="moreinfo">These icons are from the <a href="https://www.iconfinder.com/iconsets/bitsies">bitsies!</a> iconset created by Recep Kütük.</div>
</div>

<p>So let's create the "icons" directory, and copy two icons there:</p>

<ul>
 <li>"page-16.png" (<img alt="" src="https://mdn.mozillademos.org/files/13476/page-16.png" style="height:16px; width:16px" />)</li>
 <li>"page-32.png" (<img alt="" src="https://mdn.mozillademos.org/files/13478/page-32.png" style="height:32px; width:32px" />).</li>
</ul>

<div style="clear:both">&nbsp;</div>

<p>Next, create "background.js" in the add-on's root directory, and give it the following contents:</p>

<pre class="brush: js">
function openPage() {
  chrome.tabs.create({
    url: "https://developer.mozilla.org"
  });
}

chrome.browserAction.onClicked.addListener(openPage);</pre>

<p>This just listens for the browser action's click event, and opens the page in the event listener, using the <code><a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs">tabs</a></code> API.</p>

<p>At this point the complete add-on should look like this:</p>

<pre class="line-numbers  language-html">
<code class="language-html">button/

    icons/
        page-16.png
        page-32.png

    background.js
    manifest.json</code></pre>

<p>Now <a href="https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Temporary_Installation_in_Firefox">install the WebExtension</a> and click the button:</p>

<p>{{EmbedYouTube("kwwTowgT-Ys")}}</p>

<h2 id="Adding_a_popup">Adding a popup</h2>

<p>Let's try adding a popup to the button. Replace manifest.json with this:</p>

<pre class="brush: json">
{

  "description": "Demonstrating toolbar buttons",
  "manifest_version": 2,
  "name": "button-demo",
  "version": "1.0",
 
  "browser_action": {
    "browser_style": true,
    "default_popup": "popup/choose_page.html",
    "default_icon": {
      "16": "icons/page-16.png",
      "32": "icons/page-32.png"
    }
  }

}</pre>

<p>We've made three changes from the original:</p>

<ul>
 <li>We've removed "background.js", because we're now handling the logic in the popup's script (you are allowed background.js as well as a popup, it's just that we don't need it in this case).</li>
 <li>We've added <code>"browser_style": true</code>, which will help the styling of our popup look more like part of the browser.</li>
 <li>Finally, we've added <code>"default_popup": "popup/choose_page.html"</code>, which is telling the browser that this browser action now wants a popup, the document for which it can find at "popup/choose_page.html".</li>
</ul>

<p>So now create a directory called "popup" and in that directory, create a file called "choose_page.html". Give it the following contents:</p>

<pre class="brush: html">
&lt;!DOCTYPE html&gt;

&lt;html&gt;
  &lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;link rel="stylesheet" href="choose_page.css"/&gt;
  &lt;/head&gt;

&lt;body&gt;
  &lt;div class="page-choice"&gt;developer.mozilla.org&lt;/div&gt;
  &lt;div class="page-choice"&gt;support.mozilla.org&lt;/div&gt;
  &lt;div class="page-choice"&gt;addons.mozilla.org&lt;/div&gt;
  &lt;script src="choose_page.js"&gt;&lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;</pre>

<p>You can see that this is a normal HTML page, containing three <a href="/en-US/docs/Web/HTML/Element/div">div</a> elements, each with the name of a Mozilla site. It also includes a CSS file and a JavaScript file, which we'll add next.</p>

<p>Create a file called "choose_page.css", and give it these contents:</p>

<pre class="brush: css">
html, body {
  width: 300px;
}

.page-choice {
  width: 100%;
  padding: 4px;
  font-size: 1.5em;
  text-align: center;
  cursor: pointer;
}

.page-choice:hover {
  background-color: #CFF2F2;
}</pre>

<p>This is just a bit of styling for our choices.</p>

<p>Next, create "choose_page.js", and give it these contents:</p>

<pre class="brush: js">
document.addEventListener("click", function(e) {
  if (!e.target.classList.contains("page-choice")) {
    return;
  }

  var chosenPage = "https://" + e.target.textContent;
  chrome.tabs.create({
    url: chosenPage
  });

});</pre>

<p>We listen for clicks on the choices, and open a new tab containing the corresponding page. Note that we can use the WebExtension APIs in the popup's script, just as we can in the background script.</p>

<p>Now the add-on should look like this:</p>

<pre>
button/

    icons/
        page-16.png
        page-32.png

    popup/
        choose_page.css
        choose_page.html
        choose_page.js

    manifest.json</pre>

<p>Now reload the add-on, click the button again, and try clicking on the choices in the popup:</p>

<p>{{EmbedYouTube("QPEh1L1xq0Y")}}</p>

<h2>Page actions</h2>

<p><a href="/en-US/Add-ons/WebExtensions/Anatomy_of_a_WebExtension#Page_actions">Page actions</a> are just like browser actions, except that they are for actions which are relevant only for particular pages, rather than the browser as a whole.</p>

<p>While browser actions are always shown, page actions are only shown in tabs where they are relevant. Page action button are displayed in the URL bar, rather than the browser toolbar.</p>

<h2 id="Learn_more">Learn more</h2>

<ul>
 <li><code><a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/browser_action">browser_action</a></code> manifest key</li>
 <li><code><a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/API/browserAction">browserAction</a></code> API</li>
 <li>Browser action examples:
  <ul>
   <li><a href="https://github.com/mdn/webextensions-examples/tree/master/beastify">beastify</a></li>
   <li><a class="external external-icon" href="https://github.com/mdn/webextensions-examples/tree/master/bookmark-it">Bookmark it!</a></li>
   <li><a class="external external-icon" href="https://github.com/mdn/webextensions-examples/tree/master/favourite-colour">favourite-colour</a></li>
   <li><a class="external external-icon" href="https://github.com/mdn/webextensions-examples/tree/master/inpage-toolbar-ui">inpage-toolbar-ui</a></li>
   <li><a class="external external-icon" href="https://github.com/mdn/webextensions-examples/tree/master/open-my-page-button">open-my-page-button</a></li>
  </ul>
 </li>
 <li><code><a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/page_action">page_action</a></code> manifest key</li>
 <li><code><a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/API/pageAction">pageAction</a></code> API</li>
 <li>Page action examples:
  <ul>
   <li><a href="/mdn/webextensions-examples/tree/master/chill-out">chill-out</a></li>
  </ul>
 </li>
</ul>
Revert to this revision