3

I'm trying to make a Chrome Extension that on click of the extension icon opens a new tab & redirects to a url.

I've managed to find answers that redirect to a url on every new tab opening or load js but I only want it to execute when the extension icon is clicked.

Here's the broken code:

manifest.json

{
    "name": "Extension",
    "description": "Opens a new tab and redirects",
    "version": "0.2",
    "chrome_url_overrides": {
        "newtab": "redirect.html"
    },
    "manifest_version": 2
}

redirect.html

<head>
    <meta http-equiv="refresh"content="0;URL=http://www.stackoverflow.com/">
</head>

Any ideas? Thanks in advance!

meow-meow-meow
  • 508
  • 2
  • 10
  • 26
  • Possible duplicate of [Google Chrome Extensions - Open New Tab when clicking a toolbar icon](http://stackoverflow.com/questions/3188384/google-chrome-extensions-open-new-tab-when-clicking-a-toolbar-icon) – Haibara Ai Jul 04 '16 at 01:20

1 Answers1

1

See these answers: https://stackoverflow.com/a/14682627/6525260 ,and https://stackoverflow.com/a/3189335/6525260

Steps:

  1. Have manifest version 2 ("manifest_version":2,)
  2. Include tabs for permissions
  3. Listen for mouse click event on browser action in your background page (Code below)

(Mouse Click Event)

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

(now code if you chose direct url)

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

(Manifest Permissions)

"permissions":["tabs"],
Community
  • 1
  • 1
Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
  • Thanks Arnav, I poured through those answers and the links the answers refer to but none of it worked. Still getting no response on toolbar icon click. Can't seem to find a single working code example for this. – meow-meow-meow Jul 04 '16 at 01:57
  • See this [link](http://stackoverflow.com/a/21656716/6525260) Replace `_self` with `_blank` – Arnav Borborah Jul 04 '16 at 02:00