gpt4 book ai didi

javascript - Chrome 上下文菜单子(monad)项功能同时启动

转载 作者:行者123 更新时间:2023-11-29 14:48:19 24 4
gpt4 key购买 nike

我正在构建一个带有上下文菜单的 chrome 扩展。请参阅下面的代码。它应该按如下方式工作:“Connect-It”上下文项是父项,并在单击/悬停时在子菜单中显示两个子项(添加链接、添加文档)。单击特定子项时,新选项卡会打开一个新 URL。相反,如果单击任一子项,则两个新的 URl 都将打开。她是我的代号。我该如何解决这个问题?

// background.js

// Parent Level context menu item

chrome.runtime.onInstalled.addListener(function() {
var id = chrome.contextMenus.create({
id: "Connect-IT", // Required for event pages
title: "Connect-IT",
contexts: ["all"],
});

});

// child level contextmenu items

chrome.runtime.onInstalled.addListener(function() {
var id = chrome.contextMenus.create({
id: "Add a Link",
parentId: "Connect-IT",
title: "Add a Link",
contexts: ["all"],
});

});


chrome.runtime.onInstalled.addListener(function() {
var id = chrome.contextMenus.create({
id: "Add a Doc",
parentId: "Connect-IT",
title: "Add a Doc",
contexts: ["all"],
});

});

// click handler below. This is what's broken. If either Child Menu Item is clicked both of the function below execute
// launching the two web pages. If one menu item is clicked only the website with taht item shoudl launch.


chrome.contextMenus.onClicked.addListener(function addLink(info){ menuItemId="Add a Link",
chrome.tabs.create({url: "https://www.wufoo.com"});
})

chrome.contextMenus.onClicked.addListener(function addDoc(info){ menuItemId="Add a Doc",
chrome.tabs.create( {url: "https://www.google.com"});
})

最佳答案

将这两条语句替换即可

chrome.contextMenus.onClicked.addListener(function addLink(info){     menuItemId="Add a Link",
chrome.tabs.create({url: "https://www.wufoo.com"});
})

chrome.contextMenus.onClicked.addListener(function addDoc(info){ menuItemId="Add a Doc",
chrome.tabs.create( {url: "https://www.google.com"});
})

用这一条语句

chrome.contextMenus.onClicked.addListener(function(info, tabs){  
if ( info.menuItemId === 'Add a Link' )
chrome.tabs.create( {url: "https://www.google.com"});
else
chrome.tabs.create({url: "https://www.wufoo.com"});
});

关于javascript - Chrome 上下文菜单子(monad)项功能同时启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29755486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com