Asker
Asker

Reputation: 41

chrome content script onclick event

I'm trying to write a chrome extension and cannot seem to understand how to implement the following scenario:

here's the manifest.json:

{
  "name": "My First Extension",
  "version": "1.0",
  "description": "The first extension that I made.",
  "browser_action": {
    "default_icon": "icon.png",
    "default_title": "my title"
    },

  "content_scripts": [
      {
        "matches": ["http://*/*", "https://*/*"],
        "js": ["myscript.js"]
      }
  ],


  "permissions": [
    "tabs", "https://*/*"
  ]

}

and here's myscript.js:

alert('entered myscript.js..');

function doMagic()
{
    alert('extension button clicked!!');
}

chrome.extension.onClicked.addListener(doMagic);

i know im missing something really obvious, but cant seem to figure it out from the docs, other sites, etc.!!

Upvotes: 2

Views: 2678

Answers (1)

abraham
abraham

Reputation: 47833

Don't use a content_script, you really only need those if have to have access to the HTML of the tab.

Use a background_page for the onClicked listener and chrome.tabs.update to redirect the page.

function doMagic(tab) {
  chrome.tabs.update(tab.id, { url: 'http://www.google.com' });
}
chrome.browserAction.onClicked.addListener(doMagic);

Upvotes: 5

Related Questions