【问题标题】:Chrome backgroundPage issueChrome 背景页面问题
【发布时间】:2014-05-21 10:48:22
【问题描述】:

我正在构建一个 Chrome 扩展程序,我是这样设计的:

我有一个:

  • popup.html(browserAction 弹出窗口);
  • popup.js(包含在 popup.html 中);
  • background.html(后台脚本);
  • background.js(包含在 background.js 中);

它应该如何工作:后台页面(和相关的js)执行并创建一个HTML表格。

单击浏览器操作弹出图标后,我从后台页面 DOM 中检索该表(通过 getBackgroundPage)并将其附加到 popup.html 中。

会发生什么:我第一次单击按钮时,一切正常。

第二次,popup.html 不再显示表格(没有表格是追加的):所以,我发现 background.html 仍然存在,但里面没有更多表格。

backgroundPage 会发生什么?

相关代码:

// 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);

【问题讨论】:

  • 可能您在 popup.js 中的代码正在破坏表格。请将相关代码添加到您的问题中。
  • 我尽快编辑帖子,但是没有破坏后台表的代码。

标签: javascript google-chrome google-chrome-extension


【解决方案1】:

您实际上是在将表格从背景页面 DOM 树移动到弹出 DOM 树。当弹出窗口关闭时,表格将丢失。您可以尝试添加.cloneNode,以便获得表格的副本:

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

【讨论】:

  • 我不敢相信:我以为我在复制,而不是移动!非常感谢! :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-15
  • 2011-07-07
  • 1970-01-01
相关资源
最近更新 更多