Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 265
Annapoorni D Avatar asked Jan 24 '26 19:01

Annapoorni D


1 Answers

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.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!