7
votes

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!

1
Yap. I'm using version 2. But how can i make that otherwise? I'm not much schooled in javascript.Patrick Lenz
@chaohuang There's no inline event handler in the OPs code.Rob W

1 Answers

5
votes
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.