GM_notification

下面讲下 GM_notification 的用法

Tampermonkey官方文档摘录如下(2020-3-28)

GM_notification(details, ondone), GM_notification(text, title, image, onclick)

Shows a HTML5 Desktop notification and/or highlight the current tab.
details can have the following attributes:

  • text - the text of the notification (optional if highlight is set)
  • title - the notificaton title (optional)
  • image - the image (optional)
  • highlight - a boolean flag whether to highlight the tab that sends the notfication (optional)
  • timeout - the time after that the notification will be hidden (optional, 0 = disabled)
  • ondone - called when the notification is closed (no matter if this was triggered by a timeout > or a click) or the tab was highlighted (optional)
  • onclick - called in case the user clicks the notification (optional)
    All parameters do exactly the same like their corresponding details property pendant.
翻译

GM_notification(details, ondone), GM_notification(text, title, image, onclick)
显示一个HTML5形式的桌面通知,并且可以激活发出通知的标签页
details 可以有以下几个属性
text - string- 通知的文字
title -string- 通知的标题
image -string- 图片(可选)
highlight - boolean- 布尔值标识,是否激活显示发出这个通知的标签页(可选)
timeout - int- 通知多长时间后消失(可选,0 = 不消失)
ondone - function- 通知关闭(不管是超时还是点击触发)或者关联标签页激活之后执行(可选)
onclick -function- 用户点击通知后执行(可选)
所有参数做的事 和 属性字面意义 是一样的

补充说明
  • image
    是图片的地址,
    可以是本地文件地址(允许访问本地文件情况下),比如
    'file:///C:/Users/jon/Downloads/img1.jpg'
    可以是网络文件地址,比如
    'https://www.baidu.com/img/baidu_jgylogo3.gif'
    也可以是 base64 数据,具体数据不在这里粘贴了。

但是,叔叔要说但是了,
<img>标签src里面,如果是base64类型,能够直接使用 svg元素,
但是这里目前就不支持。
其他的比如什么gif,png,jpg的图片转为base64之后都能够完美支持。

  • highlight
    直接翻译就是“高亮”,实际意思是“激活”或者“重新获得焦点”,跟标签页“focus”事件差不多。
    这里说得再多也没有什么意义,你直接写个例子运行一下就知道怎么回事了。

  • 通知权限
    如果是网页要发出通知,是需要申请通知权限的。
    但是这里是使用扩展通知的API。目前是不需要申请权限的。
    怎么爽就怎么发。
    默认安装扩展的时候,默认已经给了Tampermonkey发通知的权限了。

例子

去吧,拿着例子一边玩去吧~~

function doit(message) {
  GM_notification({
    title: location.host,
    text: message,
    image: 'file:///C:/Users/jon/Downloads/黄6.jpg',
    timeout: 2000,
    // highlight: true,
    ondone: ()=>{alert('来自 ondone 事件')},
    onclick: ()=>{alert('来自 onclick 事件')}
  })
}

document.querySelector("#doit").addEventListener('click', ()=>{doit('woonigh 最帅!!!')});
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容

  • 原文出处: http://www.androidchina.net/6174.html Notification在...
    木木00阅读 12,425评论 3 32
  • 因为要结局swift3.0中引用snapKit的问题,看到一篇介绍Xcode8,swift3变化的文章,觉得很详细...
    uniapp阅读 4,630评论 0 12
  • 夫妻双方在离婚时,如果婚后生育有子女,那么就会涉及到争夺孩子的抚养权。在孩子的抚养权确定下来以后,随即而来的就是孩...
    11cba0c7c18e阅读 589评论 0 3
  • 有人说, 努力与拥有是人生一左一右的两道风景。但我以为,人生最美最不能逊色的风景应该是努力。努力是人生的一种精神状...
    阡陌凉栀阅读 224评论 0 0
  • 颓废的一整天到睡前还肚子鼓鼓 跟家人打电话视频 婆婆还是挂念着担心我感冒 真的想家真的爱家 明侦妻旅安排 补生日提...
    珍惜眼前始为真阅读 38评论 0 0