Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...

like image 556
Alon Gubkin Avatar asked Jul 06 '10 16:07

Alon Gubkin


People also ask

How do I get links to open in a new tab automatically in Chrome?

Use Mouse or Trackpad Only If you use a mouse, simply utilizing the middle mouse button to click on a link will immediately open it in a new browser tab! Holding down the Shift key while middle-clicking also helps you switch to the tab automatically.

Why does Google Chrome open a new tab everytime I click a link?

Plugins and extensions cause Chrome to open links in new tabs. To eliminate this problem, all you need to do is disable them.

How do I stop a new tab from opening when I click a link?

Some links are coded to open in the current tab while others open in a new tab. To take control of this behavior, press Ctrl when you click a link to stay on your current page while opening the link in a new tab in the background.


2 Answers

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 }); }); 

like image 187
Abhishek Mehta Avatar answered Oct 05 '22 20:10

Abhishek Mehta


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.

like image 40
Mohamed Mansour Avatar answered Oct 05 '22 21:10

Mohamed Mansour