popup window at chrome extensions' context menu

↘锁芯ラ 提交于 2019-12-01 01:30:07

问题


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:


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.



来源:https://stackoverflow.com/questions/11742449/popup-window-at-chrome-extensions-context-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!