繁体   English   中英

从后台在弹出窗口上调用函数。 (谷歌浏览器扩展程序)

[英]Calling a function on popup from background. (Google Chrome Extension)

我正在使用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>

阅读这篇文章 ,我只是为另一个人回答。

最好选择chrome.extension。* API 我明天将在GitHub上发布一个示例,我可以向您展示。

希望这可以帮助。

更新

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM