Search code examples
javascriptgoogle-chrometampermonkey

Uncaught ReferenceError: GM_notification is not defined


I am trying to have a Tampermonkey script send me desktop notification, but this API is not working:

(function() {
    'use strict'

    var timer = setInterval( () => {
        var categories = document.querySelectorAll('.am-category ')
        for (const category of categories) {
            var title = category.querySelector('.am-category-title')
            console.log(title.textContent.trim())
            if (title.textContent.includes('match')) {
                var notificationOptions = { title: 'Title', text: 'text', image: 'https://i.sstatic.net/geLPT.png' }
                GM_notification(notificationOptions)
            }
        }
    }, 5000)
})()

The output I am receiving is:

Uncaught ReferenceError: GM_notification is not defined
    at eval (userscript.html?name=my-userscript.user.js&id=618b836d-33a4-4b27-87bd-9dc8929c4e2a:24)

Do I have to do anything to enable this? The docs seem to imply it was readily usable: https://www.tampermonkey.net/documentation.php#GM_notification

Thanks for your help!


Solution

  • Ok, I figured it out myself.
    Apparently this function needs to be made available to the script by using the @grant header:

    // @grant        GM_notification
    

    to the ==UserScript== block comment.
    I coincidentally found this in the Greasemonkey documentation.