2012-03-03 75 views
0

我正在使用Google Chrome擴展程序。我在我的popup.js中有一個函數,它填充表update_table。我想從發送重要數據的background.js中調用該函數。問題是background.js上的update_table未定義,如果我將代碼複製到該文件中,則不更新popup.html上的表。從背景調用彈出功能。 (Google Chrome擴展程序)

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> 
+1

[如何與彈出窗口中的background.js進行交互?](http://stackoverflow.com/questions/9537435/how-to-interact-with-background-js-from-the-彈出) – 2012-03-03 21:48:42

+0

3種方式:chrome.extension。(getViews,sendRequest或Port) – hamczu 2012-03-03 22:33:50

回答

0

this post,我只是回答了另一個人。

你最好打賭看看chrome.extension.* API。 我將在明天的某個時間在GitHub上發佈一個示例,以便向您展示。

希望這會有所幫助。

UPDATE

Here you go.這是一個套接字劇本我創建允許後臺頁面和彈出窗口之間連續通信。自述文件中提供了一個示例。我目前使用這個擴展,每個實例最多可以傳遞100個項目,並且它非常完美。 :P

相關問題