传递Javascript消息以填充表
您好,我有以下代码,我正在尝试将两条消息从confirm.js传递到background.js脚本。该消息包含当前url以及用户是否选择从确认对话框继续。我正在尝试在window.html表中创建一个新行,并用url和单击状态填充每个列 用于填充表格的信息在background.js中的console.log中显示为request.continued和request.url 确认.js传递Javascript消息以填充表,javascript,html,google-chrome-extension,Javascript,Html,Google Chrome Extension,您好,我有以下代码,我正在尝试将两条消息从confirm.js传递到background.js脚本。该消息包含当前url以及用户是否选择从确认对话框继续。我正在尝试在window.html表中创建一个新行,并用url和单击状态填充每个列 用于填充表格的信息在background.js中的console.log中显示为request.continued和request.url 确认.js if (confirm("User continued")) { chrome.runtime.sen
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"});
}
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;
});
Background.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"});
}
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元素。window.HTML是否声明为浏览器默认弹出窗口?如果是这样,它是一个单独的页面,有自己的URL,与背景页面无关,背景页面也是一个单独的页面,有自己的URL。因此background.js应该只在manifest.json中声明后台页面。至于window.html,您需要一个单独的脚本文件,只用于该窗口-注意,弹出窗口仅在显示时存在,它有自己的devtools,可以通过右键单击弹出窗口,然后单击“检查”来访问。另请参见。它当前未声明为默认弹出窗口。我不确定如何识别window.html中的html元素,并通过后台脚本对它们进行操作。我知道它们目前没有任何ID。window.html仍然是一个单独的页面,因此上述注释仍然适用。我已经包含了完整的后台代码,以显示第二次尝试访问html文件的方式chrome.windows.create对于
type:“popup”
有不同的含义,正如您在文档中看到的,它只是一种没有地址栏的窗口类型。在您的情况下,它不会做任何事情,因为您没有指定url
。