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')});
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.
Thanks in advance!
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()
can be used to create a popup. An alternative method (just to let you know that it exists) is chrome.windows.create
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With