Google chrome extension 循环创建多个上下文菜单
我有一段代码:Google chrome extension 循环创建多个上下文菜单,google-chrome-extension,contextmenu,Google Chrome Extension,Contextmenu,我有一段代码: for (game in settings_object.games) { chrome.contextMenus.create({ "title": "Add thread("+request.thread+") to game: "+game, "contexts":["page"], "onclick": functi
for (game in settings_object.games)
{
chrome.contextMenus.create({
"title": "Add thread("+request.thread+") to game: "+game,
"contexts":["page"],
"onclick": function () {addThreadToGame(game,request.thread)}
});
}
生成一个上下文菜单,如:
- 向游戏添加线程(1234):游戏一的ID
- 将线程(1234)添加到游戏:游戏二的ID
- 将线程(1234)添加到游戏:游戏三的ID
在修改了我在中找到的代码后,这个答案似乎有我需要的一半答案:
for (game in settings_object.games)
{
chrome.contextMenus.create({
"title": "Add thread("+request.thread+") to game: "+game,
"contexts":["page"],
"onclick": (function(element) {
return function(info, tab) {
addThreadToGame(element,request.thread)
}
})(game)
});
}