我试着编写一个Google扩展,当我在短时间内单击左右键时,它只会打开一个新的选项卡。JavaScript没有问题,但我将其实现为"content_scripts“脚本。
在其他一些线程中,我读到无法从content_scripts访问chrome.* API( chrome.extension API除外)。
即使没有必要访问chrome.tabs API来打开一个新窗口(window.open应该做这个工作),但我似乎需要它来打开一个带有新选项卡页的新选项卡,显然通过window.open是不可能的。
所以我真的想不出什么是最好的方法。我可以使用一个背景页面,我可以从content_script调用,但我认为应该有一个更简单的方法,我只是不明白。
有人有主意吗?
发布于 2013-02-07 00:40:05
我认为您的内容脚本必须向后台页面发送消息才能调用chrome.tabs.create --内容脚本不能使用chrome,也不能直接与后台页面通信。
下面是一个关于在Chrome扩展中传递消息的参考文献,以了解更多细节,但下面是示例代码(根据上述参考中的示例修改)
// in background
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
switch ( request.action) {
case 'newTab' : {
//note: passing an empty object opens a new blank tab,
//but an object must be passed
chrome.tabs.create({/*options*/});
// run callback / send response
} break;
}
return true; //required if you want your callback to run, IIRC
});
// in content script:
chrome.extension.sendMessage({action: "newTab"}, function(response) {
//optional callback code here.
});发布于 2013-02-07 01:28:31
简单易
document.body.onclick = function openNewWindow( ) {
window.location.href = 'javascript:void window.open( "chrome://newtab" )';
}清单:
,"permissions":[
"http://*/*"
,"https://*/*"
]
,"manifest_version": 2
,"content_scripts":[{
"matches":[
"http://*/*"
,"https://*/*"
]
,"js":[
"js/openWindow.js"
]
}]好吧我想念这个问题..。已修改
https://stackoverflow.com/questions/14741484
复制相似问题