Javascript消息传递到填充表

时间:2018-06-29 12:29:37

标签: javascript html google-chrome-extension

你好,我有以下代码,我试图将两条消息从confirm.js传递到background.js脚本。该消息包含当前URL,以及用户是否选择从确认对话框继续。我正在尝试在window.html表中创建新行,并在每列中填充网址和点击状态。

用于填充表的信息在background.js的console.log中显示为request.continued和request.url。

Confirm.js

if (confirm("User continued")) {
    chrome.runtime.sendMessage({type:'continued', url: window.location.href, continued: "yes"});
} else {
    alert ("You pressed Cancel!");
    chrome.runtime.sendMessage({type:'cancelled', url: window.location.href, continued: "no"});
}

Background.js

chrome.runtime.onMessage.addListener(function(request) {
if (request.type === 'continued') {
    console.log(request.continued, request.url);
    chrome.tabs.create({
        url: chrome.extension.getURL('dialog.html'),            
        active: false
            });
}
//NON WORKING ATTEMPT INCLUDED FOR CLARITY PURPOSES

w = chrome.extension.getBackgroundPage()

var table = w.document.getElementById("myTable");
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = request.continued;
cell2.innerHTML = request.url;
});

Window.html

<!DOCTYPE html><html>
    <head>
        <script type="text/javascript" src="background.js"></script>
    </head>
    <body>
        <table border="1" cellpadding="3" style="border-collapse:collapse;">
            <tr>
                <td nowrap>URL:</td>
                <td align="right"><span id="total">CLICKED</span></td> **ROWS & COLUMNS UNDER HERE**
            </tr>
        </table>
    </body>
</html>

主要问题区域围绕着动态生成表行并使用标识HTML元素。

0 个答案:

没有答案