bluszcz
bluszcz

Reputation: 4128

chrome extensions - Content Scripts on demand

I wrote my small extension for Chrome which works in the context of the webpage.

Everything is fine, except that code is executed every time when I visit URL defined in the manifest.json in content-scripts matches.

What I would like is to launch it manually - 'on demand' - after clicking on the icon of the extension next to the url bar.

Is this possible?

Upvotes: 0

Views: 704

Answers (1)

Ido Green
Ido Green

Reputation: 2813

Yes - it is possible. I've took it from: http://developer.chrome.com/extensions/content_scripts.html

The relevant part is: "...To insert code into a page, your extension must have cross-origin permissions for the page. It also must be able to use the chrome.tabs module. You can get both kinds of permission using the manifest file's permissions field. Once you have permissions set up, you can inject JavaScript into a page by calling executeScript()..."

/* in background.html */
chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript(null,
    {code:"document.body.bgColor='red'"});
});

Upvotes: 1

Related Questions