Chrome notification not showing with Tampermonkey
Asked Answered
I

2

7

I'm trying to have my Tampermonkey script show a Chrome notification, but no notification is showing up. I have allowed notifications on the site.

Here is my code:

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        somewebsite.com/*
// @grant        GM_notification
// @require      http://code.jquery.com/jquery-1.12.4.min.js
// ==/UserScript==

(function ($, undefined) {
    $(function () {
        GM_notification({title: 'foo', image: 'bar', text: '42', onclick: console.log});
    });
})(window.jQuery.noConflict(true));

What do I need to change?

Intramural answered 9/8, 2018 at 14:8 Comment(0)
N
8

Per the GM_notification documentation, GM_notification's image parameter requires an image.
'bar' is not an image, so the GM_notification call fails (silently).

Granted, it would be nice if there was an error message, but currently Tampermonkey does not provide one. (Feel free to file a bug report.)

Also:

  • Those (function wraps are completely unnecessary, and just clutter/complication.
  • Ditto the window.jQuery.noConflict -- when @grant none is not in effect.
  • No parameter is passed to the onclick callback so giving it console.log has no effect.


Here is a complete working script with the above errors corrected:

// ==UserScript==
// @name        _Notification test
// @match       *://YOUR_SERVER.COM/YOUR_PATH/*
// @match       https://stackoverflow.com/questions/51769201/*
// @grant       GM_notification
// @require     http://code.jquery.com/jquery-1.12.4.min.js
// ==/UserScript==

GM_notification ( {title: 'foo', text: '42'} );


With a valid image:

GM_notification ( {
    title: 'foo', text: '42', image: 'https://i.sstatic.net/geLPT.png'
} );


With a useful onclick:

GM_notification ( {
    title: 'foo', text: '42', image: 'https://i.sstatic.net/geLPT.png',
    onclick: () => {
            console.log ("My notice was clicked.");
            window.focus ();
    }
} );
Nganngc answered 9/8, 2018 at 19:31 Comment(2)
Another example for 1sec timeout: GM_notification ( {title: 'foo', text: '42', timeout: 1000} );Homochromous
window.focus() doesn't change the tab for me for some reason on macos chromeDhammapada
K
2

These tips help, but for me the additional step required was to enable Chrome notification permissions in the Mac

Kuhns answered 18/10, 2022 at 4:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.