Kanban Your turn Notifier

Notify Kanban in your turn

当前为 2016-05-11 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name Kanban Your turn Notifier
// @namespace tequila_j-script
// @version    0.1.0
// @description  Notify Kanban in your turn
// @match      http://*.boiteajeux.net/jeux/kan/*
// @match      https://*.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();
});

//window.setVariableInterval based from user gnarf (http://stackoverflow.com/users/91914/gnarf)
window.setVariableInterval = function(callbackFunc, timing) {
  var variableInterval = {
    interval: timing,
	startInterval: 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;
      window.clearTimeout(this.timeout);
    },
    start: function() {
      this.stopped = false;
      return this.loop();
    },
    loop: function() {
      this.timeout = window.setTimeout(this.runLoop, this.interval);
      return this;
    },
	incrementInterval: function(increment) {
	  this.interval = this.interval + increment;
	  return this;
	},
	resetInterval: function() {
	  this.interval = this.startInterval;
	}
  }

  return variableInterval;
};


var notificationTimeout = {
	start: 10000,
	max: 1800000,
	step: 5000,
	current: 10000
};

function resetTimeout() {
	notificationTimeout.current = notificationTimeout.start;
}

function increaseTimeout() {
	if (notificationTimeout.current >= notificationTimeout.max) {
		return;
	}
	notificationTimeout.current = notificationTimeout.current + notificationTimeout.step;	
}


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

}

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


var turnNotifier = setVariableInterval(function() {
	var message = $('#dvMessage').html();
	if (isMyTurn()) {
		notify(message);
		this.stop();
	} else {
		proxied_actualiserPage();
		if (this.interval <= notificationTimeout.max) 
			this.incrementInterval(notificationTimeout.step)
	}
	
	}, notificationTimeout.current

)
	



function startNotification() {
	turnNotifier.stop();
	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_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();
	startNotification();
}

var proxied_faire = faire;
faire = function() {
	var result = proxied_faire.apply( this, arguments );
	if (arguments[0] == "moveWorker" || arguments[0] == "perfGoal" || arguments[0] == "nextperfgoal")
			startNotification();
	return result;
};

startNotification();