CryptureWorld Notifications

Notification script for cryptureworld adventures

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         CryptureWorld Notifications
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  Notification script for cryptureworld adventures
// @author       Xortrox
// @match        https://play.cryptureworld.com/*
// @match        https://play.cryptureworld.com/
// @icon         https://www.google.com/s2/favicons?domain=cryptureworld.com
// @grant        none
// @license MIT
// ==/UserScript==

(async function() {
    const icon = 'https://www.google.com/s2/favicons?domain=cryptureworld.com';

    const notifyTimerInterval = 60000;

    await hasPermission();

    setInterval(() => {
        const adventureSpan = document.querySelectorAll('adventure span')[0];

        if (adventureSpan) {
            const text = adventureSpan.innerText;

            if (text.toLowerCase().includes('ready to adventure')) {
                notify('Ready to Adventure');
            }
        }
    }, notifyTimerInterval);

    function notify(text) {
        console.log('Notifying.');
        hasPermission().then(function (result) {
            console.log('Notify result:', result);
            if (result === true) {
                let popup = new window.Notification('CryptureWorld', { body: text, icon: icon });
                popup.onclick = function () {
                    window.focus();
                }
            }
        });
    }

    function hasPermission() {
        return new Promise(function (resolve) {
            if ('Notification' in window) {
                if (window.Notification.permission === 'granted') {
                    resolve(true);
                } else {
                    window.Notification.requestPermission().then(function (permission) {
                        if (permission === 'granted') {
                            resolve(true);
                        } else {
                            resolve(false);
                        }
                    });
                }
            } else {
                resolve(true);
            }
        });
    }
})();