Chrome extensions context menu popup

I am developing a chrome extension and have a problem. I added an item to the chrome context menu and want to open a popup if I click on a menu item. My code is as follows:

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 does not work as I want. The popup does not appear after clicking on the context element, but rather after updating the extension in the chrome extension settings.

Thanks in advance!

+7
source share
1 answer
 chrome.contextMenus.create({... "onclick": popup('http://example.com')}) 

calls the popup function immediately, as a result of which a popup window opens. You must pass a link to the function. To make your code 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 window. An alternative method (just to let you know that it exists) is chrome.windows.create .

+5
source

All Articles