在Chrome扩展中显示通知 [英] show notifications in chrome extension

查看:449
本文介绍了在Chrome扩展中显示通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写我的第一个Chrome扩展。在我的Chrome扩展中,我在右键单击上下文菜单中添加了一个选项。

  chrome.contextMenus.create({
id:MyExntesion,
title:My Extension,
type:normal,
contexts:[image],
onclick:this.handleClick
});

在我的handleClick方法中,我想显示通知。此通知应弹出在浏览器窗口的右上角,它只是确认用户已经点击了上下文菜单。我怎么做?



我做了一些研究,但没有发现任何有用的东西。 。在Chrome中,看起来类似于 chrome.notifications ,但对格式化的控制较少并且未由Chrome通知中心管理。用法概述此处



3)如果你真的想要控制它的显示方式,最具侵入性和最困难的方法是将你的用户界面注入到包含内容脚本的所有页面中。


$ b $如果你这样做的话,你会遇到几个问题:



  • 一般来说,自己设计UI很困难;

  • UI将被限制在网页视口中;
  • CSS可能会从页面流血并破坏您的UI(尽管Shadow DOM可能是答案);
  • li>
  • 为了显示您的用户界面,您需要所有网页上的数据的一揽子权限。它往往会吓跑用户。



如果你决定走这条路线,这里有个问题可能有帮助: Chrome扩展内容脚本自定义UI


I'm writing my first chrome extension. In my chrome extension I have added an option in Right Click Context menu.

        chrome.contextMenus.create({
            "id" : "MyExntesion",
            "title" : "My Extension",
            "type" : "normal",
            "contexts" : ["image"],
            "onclick" : this.handleClick
        }); 

In my handleClick method, I want to show a notification. This notification should pop out in top right corner of the browser window which just confirms that user has clicked on the context menu. How do I do that?

I did some research but didn't find anything useful. https://developer.chrome.com/extensions/notifications this talkes about system tray notification, where as this https://developer.chrome.com/extensions/browserAction#method-setPopup lets you create new popups but they are only shown when extension icon is clicked.

解决方案

There are 3 principal ways of showing a notification in Chrome.

1) Aforementioned chrome.notifications API. It will show a toast (not merely an indication in systray), but you as a developer have little say on how it appears. Usage overview here.

2) Standard HTML Notification API. In Chrome, looks similar to chrome.notifications, except for less control over formatting and not managed by Chrome's notification center. Usage overview here.

3) If you really want control over how it's shown, the most invasive and hard way is to inject your UI into all pages with a content script.

You face several problems if you do that:

  • In general, it's harder to design the UI yourself;
  • The UI will be confined to the webpage viewport;
  • CSS may bleed from the page and mangle your UI (though Shadow DOM can be an answer);
  • You need blanket permissions for "data on all webpages" just to show your UI. It tends to scare users away.

If you decide to go that route nonetheless, here's a question that might help: Chrome extension content scripts custom ui

这篇关于在Chrome扩展中显示通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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