【问题标题】:Calling a function on popup from background. (Google Chrome Extension)从后台调用弹出函数。 (谷歌浏览器扩展)
【发布时间】:2012-03-03 21:40:59
【问题描述】:

我正在使用 google chrome 扩展程序。我的 popup.js 中有一个填充表格 update_table 的函数。我想从发送重要数据的 background.js 调用该函数。问题是 background.js 上的 update_table 没有定义,如果我将代码复制到该文件中,则不要更新 popup.html 上的表。

popup.js

function update_table(content, morecontent){
  if (!document.getElementsByTagName) return;
      tabBody=document.getElementsByTagName("tbody").item(0);
      row=document.createElement("tr");
      cell1 = document.createElement("td");
      cell2 = document.createElement("td");
      textnode1=document.createTextNode(content);
      textnode2=document.createTextNode(morecontent);
      <!-- ... -->
}

popup.html

<!doctype html>
<html>
<head>
<title>Popup Page</title>
<script src="./popup.js" type="text/javascript"></script>
</head>
<body>
   <table border='1' id='mytable'>
      <tbody>
        <tr><td>22</td><td>333</td></tr>
        <tr><td>22</td><td>333</td></tr>
      </tbody>
   </table>
</body>
</html>

background.js

chrome.webRequest.onCompleted.addListener(
    function(request) {
        update_table(request.type, request.url);
    },
    {urls: ["*://*/*"]},
    ["responseHeaders"]
);

background.html

<!doctype html>
<html>
<head>
   <title>Background Page</title>
   <script src="./background.js" type="text/javascript"></script>
</head>
<body>
</body>
</html>

【问题讨论】:

标签: javascript google-chrome-extension


【解决方案1】:

阅读this post,我只是为另一个人回答这个问题。

你最好看chrome.extension.* API。 明天某个时候我会在 GitHub 上发布一个示例,我可以向您展示。

希望这会有所帮助。

更新

Here you go. 这是我创建的一个套接字脚本,它允许后台页面和弹出窗口之间的持续通信。自述文件中提供了一个示例。我目前将其用于每个实例最多传递 100 个项目的扩展,并且效果很好。 :P

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-15
    • 2011-04-19
    • 1970-01-01
    • 2015-04-17
    • 2014-07-24
    相关资源
    最近更新 更多