Javascript 在后台弹出窗口上调用函数。(谷歌浏览器扩展)
我在谷歌浏览器的一个扩展中工作。我的popup.js中有一个函数,用于填充一个表update\u table。我想从background.js调用发送重要数据的函数。问题是background.js上的update_表没有定义,如果我将代码复制到该文件中,请不要更新popup.html上的表 popup.jsJavascript 在后台弹出窗口上调用函数。(谷歌浏览器扩展),javascript,google-chrome-extension,Javascript,Google Chrome Extension,我在谷歌浏览器的一个扩展中工作。我的popup.js中有一个函数,用于填充一个表update\u table。我想从background.js调用发送重要数据的函数。问题是background.js上的update_表没有定义,如果我将代码复制到该文件中,请不要更新popup.html上的表 popup.js function update_table(content, morecontent){ if (!document.getElementsByTagName) return;
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);
<!-- ... -->
}
chrome.webRequest.onCompleted.addListener(
function(request) {
update_table(request.type, request.url);
},
{urls: ["*://*/*"]},
["responseHeaders"]
);
background.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>
<!doctype html>
<html>
<head>
<title>Background Page</title>
<script src="./background.js" type="text/javascript"></script>
</head>
<body>
</body>
</html>
背景页
阅读,我只是为另一个人回答这个问题
你最好的办法就是让它看起来更漂亮。
明天某个时候,我将在GitHub上发布一个示例,我可以向您展示
希望这有帮助
更新
这是我创建的一个套接字脚本,允许在后台页面和弹出窗口之间进行连续通信。自述文件中提供了一个示例。目前,我将其用于一个扩展,每个实例最多可传递100个项目,而且效果非常好:P三种方式可能重复:chrome.extension.(GetView、sendRequest或Port)