Utilisateur:Stef48/compteur.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) ;
Firefox (sur GNU/Linux) / Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5./**
* Display a counter of how many times the page has been consulted.
* The counter is displayes in the upper right corner of the page.
* It requires Firefox 2.0 (uses client side persistent storage),
* and will NOT work under other non-gecko browsers (IE, OPERA, ...)
*/
// counter label (internationnalizable and personnalizable variable)
var pgCounterLabel = "<small>page consultée <b>$count</b> fois [<a href=\"javascript:resetPageCounter()\">RAZ</a>]</small>";
// prefix for counter storage (shorter is better)
var pgCounterPrefix = "counter."
/**
* Increments and displays page counter
*/
function pageCounter() {
if (typeof globalStorage == "undefined") {
alert("This page counter uses persistent storage features and requires Mozilla Firefox 2.0");
return;
}
// Counter only available in main namespace to spare storage
if (mw.config.get('wgNamespaceNumber') != 0)
return;
if (mw.config.get('wgAction') != "view")
return;
// Retrieve and increment counter
var count = getPageCounter() + 1;
setPageCounter(count);
displayPageCounter(count);
}
/**
* Return page counter global storage
*/
function getPageCounterStorage() {
return globalStorage[location.hostname];
}
/**
* Return page counter as an integer
*/
function getPageCounter() {
var count = getPageCounterStorage().getItem(pgCounterPrefix + mw.config.get('wgArticleId'));
if (! count) return 0;
return parseInt(count);
}
/**
* Page counter setter
*/
function setPageCounter(count) {
getPageCounterStorage().setItem(pgCounterPrefix + mw.config.get('wgArticleId'), count);
}
/**
* Reset counter for ALL pages
*/
function resetPageCounter() {
var storage = getPageCounterStorage();
for (var i = 0; i < storage.length; i++) {
var key = storage.key(i);
if (key.substr(0, pgCounterPrefix.length) == pgCounterPrefix) {
storage.removeItem(key);
}
}
// updating counter
var counter = document.getElementById("page-counter");
if (counter) {
counter.innerHTML = pgCounterLabel.replace(/\$count/, getPageCounter());
}
}
/**
* Display page counter
*/
function displayPageCounter(count) {
var counterDiv = document.createElement("div");
counterDiv.innerHTML = pgCounterLabel.replace(/\$count/, count);
counterDiv.style.textAlign="right";
counterDiv.id = "page-counter";
var contentNode = document.getElementById("content");
contentNode.insertBefore(counterDiv, contentNode.firstChild);
}
$(pageCounter);
if (mw.config.get('wgCanonicalSpecialPageName') && mw.config.get('wgCanonicalSpecialPageName')=='Watchlist' && mw.config.get('wgAction')=='view')
addOnloadHook(function () {
var frm = document.getElementsByTagName('form')[0]
frm.parentNode.insertBefore(document.createTextNode(' | '), frm)
var lnk = document.createElement('a')
lnk.appendChild(document.createTextNode('Unwatch…'))
lnk.href = 'javascript:importScript("Utilisateur:Stef48/wlunwatch.js")'
frm.parentNode.insertBefore(lnk, frm)
})