Kanban Your turn Notifier

Notify Kanban in your turn

目前為 2016-04-13 提交的版本,檢視 最新版本

// ==UserScript==
// @name Kanban Your turn Notifier
// @namespace tequila_j-script
// @version    0.0.4
// @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: 600000,
	step: 2000,
	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();
	console.log("turn notifier on");
}


//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 );
	console.log(arguments[0])
	if (arguments[0] == "moveWorker" || arguments[0] == "perfGoal")
			startNotification();
	return result;
};

startNotification();