user1094081
user1094081

Reputation:

Chrome backgroundPage issue

I'm building a Chrome extension and I designed it this way:

I have a:

How should it work: the background page (and the related js) executes and creates an HTML table.

Once I click the browser action popup icon, I retrieve that table (through getBackgroundPage) from the background page DOM and I append it in the popup.html.

What happens: the first time I click the button anything works absolutely fine.

The second time, the popup.html doesn't display the table anymore (no table is appendend): so, I found background.html still exists BUT it has no more tables inside.

What happens to backgroundPage?

Relevant code:

// reference to the background page window
    var w = chrome.extension.getBackgroundPage();

    console.log(w);

    // the background page week table
    var newWeek = w.document.getElementById("week");

    console.log(newWeek);

    // the popup week table
    var oldWeek = document.getElementById("week");

    // replace popup table with background table (first time it works, second time newWeek is null)
    oldWeek.parentNode.replaceChild(newWeek,oldWeek);

Upvotes: 0

Views: 76

Answers (1)

rsanchez
rsanchez

Reputation: 14657

You are effectively moving the table from the background page DOM tree to the popup DOM tree. When the popup is closed, the table is lost. You can try adding .cloneNode so you get a copy of the table instead:

var newWeek = w.document.getElementById("week").cloneNode(true);

Upvotes: 2

Related Questions