-1

I need some help understanding something.

What i have so far is extension parsing page and injecting link on match. What i want is to open my extension /tab.html and fill input field there with a variable that i get from the content script. So far i got this working.

I have in background.js this

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
   chrome.tabs.create({url: chrome.extension.getURL('tab.html')}, function(tab) {
      request_s = request.x+' '+request.y;
      console.log(request_s);
   });
});

which works.

I have the tab opened and the request, but no matter what i do it seems I can't get to change the value of the input field in tab.html I tried with $('#id').val(request_s), but as it seems dom is not ready, so nothing happens.

So i tried to add in background

chrome.tabs.onUpdated.addListener(function(tabid, changeInfo, tab) {
   if(changeInfo.status == "complete" && tab.url == chrome.extension.getURL('tab.html')) {
      console.log($('body'));
   }
});

and log body innerhtml contains only the scripts of tab.html . Any advice what should i do to access tab.html dom when ready?

abraham
  • 46,583
  • 10
  • 100
  • 152

2 Answers2

1

None of the code that you have shown every runs in a content script (and, thus, never in tab.html)

You should look into https://developer.chrome.com/extensions/content_scripts and chrome.tabs.executeScript

Also, you should look into onMessage documentation and the sendResponse parameter. And if you want to sendResponse after an async call (like tabs.create) you need to return true.

Scott
  • 604
  • 3
  • 7
  • Well i don't need sendResponse actually and i tried the chrome.tabs.executeScript approach, but i get `Error in response handler for tabs.executeScript: Cannot access contents of url "chrome-extension://ekhpenipecnadolfejkncbclgjahokjh/tab.html". Extension manifest must request permission to access this host.` What i believe is the issue is that `console.log($('body'));` gets the body of _generated_background_page.html instead of tab.html So how can i select the body of tab.html from the background.js? – Branislav Kirilov Jul 18 '14 at 06:28
  • if `ekhpenipecnadolfejkncbclgjahokjh` is your extensions, then running code in tab.html is not a content script -- it's an extension view. if not, then you can't run code in that tabs. Yes, $('body') gets the document.body element of the view that the code is running in -- which is _generated_background_page.html. I suggest that you read the documentation cover to cover. – Scott Jul 18 '14 at 15:41
  • You obviously don't understand what i want to achieve. Yes ekhpenipecnadolfejkncbclgjahokjh is my extension. Telling somebody to go read the documentation is not very helpful. If i have found the information there i would not ask here. Meanwhile I will accept XAN's asnwer, because I got it working thanks to his link. – Branislav Kirilov Jul 19 '14 at 07:42
0

See this answer I wrote for a similar request - open an extension tab and pass data to it.

The idea is to save the data you want to pass, and then request it from the tab you opened, when it's ready.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206
  • This solution is really not pretty with all the back and forward messages, but i got it working. Needed few extra vars and changes to the code there, but it works. Thanks – Branislav Kirilov Jul 19 '14 at 07:44
  • If your request is not long, you can try fitting it in URL parameters, i.e. opening `tab.html?p1=v1&p2=v2` and parsing it from `tab.html`. – Xan Jul 19 '14 at 07:45