Google Chrome Extensions - Open New Tab when clicking a toolbar icon

Google ChromeGoogle Chrome-Extension

Google Chrome Problem Overview


How can I create an extension for Chrome that adds an icon to the toolbar, and when you click it, it opens a new tab with some local web page (for example: f.html)?

I saw this question, but it doesn't really explains what should I add in the manifest file...

Google Chrome Solutions


Solution 1 - Google Chrome

This is not true for newer chrome apps.

Newer chrome apps having manifest_version: 2 requires the tabs be opened as:


chrome.browserAction.onClicked.addListener(function(activeTab)
{
    var newURL = "http://www.youtube.com/watch?v=oHg5SJYRHA0";
    chrome.tabs.create({ url: newURL });
});

Solution 2 - Google Chrome

Well, in the extensions docs, it states in manifest, you would need to include "tabs" as its permission. Same way they explain the hello world application:

Manifest File:

{
  "name": "My Extension",
  "version": "1.0",
  "description": "Opens up a local webpage",
  "icons": { "128": "icon_128.png" },
  "background_page": "bg.html",
  "browser_action": {
    "default_title": "",
    "default_icon": "icon_19.png"
  },
  "permissions": [
    "tabs"
  ],
}

Within the background page, you listen to the mouse click event on the browser action.

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.create({'url': chrome.extension.getURL('f.html')}, function(tab) {
    // Tab opened.
  });
});

As you noticed above, you will see that I used the question you saw in the other post. Note, this isn't tested, but I believe it should work.

Solution 3 - Google Chrome

chrome.tabs.create need the permission of "tabs".

Simply using window.open in extension without need of any permission. and the code is shorter. I suggest this solution.

window.open(url,'_blank');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlon GubkinView Question on Stackoverflow
Solution 1 - Google ChromeAbhishek MehtaView Answer on Stackoverflow
Solution 2 - Google ChromeMohamed MansourView Answer on Stackoverflow
Solution 3 - Google ChromecuixipingView Answer on Stackoverflow