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?
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 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.
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:
(function
wraps are completely unnecessary, and just clutter/complication.window.jQuery.noConflict
-- when @grant none
is not in effect.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 ();
}
} );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With