如何在 Google Chrome 扩展程序中启动新窗口 [英] How to launch a new window in Google Chrome Extension
问题描述
我正在尝试为 Google Chrome 开发一个扩展程序,但我遇到了一些问题,我想在用户点击图标时启动或创建一个新窗口.
像这样:.create()
听起来像您所需要的:
chrome.browserAction.onClicked.addListener(function(tab) {chrome.windows.create({/* 选项 */});});
您需要哪些选项?假设您想从扩展程序打开一个页面,您需要一个包含在 chrome.runtime.getURL
中的 URL:
chrome.browserAction.onClicked.addListener(function(tab) {chrome.windows.create({//如果您需要打开外部页面,只需使用完整的 URLurl: chrome.runtime.getURL("mypage.html")});});
然后,要显示一个没有顶部工具栏的窗口,您需要一个窗口类型 "popup"
:
chrome.browserAction.onClicked.addListener(function(tab) {chrome.windows.create({url: chrome.runtime.getURL("mypage.html"),类型:弹出"});});
最后,如果你想在窗口打开后做一些事情,使用回调:
chrome.browserAction.onClicked.addListener(function(tab) {chrome.windows.create({url: chrome.runtime.getURL("mypage.html"),类型:弹出"}, 函数(赢){//win 代表来自 windows API 的 Window 对象//打开后做一些事情});});
I'm trying to develop a Extension for Google Chrome, but I have some problems, I want to launch or create a new window when user click on it in the icon.
Like this: http://i.imgur.com/8iRkEOb.png
Thanks so much!
First off, if you have a default_popup
defined in the manifest - you need to remove it, as it interferes with the click event you want to catch.
Then, you need to catch the event in a background script:
chrome.browserAction.onClicked.addListener(function(tab) {
// ...
});
Next, if we want a window, we probably want to look at the windows
API. create()
sounds like what you need:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.windows.create({/* options */});
});
What options do you need? Assuming you want to open a page from your extension, you'll need an URL wrapped in a chrome.runtime.getURL
:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.windows.create({
// Just use the full URL if you need to open an external page
url: chrome.runtime.getURL("mypage.html")
});
});
Then, to show a window like you're showing, without top toolbar, you need a window type "popup"
:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.windows.create({
url: chrome.runtime.getURL("mypage.html"),
type: "popup"
});
});
Finally, if you want to do something after the window has opened, use the callback:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.windows.create({
url: chrome.runtime.getURL("mypage.html"),
type: "popup"
}, function(win) {
// win represents the Window object from windows API
// Do something after opening
});
});
这篇关于如何在 Google Chrome 扩展程序中启动新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!