Browse Source

fix #1894: add `tag` to GM_notification (#1896)

tophf 2 years ago
parent
commit
23ae133630

+ 2 - 2
src/background/utils/notifications.js

@@ -7,8 +7,8 @@ const removeNotification = id => browser.notifications.clear(id);
 
 addPublicCommands({
   /** @return {Promise<string>} */
-  async Notification({ image, text, title, silent, onclick, zombieTimeout }, src) {
-    const notificationId = await browser.notifications.create({
+  async Notification({ image, text, tag, title, silent, onclick, zombieTimeout }, src) {
+    const notificationId = await browser.notifications.create(tag, {
       type: 'basic',
       title: [title, IS_FIREFOX && i18n('extName')]::trueJoin(' - '), // Chrome already shows the name
       message: text,

+ 1 - 1
src/injected/web/gm-api.js

@@ -135,6 +135,7 @@ export const GM_API = {
       });
       return onRequestCreate(opts, this, name);
     },
+    GM_notification: createNotification,
     /** @this {GMContext} */
     GM_xmlhttpRequest(opts) {
       return onRequestCreate(nullObjFrom(opts), this);
@@ -167,7 +168,6 @@ export const GM_API = {
       options.url = url;
       return onTabCreate(options);
     },
-    GM_notification: createNotification,
     GM_setClipboard(data, type) {
       bridge.post('SetClipboard', { data, type });
     },

+ 4 - 2
src/injected/web/notifications.js

@@ -1,6 +1,5 @@
 import bridge, { addHandlers } from './bridge';
 
-let lastId = 0;
 const notifications = createNullObj();
 addHandlers({
   NotificationClicked(id) {
@@ -15,11 +14,14 @@ addHandlers({
   },
 });
 
+/** @this {GMContext} */
 export function createNotification(text, title, image, onclick) {
   let options;
+  let tag;
   if (isObject(text)) {
     options = nullObjFrom(text);
     text = options.text;
+    tag = options.tag;
   } else {
     options = createNullObj();
     options.text = text;
@@ -28,7 +30,7 @@ export function createNotification(text, title, image, onclick) {
     options.onclick = onclick;
   }
   if (!text) throw new SafeError('Notification `text` is required!');
-  const id = ++lastId;
+  const id = `${this.id}:${options.tag = tag ?? safeGetUniqId()}`;
   const msg = createNullObj();
   for (const k in options) {
     msg[k] = isFunction(options[k]) ? true : options[k];