GM_notification

2020-03-28  本文已影响0人  金鱼叔叔

下面讲下 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- 用户点击通知后执行(可选)
所有参数做的事 和 属性字面意义 是一样的

补充说明

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

例子

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

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 最帅!!!')});
上一篇下一篇

猜你喜欢

热点阅读