Utilisateur:Creasy/echo.js
Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.
Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;
Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5./*
* Ajoute un pseudo système "temps réel" à Echo (le système de notifications)
*
* Toutes les XX secondes, un appel XHR est réalisé pour connaitre le nombre de notifs
* non lues. Si le chiffre a changé:
* Le bouton de notifications est mis à jour
* Si l'onglet n'a pas le focus, le titre de la page change pour indiquer le changement
*
* bugs possibles:
* - Le symbole UTF-8 (📨) peut ne pas marcher sur certains navigateurs. Le remplacer par: (!)
* par exemple.
* - pas testé sur IE... donc peut ne pas marcher.
*/
// à configurer
var reload_delay = 15*1000;
var new_notification_symbol = "📨 ";
// Fonction principale qui va chercher le nombre de notifs et agit selon ce dernier.
function isNewNotifications(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", '/w/api.php?action=query&meta=notifications¬prop=count&format=json',true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState== 4 && xmlhttp.status == 200) {
var ret = JSON.parse(xmlhttp.responseText);
var nb_notifs = parseInt(ret["query"]["notifications"]["count"]);
var old_nb_notifs = parseInt(document.getElementsByClassName("mw-echo-notifications-badge")[0].innerHTML);
//var nb_notifs = old_nb_notifs+1; // DEBUGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
if ((old_nb_notifs != nb_notifs) && (nb_notifs > 0)) {
if (!document.hasFocus()) {
if (!document.title.split(new_notification_symbol)[1]) {
var t = document.title;
document.title = new_notification_symbol + t;
}
}
var dom_notif_area = document.getElementsByClassName("mw-echo-notifications-badge")[0];
dom_notif_area.innerHTML = nb_notifs;
dom_notif_area.className = "mw-echo-notifications-badge mw-echo-unread-notifications";
}
// Si toutes les notifs ont été lues sur d'autres onglets on fait un reset
if (nb_notifs == 0) {
if (document.title.split(new_notification_symbol)[1]) {
document.title = document.title.split(new_notification_symbol)[1];
}
var dom_notif_area = document.getElementsByClassName("mw-echo-notifications-badge")[0];
dom_notif_area.innerHTML = nb_notifs;
dom_notif_area.className = "mw-echo-notifications-badge";
}
}
}
xmlhttp.send(null);
setTimeout("isNewNotifications();", reload_delay);
return;
}
// Première vérification
setTimeout("isNewNotifications();", reload_delay);
// Remettre le titre par défaut.
window.addEventListener('focus', function() {
if (document.title.split(new_notification_symbol)[1]) {
document.title = document.title.split(new_notification_symbol)[1];
}
});