Google chrome 将值从background.js传递到弹出窗口
在background.js中,我创建了一个弹出窗口,如下所示:Google chrome 将值从background.js传递到弹出窗口,google-chrome,google-chrome-extension,Google Chrome,Google Chrome Extension,在background.js中,我创建了一个弹出窗口,如下所示: chrome.windows.create({ focused: true, width: 1170, url : "settings/index.html", type: "popup" }, function(popup) { tab_app = popup.id; alert(tab_app); }); 我将id存储在tab_应用程序中 如何将值从background.js传
chrome.windows.create({
focused: true,
width: 1170,
url : "settings/index.html",
type: "popup"
}, function(popup) {
tab_app = popup.id;
alert(tab_app);
});
我将id存储在tab_应用程序中
如何将值从background.js传递到弹出窗口
我试着这样做:
chrome.tabs.executeScript(tab_app, {code: "alert("+message.add+");"});
但它一直告诉我这个标签id不存在。。我假设是因为它是一个弹出窗口。谢谢你的帮助 您不能在
扩展名
页面中调用executeScript
。如果您试图在扩展页中使用executeScript
。它将显示错误:
运行tabs时未选中runtime.lastError.executeScript:无法
访问url的内容
“铬-extension://extension_id/yourPage.html".
扩展清单必须请求访问此主机的权限
现在您无法添加“chrome-extension:///yourPage.html“manifest.json中权限
下的
,因为它无效且不允许。
相反,您可以使用消息传递
background.js:
function createNewtab(){
var targetId = null;
chrome.tabs.onUpdated.addListener(function listener(tabId, changedProps) {
if (tabId != targetId || changedProps.status != "complete")
return;
chrome.tabs.onUpdated.removeListener(listener);
chrome.tabs.sendMessage(targetId, {message : "loadNewTab"},function(response){
// do nothing yet
});
});
chrome.windows.create({
focused: true,
width: 1170,
url : chrome.extension.getURL("settings/index.html"),
type: "popup"
}, function(popup) {
targetId = popup.tabs[0].id;
});
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
switch (request.message){
case "loadNewTab":
alert("HI")
break;
}
});
index.js:
function createNewtab(){
var targetId = null;
chrome.tabs.onUpdated.addListener(function listener(tabId, changedProps) {
if (tabId != targetId || changedProps.status != "complete")
return;
chrome.tabs.onUpdated.removeListener(listener);
chrome.tabs.sendMessage(targetId, {message : "loadNewTab"},function(response){
// do nothing yet
});
});
chrome.windows.create({
focused: true,
width: 1170,
url : chrome.extension.getURL("settings/index.html"),
type: "popup"
}, function(popup) {
targetId = popup.tabs[0].id;
});
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
switch (request.message){
case "loadNewTab":
alert("HI")
break;
}
});
因为它是您的扩展页,所以选择的方法是
注意:您不能使用chrome.tabs.sendMessage的每标签消息,因为这明确针对内容脚本上下文(扩展页不存在)。您需要使用“broadcast”chrome.runtime.sendMessage
,它将发送到所有其他扩展页
如果您一次可以有多个弹出式窗口,这可能是一个问题-您需要一些标识符。您可以将其作为URL参数或URL哈希传递,例如“settings/index.html?id=foo”
或“settings/index.html”\foo“
。如果您不希望出现多个弹出式窗口(在打开新窗口之前,您始终可以检查是否有一个窗口处于打开状态),那么这并不重要
如果您确实需要动态代码加载或执行,而不仅仅是传递数据(值得怀疑),那么您需要注意
- 只需创建
标记并将其添加到文档中,即可从扩展包中动态加载脚本
- 但是,默认情况下,您不能在扩展上下文中传递一个代码字符串并
eval
它。您可以将'safe-eval'
添加到CSP字符串中,但这通常是个坏主意
- 最可能的情况是,您只需要将一些命令与数据一起传递。纯粹的消息传递非常有用,只要看看文档就知道了
这可能很有用-我正在使用打开一个新选项卡并在那里传递数据来打印它。但我希望它是动态的-我希望能够在需要时使用executeScript在创建窗口或选项卡时,您必须等待它加载,然后再调用executeScript()。加载后,只需删除侦听器,并随时调用executeScript即可。我已经编辑了答案以删除listenerHey,这对我不起作用。警报没有显示。@user3800799我编辑了答案。检查它现在是否工作不,不会chrome.tabs.sendMessage
发送到内容脚本上下文。这是我在这里注册后得到的最佳答案。谢谢