Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome notification not showing with Tampermonkey

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?

like image 876
user984648 Avatar asked Aug 09 '18 14:08

user984648


People also ask

Why am I not getting notifications on my desktop?

A few clicks can fix this problem whether you're using Windows or a Mac. On Windows, click the Start button and then Settings. In the navigation pane on the left, choose System, then Notifications. At the top of the page, make sure Notifications is on by swiping the button to the right.

What is a push notification on Chrome?

What is a Chrome push notification? Chrome Push notifications allow website visitors to opt-in to timely updates from websites they love and allow you to effectively re-engage them with personalized, engaging content.


1 Answers

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.stack.imgur.com/geLPT.png'
} );


With a useful onclick:

GM_notification ( {
    title: 'foo', text: '42', image: 'https://i.stack.imgur.com/geLPT.png',
    onclick: () => {
            console.log ("My notice was clicked.");
            window.focus ();
    }
} );
like image 138
Brock Adams Avatar answered Sep 20 '22 12:09

Brock Adams