Utilisateur:Cj73/QuickEdit.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./*
* <nowiki>
* debug - v0.3 - 6/8/2009
* http://benalman.com/projects/javascript-debug-console-log/
*
* Copyright (c) 2009 "Cowboy" Ben Alman
* Licensed under the MIT license
* http://benalman.com/about/license/
*
* With lots of help from Paul Irish!
* http://paulirish.com/
*/
window.debug=(function(){var c=this,e=Array.prototype.slice,b=c.console,i={},f,g,j=9,d=["error","warn","info","debug","log"],m="assert clear count dir dirxml group groupEnd profile profileEnd time timeEnd trace".split(" "),k=m.length,a=[];while(--k>=0){(function(n){i[n]=function(){j!==0&&b&&b[n]&&b[n].apply(b,arguments)}})(m[k])}k=d.length;while(--k>=0){(function(n,o){i[o]=function(){var q=e.call(arguments),p=[o].concat(q);a.push(p);h(p);if(!b||!l(n)){return}b.firebug?b[o].apply(c,q):b[o]?b[o](q):b.log(q)}})(k,d[k])}function h(n){if(f&&(g||!b||!b.log)){f.apply(c,n)}}i.setLevel=function(n){j=typeof n==="number"?n:9};function l(n){return j>0?j>n:d.length+j<=n}i.setCallback=function(){var o=e.call(arguments),n=a.length,p=n;f=o.shift()||null;g=typeof o[0]==="boolean"?o.shift():false;p-=typeof o[0]==="number"?o.shift():n;while(p<n){h(a[p++])}};return i})();
// =============== REWORD ===============
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
function logg(message) {
debug.log(message);
//if (console && console.log) console.log(message);
}
addLoadEvent(enableQuickEditing);
//$(window).load( enableQuickEditing );
qeTextSel="";
function enableQuickEditing() {
//if (window.location.href.indexOf("index.php") != -1) return;
updateData(); //TODO: make lazy
//window.onmouseup = releaseSelection; //TODO - add listener
$("body").mouseup(releaseSelection);
$("body").mousedown(updateCursor);
$("body").mousemove(updateCursor);
}
function updateCursor(e) {
logg("updating cursor");
if (qeTextSel==getSelectedText() && e.ctrlKey) return;
if (! e.ctrlKey) {
$("#bodyContent").css("background","");
return; // ignore ctrl not held
}
logg("updating cursor - valid");
var xqeTextSel = getSelectedText();
var xqeHtmlSel = getSelectedHtml();
var text = (xqeHtmlSel==xqeTextSel?xqeTextSel:xqeHtmlSel);
if (xqeTextSel.length > 5) {
logg("updating cursor >5");
var first = qeText.indexOf(text);
var second = qeText.indexOf(text, first+1);
var bgColor="";
if (first!=-1 && second==-1) {
bgColor = "#efe"; // 'crosshair';
} else {
bgColor = second==-1?"#fee":"#ffe"; //(second==-1?'help':'move');
}
$("#bodyContent").css("background",bgColor);
}
//var newText=getSelectedText();
}
function releaseSelection(e){
logg("released");
if (qeTextSel==getSelectedText()) return; //ignore stray mouse clicks
if (! e.ctrlKey) return; // ignore ctrl not held
logg("released - valid");
// document.body.style.cursor = 'crosshair';
qeTextSel = getSelectedText();
qeHtmlSel = getSelectedHtml();
if (qeTextSel.length > 5) {
logg("released >5");
if (qeHtmlSel==qeTextSel) {
tryToEdit(qeTextSel);
} else {
tryToEdit(qeHtmlSel);
}
}
};
function getSelectedText(){
if(window.getSelection){
return window.getSelection().toString();
} else if(document.getSelection){
return document.getSelection();
} else if(document.selection){
return document.selection.createRange().text;
}
}
function getSelectedHtml(){ // returns translated HTML
if(window.getSelection){
var frag = window.getSelection().getRangeAt(0).cloneContents()
var $div = $("<div/>").append(frag);
$div.children("a").each( function() {
var $this = $(this);
var text = $this.text();
var ref = $this.attr("href"); // TODO try both?
ref = ref.split("_").join(" ");
ref = ref.split("/wiki/").join("");
ref = ref.split("/w/index.php?title=").join("");
ref = ref.split("action=edit").join("");
ref = ref.split("redlink=1").join("");
REF = decodeURI(ref);
//alert(text+"\n"+ref);
$this.removeAttr("title");
$this.removeAttr("class");
$this.removeAttr("href");
if (text!=REF) $this.attr("href",REF);
});
var trans = $div.get(0).innerHTML;
trans = trans.split("&").join("");
trans = trans.split("<a>").join("[[");
trans = trans.split("</a>").join("]]");
trans = trans.split("<ul>").join("");
trans = trans.split("</ul>").join("");
trans = trans.split("<li>").join("*");
trans = trans.split("</li>").join("");
trans = trans.split("<i>").join("''");
trans = trans.split("</i>").join("''");
trans = trans.split("<b>").join("'''");
trans = trans.split("</b>").join("'''");
trans = trans.split("<a href=\"").join("[[");
trans = trans.split("\">").join("|");
trans = trans.split(" ").join(" ");
trans = trans.split("%20").join(" ");
return trans;
} else if(document.getSelection){
return document.getSelection();
} else if(document.selection){
return document.selection.createRange().htmlText;
}
}
function updateData() {
$.getJSON("http://fr.wikipedia.org/w/api.php?action=query&prop=revisions&titles="+mw.config.get('wgPageName')+"&rvprop=timestamp|user|comment|content&format=json",
function(data){
$.each(data.query.pages, function(i,item){
qeText = item.revisions[0]["*"];
});
});
}
var rewordComment = (rewordComment? rewordComment:"---");
var qeSpecialSequences ={};
function rewordReplace(key, value) {
qeSpecialSequences[key] = value;
}
rewordReplace("\\n","\n");
function tryToEdit(text) {
var first = qeText.indexOf(text);
var second = qeText.indexOf(text, first+1);
logg("trying to edit");
// if (first!=-1 && second==-1) { //alert(newText);
var replacement = prompt("Modification de :\n <" + text + ">",text);
if (replacement==null) return;
for (var key in qeSpecialSequences) {
replacement = replacement.split(key).join(qeSpecialSequences[key]);
}
var edsum="";
if (replacement.indexOf("~~"+"~~")!= -1) {
edsum = "réponse ";
// TODO: perhaps display "[the first 10/20 characters]..." of the inserted comment.
} else {
var esIndex= replacement.indexOf(rewordComment);
if(esIndex!= -1) {
edsum += replacement.substring(esIndex+rewordComment.length);
replacement = replacement.substring(0,esIndex);
} else {
edsum += "'"+text+"' → '"+replacement+"' ";
}
}
edsum += "(avec [[Utilisateur:Dr Brains/QuickEdit.js|QuickEdit]])";
var newText = qeText.replace(text,replacement);
$.getJSON("http://fr.wikipedia.org/w/api.php?action=query&prop=info|revisions&intoken=edit&titles="+mw.config.get('wgPageName')+"&format=json", function(data){
$.each(data.query.pages, function(i,item){
qeToken = item.edittoken;
qeTimestamp = item.starttimestamp;
$.post("/w/api.php", { action: "edit", title: mw.config.get('wgPageName'), notminor:"", summary:edsum, text:newText, basetimestamp:qeTimestamp, token:qeToken, format:"json" },
function(data) {
alert(data);
window.location.reload(true); //updateData();
history.go(0);
window.location.href=window.location.href;
updateData();
});
});
});
// } else {
// var out = "";
// out+= second==-1?"(not found/couldn't parse)":"(not unique)";
//alert(out +"\n"+ qeTextSel+"\n"+qeHtmlSel+"\n"+text + "\n(text, html, wikitext)\n" + first + " " + second+"\n first found, second found.");
// }
}
// </nowiki>