Chrome extension: how to open a link in a new tab?

In my Stackoverflow folder, I have stackoverflow.ico and 2 bellow files. When you import it into Chrome, it shows an icon in the address bar, but when I click on it, Chrome does not open any new tabs. What am I doing wrong?

manifest.json

 { "name": "Stackoverflow", "version": "1", "browser_action": { "default_icon": "stackoverflow.ico" }, "background": { "page": "index.html" }, "permissions": ["tabs"], "manifest_version": 2 } 

index.html

 <html> <head> <script> chrome.browserAction.onClicked.addListener(function(activeTab) { var newURL = "http://stackoverflow.com/"; chrome.tabs.create({ url: newURL }); }); </script> </head> </html> 
+42
javascript html google-chrome google-chrome-extension tabs
May 12 '13 at 3:22
source share
1 answer

The problem is that you are violating the version 2 manifest of the content security policy . To fix all you have to do is get rid of the built-in script, in this case your background page . Rotate it into your script background as follows:

manifest.json

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

background.js

 chrome.browserAction.onClicked.addListener(function(activeTab){ var newURL = "http://stackoverflow.com/"; chrome.tabs.create({ url: newURL }); }); 

If for some reason you need this to be a page, just add the script as an external file and declare it as a page, as before.

+63
May 12 '13 at 5:46
source share
— -



All Articles