选择类型使用Chrome扩展更改上下文菜单 [英] Selection type changes context menu using chrome extension
问题描述
我正在尝试构建一个Chrome扩展。其中我希望上下文菜单根据页面上选定的文本进行更改。
I am trying to build a chrome extension. In it I want the context menu to change according to the selected text on the page.
例如,如果它的数字我想要一个菜单来说明对此的可分性测试numbr是...
,如果它是一个字符串,它应该有别的东西,但不能同时存在。
For example, if its a number I want a menu that says divisibility test for this numbr is ... and if it is a string, it should have a something else, but not both at the same time.
我无法弄清楚如何做到这一点。
I cant figure out how to do that.
推荐答案
您必须为鼠标设置侦听器。
You will have to set a listener for mouse down. There is no other way to get the selected text before the menu is created.
看到这个问题:
在菜单创建前没有其他方法可以获取选定的文本。 > chrome扩展上下文菜单,如何仅在没有选择的情况下显示菜单项?
chrome extension context menus, how to display a menu item only when there is no selection?
以下是代码的一部分,其余部分位于链接处。 / b>
Here is part of the code the rest is at the link.
document.addEventListener("mousedown", function(event){
//right click
if(event.button == 2) {
if(window.getSelection().toString()) {
chrome.extension.sendRequest({cmd: "createSelectionMenu"});
} else {
chrome.extension.sendRequest({cmd: "createRegularMenu"});
}
}
}, true);
这篇关于选择类型使用Chrome扩展更改上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!