RaviTeja
RaviTeja

Reputation: 1026

How to make chrome remember the one tab(or a window) information that can be used in other tabs

In a chrome Extension , How to make chrome remember the tab information that can be used by other tabs.

For example or being more specific., I've a button in one of the tabs that when clicked opens another tab and i used chrome.createtabs for this in Background.html. I also created a variable which will save the tab id of the new tab created (say tab1). I want to use tab1 in myscript.js(content script) to place the information in parent tab. So how can i send the request from background.html to content script?

Or Is there any way to post the content to the webpage using background.html?

btw I used localStorage['tab1'] to save the new Tab ID.

Lemme know if i'm not clear. Thanks

Upvotes: 1

Views: 375

Answers (1)

serg
serg

Reputation: 111265

It's all described in Message Passing with examples. To send a message from a background page to a content script:

background.html
===============
chrome.tabs.getSelected(null, function(tab) {
  chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function(response) {
    console.log(response.farewell);
  });
});

content_script.js
=================
chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});
    else
      sendResponse({}); // snub them.
  });

Upvotes: 1

Related Questions