Kanban Your turn Notifier

Notify Kanban in your turn

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name Kanban Your turn Notifier
// @namespace tequila_j-script
// @version    0.4.2
// @description  Notify Kanban in your turn
// @match      http://*.boiteajeux.net/jeux/kan/*
// @match      https://*.boiteajeux.net/jeux/kan/*
// @match      http://www.boiteajeux.net/jeux/kan/*
// @match      https://www.boiteajeux.net/jeux/kan/*
// @grant    GM_addStyle
// @run-at document-idle
// ==/UserScript==

// request permission on page load

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

(function() {

  'use strict';
  /*jshint multistr: true */


var honkSound = new Audio("http://www.soundjay.com/transportation/car-locked-honk-1.mp3");

window.setCheckTurn = function(callbackFunc, timing) {
        var variableInterval = {
            config: timing,
            callback: callbackFunc,
            stopped: false,
            runLoop: function() {
                if (variableInterval.stopped) return;
                    var result = variableInterval.callback.call(variableInterval);
                    if (typeof result == 'number') {
                        if (result === 0) return;
                            variableInterval.interval = result;
                    };
                    variableInterval.loop();
                        },
                        stop: function() {
	                        this.stopped = true;
                            console.log("Auto refresh stopped");
                            window.clearTimeout(this.timeout);
                        },
                        start: function() {
                        this.stopped = false;
                            console.log("Auto refresh started");
                            return this.loop();
                        },
                        loop: function() {
                        this.timeout = window.setTimeout(this.runLoop, this.getInterval());
                                return this;
                        },
                        incrementInterval: function() {
                        var alic = this.getInterval() + this.config.step;
                                if (alic > this.config.max) {
                        this.setInterval(this.config.max)
                                console.log("Interval already set to maximum:" + this.getInterval());
                        } else {
                        this.setInterval(this.getInterval() + this.config.step);
                                console.log("Interval increased to:" + this.getInterval());
                        }
                        return this;
                        },
                        resetInterval: function() {
                        this.setInterval(this.config.start)
                                console.log("Interval reset to:" + this.getInterval());
                        },
                        getInterval: function() {
                        if (sessionStorage.getItem("currentTime") === null) {
                        sessionStorage.setItem("currentTime", this.config.start);
                        }
                        return Number(sessionStorage.getItem("currentTime"));
                        },
                        setInterval: function(val) {
                        sessionStorage.setItem("currentTime", val);
                        }

                }
                
       return variableInterval;
               
    };


var notificationTimeout = {
	start: 10000,
	max: 60000,
	step: 3000,
	current: 10000
};


var gameName = $("#dvBandeauHaut > div:first > div.clTexteFort:nth-child(2)").html();

function notify(message) {
  if (!Notification) {
    console.log('Desktop notifications are not available for your browser.'); 
    return;
  }

  if (Notification.permission !== "granted")
    Notification.requestPermission();

  else {
    var notification = new Notification(gameName + ': your turn!', {
      icon: 'http://www.boiteajeux.net/jeux/kan/img/sandra_1.png',
      body: message,
	  requireInteraction: true
    });
	notification.onclick = function () {
    	window.focus();
  	};
  }
  
  honkSound.play();

}

var isMyTurn = function() {
	var message = $('#dvMessage').html();
	return ! message.startsWith("Still twiddling your thumbs");
}


var turnNotifier = setCheckTurn(function() {
	actualiserPage();
	}, notificationTimeout
)


function startNotification() {
	turnNotifier.resetInterval();
	turnNotifier.start();
}


//override function so notification can start again when they are clicked
var proxied_finalizeActions = finalizeActions
finalizeActions = function() {
	turnNotifier.stop();
	proxied_finalizeActions.apply(this, arguments);
	startNotification();
}


var proxied_refreshDisplay = refreshDisplay;
refreshDisplay = function() {
	proxied_refreshDisplay.apply( this, arguments );
    console.log("Display refreshed");
  	if (isMyTurn()) {
      	var message = $('#dvMessage').html();
		notify(message);
		turnNotifier.stop();
      	console.log("Auto refresh stop");
    } else {
		turnNotifier.incrementInterval();
    }
}

var proxied_passer = passer;
passer = function() {
	turnNotifier.stop();
	var result =  proxied_passer.apply( this, arguments );
	startNotification();
}

var proxied_actualiserPage = actualiserPage;
actualiserPage = function() {
	turnNotifier.stop();
	var result = proxied_actualiserPage.apply( this, arguments);
  	turnNotifier.start();
}

var proxied_faire = faire;
faire = function() {
	var result = proxied_faire.apply( this, arguments );
	//if (arguments[0] == "moveWorker" || arguments[0] == "perfGoal" || arguments[0] == "nextperfgoal" || arguments[0] == "selectDesignInit" || arguments[0] == "selectPosCertifInit") // selectPosCertifInit does not work. It is used by any cert
	if (arguments[0] == "moveWorker" || arguments[0] == "perfGoal" || arguments[0] == "nextperfgoal" || arguments[0] == "selectDesignInit" )
			startNotification();
	return result;
};

startNotification();
  
})() //end of script