我正在开发Chrome扩展程序。 我有一个要求,即创建一个带有一些参数的新选项卡,例如javascript' window'功能
默认情况下,javascript窗口函数,我需要设置如下代码,然后window.location.replace
允许在新窗口中访问页面。
window.name = JSON.stringify({
id : 'tmp',
companyCode : companyCode,
locationCode : locationCode,
terminalNo : terminalNo,
terminalKey : terminalKey,
server:{
protocol : protocol,
remoteHost : remoteHost,
remotePort : remotePort,
clientHost : clientHost,
localPort : clientPort,
webContext : null,
gcRemoteHost : remoteHost,
gcRemotePort : remotePort,
soaPort: 9091,
webSocketPort : 9099,
}
});
现在,我正在使用google chrome api创建标签。
chrome.tabs.create({
url: new_location
});
所以,我有一个问题,我应该如何通过上面window.name
来创建一个我可以访问新位置的新标签。
答案 0 :(得分:1)
注入设置名称的内容脚本代码。
在manifest.json中允许URL(更好)或"<all_urls>"
(更糟糕,但有时候是不可避免的):
"permissions": ["http://www.example.com/*"],
将名称放入变量:
var windowName = JSON.stringify({
foo: 'bar',
});
在后台/弹出/选项页面脚本中使用chrome.tabs.executeScript将内容脚本代码作为字符串运行,并在新创建的选项卡中将嵌入的窗口名称作为参数:
chrome.tabs.create({
url: 'http://www.example.com'
}, function(tab) {
runContentCode(tab.id, injectedSetWindowName, windowName);
});
function injectedSetWindowName(name) {
window.name = name;
}
function runContentCode(tabId, fn, params) {
chrome.tabs.executeScript(tabId, {
code: '(' + fn + ')(' + JSON.stringify(params) + ')',
runAt: 'document_start',
});
}
如果您已经有一个自动执行的内容脚本(例如在manifest.json中声明),请使用messaging(chrome.tabs。sendMessage)发送窗口名称并相应地设置它。 / p>