Ikariam desktop notifications

Runs when you have ikariam open and provides push notifications when the advisors lights up.

目前為 2016-06-15 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Ikariam desktop notifications
// @namespace    Danielv123
// @version      1.4
// @description  Runs when you have ikariam open and provides push notifications when the advisors lights up.
// @author       Danielv123
// @match        *.ikariam.gameforge.com/*
// @grant        none
// ==/UserScript==

// request permission on page load
document.addEventListener('DOMContentLoaded', function () {
    if (Notification.permission !== "granted") {
        Notification.requestPermission();
    }
});

// main checking loop
setInterval(function() {
    if (Notification.permission !== "granted")
        Notification.requestPermission();
    else {
            // check if the advisors are glowing
            if ($('#js_GlobalMenu_cities')[0].className != "normal") {
                console.log('Ding!');
                notifyMe("Ikariam", "Something happened in one of your towns!", "http://s28-en.ikariam.gameforge.com/skin/layout/advisors/mayor_active.png");
            }
            if ($('#js_GlobalMenu_diplomacy')[0].className != "normal") {
                console.log('Ding!');
                notifyMe("Ikariam", "Someone sent you a message!", "http://s28-en.ikariam.gameforge.com/skin/layout/advisors/diplomat_active.png");
            }
            // by checking if military is !normal we can also include the red status
            if ($('#js_GlobalMenu_military')[0].className != "normal") {
                console.log('Ding!');
                notifyMe("Ikariam", "Your military advisor is trying to tell you something!", "http://s28-en.ikariam.gameforge.com/skin/layout/advisors/general_active.png");
            }
            if ($('#js_GlobalMenu_research')[0].className != "normal") {
                console.log('Ding!');
                notifyMe("Ikariam", "New research aviable!", "http://s28-en.ikariam.gameforge.com/skin/layout/advisors/scientist_active.png");
            }
    }
// wait 60.000 ms (1 min) between checking for notifications
},30000);


function notifyMe(title, message, picture) {
    // check if functionality exists
    if (!Notification) {
        alert('Desktop notifications not available in your browser. Try Chromium.');
        return;
    }
    
    if (!picture) {
        picture = "https://www.jcdanczak.com/images/samples.png";
    }
    
    // ask for permission to speak
    if (Notification.permission !== "granted")
        Notification.requestPermission();
    else {
        var notification = new Notification(title, {
            // notification icon, should be replaced with the correct advisor later
            icon: picture,
            body: message,
        });
        // kill notifications 700 ms after their birth
        setTimeout(function(){notification.close();}, 7000);
        // if user shows affection for notify, let notify do them a last service before it dies prematurely.
        notification.onclick = function () {
            window.open("http://s28-en.ikariam.gameforge.com/index.php");
        };
    }
}