Stevie Stevie - 1 year ago 70
HTML Question

Chrome Extension: pushing information gathered before a new tab is created?

So I have a popup.html which loads a script (popup.js) as its header for the browser action.
This then makes calls to a site to get information necessary.

I got all the information necessary using XMLHTTPRequests and saved it into variables in popup.js.

Now I try to create a new tab, load a webpage and then use the information I saved to fill out a form.

chrome.tabs.create({ 'url': "https://site.html" });

This line is in popup.js

This takes my to the site desired quickly but seems to remove all the information I had previously retrieved. It does not even alert me the information I want even though I ask for it before making the tab. How would I go about creating a tab with a specific url and then using the information I gathered previously in popup.js to fill a form on it all while contained inside popup.js? Is my logic flawed? Thank you so much for any advice.

UPDATE: Ill edit this to make more sense.

this is my popup.js

function do(){
var xhr = new XMLHttpRequest();"Get", Url);
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
var dogName = this.responseText;
chrome.tabs.create({ 'url': "site.html" });

inside site.html there is a form. I was to do this to that form.

document.getElementById('form').getElementsByTagName('input')[0].value = dogName;

But it wont do anything if the line

chrome.tabs.create({ 'url': "https://site.html" });

exists, it will only take me to that page and not run
to alert anything.

By removing the create line, the extension alerts me of the information I want.

Answer Source

You will need to use an event page instead of a popup. The event page is a long-running script that will allow you to retrieve your data on-demand. The tab that is opened will also need to run a content script. This content script will send messages to the event page to retrieve the fetch XHR data and then apply the data to the form fields. Content scripts can be injected using either the manifest.json file or directly on the chrome.tabs.create function passed in as the second argument.

Note: The below code is a close example of how the files and structure should be. It has not been tested and it should be used to get a general idea of the solution.


  "content_scripts": [{
    "matches":    ["*://"],
    "js":         ["content.js"],
    "run_at":     "document_idle"



chrome.tabs.create({ 'url': "" }, function(tab1) {
  chrome.tabs.executeScript(, {file: 'content.js', run_at});


chrome.runtime.sendMessage({data: "dogName"}, function(response) {
  var dogName =;
  document.getElementById('form').getElementsByTagName('input')[0].value = dogName;


function getDogName(callback){
  var xhr = new XMLHttpRequest();"Get", Url);
  xhr.addEventListener("readystatechange", function () {
    if (this.readyState === 4) {
        var = dogName = this.responseText;
        callback({data: dogName});
  function(request, sender, sendResponse) {
    if ( === "dogName") {
      return true;

You can find more details about extension communications here: Message Passing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download