简体   繁体   English

如何在Chrome扩展程序中创建鼠标左键菜单

[英]How can I create a left-click menu in a Chrome extension

I want to make a left-click menu. 我想做一个左键菜单。 How can I make it on my Chrome extension? 如何在Chrome扩展程序中使用它?

This is my right-click menu: 这是我的右键菜单:

    chrome.runtime.onInstalled.addListener(function() {
    chrome.contextMenus.create({
        title: 'Dolar',
        id: 'dolar',
        contexts: ['all'],
    });
});

chrome.contextMenus.onClicked.addListener(function(info, tab) {
    if (info.menuItemId === "dolar") {
        alert("Hello");
    }
});

For making it left-click, how do I change it? 要使其左键单击,如何更改它?

There is no special Chrome extension API to create a menu that opens on left-click. 没有特殊的Chrome扩展程序API可以创建可在左键单击时打开的菜单。 To make a menu that opens on left-click, you will need to inject a content script in any page in which you desire to have it used. 要使菜单左键打开,您需要在想要使用它的任何页面中注入内容脚本 In that content script you will need to listen for the click event with the left button. 在该内容脚本中,您将需要使用左按钮监听click事件。 You will then need to insert into the DOM the elements that make up your menu. 然后,您需要将构成菜单的元素插入DOM。 You will need to do all of this manually, or find a library which will abstract the problem. 您将需要手动完成所有这些操作,或者找到一个可以抽象出问题的库。

In general, this will be inconsistent with the user interface which is used on most machines. 通常,这将与大多数计算机上使用的用户界面不一致。 This will be counter to most user's expectations and may cause user confusion. 这将违反大多数用户的期望,并可能导致用户困惑。 In addition, web pages will expect to be able to use left-click for whatever purpose they desire, if any, beyond the normal clicks on links (eg activating drop-down/pop-out menus, etc.). 另外,除了正常的链接点击(例如,激活下拉菜单/弹出菜单等)之外,网页还将希望能够将左击用于他们想要的任何目的(如果有)。 Your use of it will conflict with using left-click for any interaction within the page. 您对它的使用将与使用鼠标左键进行页面内的任何交互产生冲突。 Unless you have a specific use case, having a left-click menu that is generally available is probably a bad idea. 除非您有特定的用例,否则使用通常可用的左键菜单可能不是一个好主意。

Having a UI element which opens a menu when the user left-clicks on it is a different situation. 有一个UI元素可以在用户左键单击时打开菜单,这是另一种情况。 In such case, the user is clicking on a UI element which is expected to do something. 在这种情况下,用户正在单击预期会执行某项操作的UI元素。 However, you should consider carefully if loading a UI into every page is appropriate. 但是,您应该仔细考虑是否适合将UI加载到每个页面中。 Sometimes it is. 有时候是。 Doing so can place a heavy burden on pages where your add-on is completely unused. 这样做会给完全未使用加载项的页面带来沉重负担。 You can minimize this impact by loading just the minimal amount into the page to show the beginning of the UI. 您可以通过将最小量的内容加载到页面中以显示UI的开始,来最大程度地减少这种影响。 Then, loading the rest of your code/libraries/UI only when the user begins interacting with your extension's UI. 然后,仅在用户开始与扩展程序的UI交互时才加载其余的代码/库/ UI。 Needing a UI in the page is much more likely to be the case when your extension is for a limited set of pages (ie modifying the UI on a particular domain/page). 如果您的扩展程序只适用于一组有限的页面(例如,修改特定域/页面上的UI),则在页面中更需要UI。 However, for most extensions, general interaction with your add-on will begin with a browser/page action button click, a hotkey, a context menu entry, or your add-on detecting something and opening a UI for the user (ie not through adding a full UI to every page). 但是,对于大多数扩展,与您的附件的一般交互将始于单击浏览器/页面操作按钮,热键,上下文菜单项,或者您的附件检测到某些东西并为用户打开UI(即不通过向每个页面添加完整的用户界面)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为了选择或聚焦对象,如何使右键单击的行为与单击鼠标左键的行为相同 - How can I make a right-click behave as a left-click for the purpose of selecting or focusing an object 是否可以检测到右键单击浏览器上下文菜单上的左键单击? - Is it possible to detect a left-click on a right-click browser context menu? 如何在Chrome扩展程序中动态地向上下文菜单添加选项? - How can I add an option to the context menu dynamically in a Chrome extension? 将功能附加到左键单击 <listbox> 项目 - Attach function to Left-Click on <listbox> item Chrome 扩展强制左键单击 - Chrome extension force left click Chrome扩展程序-如何捕获vanillaJS文档上的点击事件 - Chrome extension - How can I capture click events on the document vanillaJS 如何以编程方式单击带有Chrome扩展程序的Google文档中的按钮? - How can I programmatically click a button in Google Docs with a Chrome Extension? 如何弹出上下文菜单并单击Chrome扩展程序中的一个菜单项? - How to popup a context menu and click on one menu item in Chrome extension? 如何创建一个读取xml或json的Chrome扩展程序 - How I can create a Chrome extension that reads an xml or json 如何在 Chrome 扩展程序中创建“onclick”事件? - How can I create "onclick" event in Chrome extension?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM