Annapoorni D
Annapoorni D

Reputation: 849

How to pass values to new tab opened by chrome.tabs.create?

I am new to writing extensions for Chrome. I am trying to write a simple extension that will open a new tab with the specified url, on a click of the extension icon and need to pass a value to it so that this value is filled in the input area (ex: input for search) of the specified url.

I am successful in opening the new tab with the given url on clicking the icon. I used background script to listen for the event on the icon and open a tab, the script is as follows:

chrome.browserAction.onClicked.addListener(function(tab) {

  chrome.tabs.create({'url': 'www.google.com'}, function(tab1) {
    // Tab opened.
  });
});

Now I am confused about what method will allow me to send some values to the new tab and use the value and perform some operation there like, if I pass "java api" I have to put this in the search area of the google page. I tried looking the Chrome extension docs but it is confusing as to what to use?

Upvotes: 3

Views: 4176

Answers (1)

Wladimir Palant
Wladimir Palant

Reputation: 57651

You should use chrome.tabs.executeScript() to run a content script in this tab:

chrome.tabs.create(..., function(tab1) {
  chrome.tabs.executeScript(tab1.id, {file: ...});
});

This content script will then be able to do something with the tab contents. If it needs some data from your extension it will have to send a message.

Upvotes: 2

Related Questions