如何在Chrome扩展中从上下文菜单中打开默认弹出窗口 [英] How to open the default popup from context menu in a chrome extension

查看:343
本文介绍了如何在Chrome扩展中从上下文菜单中打开默认弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个Chrome扩展程序,当我点击地址栏附近的图标时会打开一个弹出窗口。一切工作正常,但我想添加一些功能。所以我想我还要添加一个上下文菜单项,以便用户可以简单地搜索突出显示的单词。我希望弹出窗口在用户点击上下文菜单中的项目时显示(默认弹出窗口在右上角,而不是新的弹出窗口或新选项卡)。



我可以拥有这个功能吗?如果是,我该如何实现它?

解决方案

您无法以编程方式将弹出页面显示为用户点击它。

但是,您仍然可以根据背景脚本/内容菜单单击进行显示。您的后台脚本有四个主要选项:


  1. 打开popup.html页面的新选项卡

  2. JavaScript的编程注入在页面上构建类似弹出式对话框

  3. 使用正在运行的内容脚本传递内容脚本消息以执行上述操作。

  4. 使用通知API 为用户提供简单的格式化消息。

选项2,3,4将允许用户在没有任何导航的情况下留在他们的标签上。如果您只需要向用户快速发送通知,并且安全性障碍较少,则通知API路由最为简单。 2和3需要更多的簿记,但你可以使对话框看起来像你的弹出窗口。

I have developed a chrome extension that opens a popup when I click on the icon near the address bar. Everything works fine, however I want to add some functionality to it. So I thought I'd also add a context menu item so that the user can simply search for the highlighted word. I want the popup to showup when the user clicks on the item in the context menu(the default popup in the top right corner and not a new popup window or a new tab).

Can I have this functionality? If yes, how do I implement it?

解决方案

You can't make the popup page show programmatically as if the user clicked it.

However, you can still have something display based on the background script / content menu click. There are 4 main options for your background script:

  1. Open a new tab to the popup.html page
  2. Programmatic injection of javascript to construct a popup-like dialog on the page
  3. Content script message passing to do the same as above, using a running content-script.
  4. Use the notifications API for a simple minimally stylized message to the user.

Options 2, 3, 4 will allow the user to stay on their tab without any navigation. The notifications API route is the simplest to use if you just want some quick notification to the user, and there are fewer security snags. 2 and 3 require more book-keeping, but you can make the dialog look like your popup.

这篇关于如何在Chrome扩展中从上下文菜单中打开默认弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆