Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Chrome Extension to open a link in a new tab

I would like to create a simple chrome extension that when clicked opens a url in a new browser tab. This is what I have for the manifest.json

{
    "name": "Sprout Social",
    "description": "Shortcut to Sprout Social",
    "permissions": [
        "tabs"
    ],
    "icons": {
        "128": "128.png"
    },
    "launch": {
        "web_url": "http://www.sproutsocial.com"
    }
}

Any help would be great.

like image 957
user704739 Avatar asked Mar 30 '12 20:03

user704739


2 Answers

Ok, first of all, manifest.json (not jason) has a strict structure, you can't mess with it.

https://developer.chrome.com/extensions/manifest.html

You have to create a Browser Action extension, that means your extension will have a button near the tool button.

https://developer.chrome.com/extensions/browserAction.html

You don't need any popup.html, you can skip that part. You need to write your background page, many people name it background.html This HTML file will have your code, in this form:

<html><head><script> your script here (use as many lines as you want)  </script></head>/html>

This HTML won't never show up.

And the code can be anything you want, like the code in the other answer:

chrome.browserAction.onClicked.addListener(function() {
    chrome.tabs.create({'url': "http://www.sproutsocial.com"});
});

And that's it.

like image 137
Alejandro Silvestri Avatar answered Oct 24 '22 09:10

Alejandro Silvestri


Simpler solution, you do not need HTML.

Add this to manifest.json

"browser_action": {
    "default_icon": "images/icon38.png",
    "default_title": "Your title"
},
"background": {
    "scripts": ["background.js"],
    "persistent": false
}

Create the background.js file, with this code:

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.create({ url: "http://www.yoursite.com" });
});

Note: I do not add "permissions": ["tabs"] in manifest.json since it adds the Permission Warning: "Read your browsing history" and that can be confusing to the user. The extension still works.

like image 32
IvanRF Avatar answered Oct 24 '22 10:10

IvanRF