在Chrome扩展程序的上下文菜单中弹出窗口 [英] popup window at chrome extensions' context menu
问题描述
我正在开发Chrome扩展并遇到问题。我已经添加了一个项目到chrome的上下文菜单,并且想要在菜单项被点击时打开一个弹出窗口。我的代码如下所示:
I'm developing a chrome extension and have a problem. I've added an item to chrome's context menu and want to open a popup window if the menu item is clicked. My code looks like this:
function popup(url) {
window.open(url, "window", "width=600,height=400,status=yes,scrollbars=yes,resizable=yes");
}
chrome.contextMenus.create({"title": "Tumblr", "contexts":["page","selection","link","editable","image","video","audio"], "onclick": popup('http://example.com')});
但是这段代码不能按我的要求工作。点击上下文项目后,弹出窗口不会出现,而是在Chrome扩展程序首选项中刷新扩展名后。
But this code doesn't work as I want. The popup window doesn't appear after an click on the context item, but rather after a refresh of the extension in the chrome extension preferences.
预先感谢!
推荐答案
chrome.contextMenus.create({... "onclick": popup('http://example.com')})
调用弹出窗口
立即生效,导致弹出窗口被打开。你必须传递一个函数的引用。为了让你的代码工作,将函数调用包装在一个函数中:
invokes the popup
function immediately, causing a pop-up to be opened. You have to pass a reference to a function. To get your code to work, wrap the function call in a function:
chrome.contextMenus.create({
"title": "Tumblr",
"contexts": ["page", "selection", "link", "editable", "image", "video", "audio"],
"onclick": function() {
popup('http://example.com');
}
});
window.open()
创建一个弹出窗口。另一种方法(只是为了让您知道它存在)是 chrome.windows.create
。
window.open()
can be used to create a popup. An alternative method (just to let you know that it exists) is chrome.windows.create
.
这篇关于在Chrome扩展程序的上下文菜单中弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!