Javascript:延迟显示上下文菜单(Chrome)

发布于 2024-09-17 23:39:11 字数 744 浏览 2 评论 0原文

我正在尝试在 Chrome 扩展程序中动态创建上下文菜单项;这涉及到向主扩展进程发送修改菜单的请求,并阻止菜单显示,直到发送响应为止。我一整天都在尝试不同的解决方案,但还没有想出一个有效的解决方案。例如,

node.oncontextmenu = initContext({...});

initContext = function(menuParams) {
  return function(event) {
    if (event.showContext) // Menu has been created and is ready for display
      return true;

    event.preventDefault(); // Block original menu event
    chrome.extension.sendRequest(menuParams, function() { // Send request to modify menu,
      event.showContext = true; // generate new event when menu is ready
      event.target.dispatchEvent(event);
    });
  };
};

使用 PreventDefault() 阻止菜单,并在菜单准备好显示时执行 chrome.extension.sendRequest() 的回调函数。使用此解决方案(以及我尝试过的所有其他解决方案),菜单永远不会显示。我将不胜感激任何帮助。

I'm trying to dynamically create context menu items in a Chrome extension; this involves sending a request to the main extension process to modify the menu, and blocking the menu from being displayed until a response is sent. I've been experimenting with different solutions all day, and I have yet to come up with one that works. For example,

node.oncontextmenu = initContext({...});

initContext = function(menuParams) {
  return function(event) {
    if (event.showContext) // Menu has been created and is ready for display
      return true;

    event.preventDefault(); // Block original menu event
    chrome.extension.sendRequest(menuParams, function() { // Send request to modify menu,
      event.showContext = true; // generate new event when menu is ready
      event.target.dispatchEvent(event);
    });
  };
};

The menu is blocked with preventDefault(), and the callback function to chrome.extension.sendRequest() is executed when the menu is ready to be displayed. With this solution (and all the others I've tried), the menu is never displayed. I'd appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文