user1965515
user1965515

Reputation: 33

chrome extension to open link in new tab - no error, but does nothing on click

I'm trying to create a really simple chrome extension to open a hard-coded link in a new tab when clicked, and I'm not having any luck. After adding the extension the icon shows up, but nothing happens when I click on it. Any suggestions?

manifest.json

{
"name": "Drive Button",
"version": "1.0",
"manifest_version": 2,
"description": "Open Google Drive",
"browser_action": {
"default_icon": "icon.png"
},
"background": "background.html",
"permissions": [
"tabs"
]
}

background.html

<html>
<head>
<script>
// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.create({url: "http://drive.google.com"});
});
</script>
</head>
</html>

Upvotes: 3

Views: 4914

Answers (1)

Sudarshan
Sudarshan

Reputation: 18564

There are some problems in your script

  • Manifest registration
  • CSP

Manifest registration

You should register background as

"background": {
    "scripts": ["background.js"]
  },

or

"background": {
    "page": "background.html"
  },

CSP

If you are looking to have background as a html page eliminate <script> tag in html page to adhere Content Security Policy.

After Eliminating these problems i got your script running.

Working Version

manifest.json

Registered background page to manifest file.

{
    "name": "Drive Button",
    "version": "1.0",
    "manifest_version": 2,
    "description": "Open Google Drive",
    "browser_action": {
        "default_icon": "icon.png"
    },
    "background": {
        "scripts": [
            "background.js"
        ]
    },
    "permissions": [
        "tabs"
    ]
}

background.js

Used your code without any modifications.

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

I was able to create window as expected.

References

Upvotes: 10

Related Questions