CryptureWorld Notifications

Notification script for cryptureworld adventures

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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);
            }
        });
    }
})();