User:M~enwiki/dereference.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:M~enwiki/dereference. |
/*
importScript('User:M/jquery.js');
importScript('User:M/dereference.js');
*/
/*! selectText jQuery plugin by Steve Clay http://code.google.com/p/mrclay/source/browse/trunk */
/*
* Simple plugin to select a range of text within a text box/textarea
*/
(function(){function selectText(el,begin,end){var len=el.value.length;end=end||len;if(begin==null) el.select();else if(el.setSelectionRange) el.setSelectionRange(begin,end);else if(el.createTextRange){var tr=el.createTextRange(),c="character";tr.moveStart(c,begin);tr.moveEnd(c,end-len);tr.select();} else el.select();el.focus();} jQuery.fn.selectText=function(begin,end){this.length&&selectText(this.get(0),begin,end);return this;};})();
// Adds a link below the page heading which replaces references of the following form:
// <ref name=aa/><ref>678</ref>
// <ref name="bb">
// </ref>
// with this form:
// [1][2]
// [3]
// The refs are re-inserted upon submit (unless these numbers are deleted).
//
// TODO:
// * Dump the refs into a list, where clicking each opens a textarea with its removed ref data.
// * Timer to check for new number-refs, add them to list, allow creation. Check duplicates [12][12], highlight removed for re-insertion.
// * Handle slashes in: ref name="aa/bb".
// * Entry form for template refs instead of textarea, make ref list look nicer.
// * More efficient, if problems arise.
//
// "...that removes all refs into an a div below the edit textarea, replacing them with numbers[439].
// If you mess with the number, the ref-down-there turns red. Click the button to inject another ref
// below using reftools, and a number inline. Parsing/replacing the refs wouldn't be very difficult,
// and then add a timer that makes sure the numbers are all still there in the text. Offer little 'add
// this ref' for any new numbers (so right now I'd see '439: [click to add ref]' somewhere below)."
//
$(window).load( addRefTab );
function addRefTab() {
if (window.location.href.indexOf("action=") == -1) return;
$("<div id='refbox' style='display:none;margin-bottom:.4em; border:1px solid #bbb;background:#fafafa;border-top:none;padding:1px;margin-bottom:.3em;'></div></div>").insertAfter($("#wpTextbox1"));
$("<div id='refbutton' style='padding-top:.3em;text-align:center;cursor:pointer; border:1px solid #bbb;background:#fafafa;border-top:none;padding:1px;margin-bottom:.3em;'>References</div>").insertAfter($("#wpTextbox1"));
$("#refbutton").click(function () {
$("#refbox").slideDown( 500 );
$(this).fadeTo(100,.01).slideUp(100);
hideRefs();
}).mouseenter(function(){$(this).css("color","#900").css("background","#fafafa");
}).mouseleave(function(){$(this).css("color","black").css("background","#fafafa");
});
$("#wpTextbox1").width($("#refbutton").width());
}
var refMap = new Array();
function hideRefs (){
var t1 = new Date().getTime()
if (window.location.href.indexOf("action=") == -1) return;
var text = $("#wpTextbox1").val();
//var regex = /<ref\b[^(/>)>]*(\/>|>(?:(?! y4bo4gche5h)[\s\S])*?<\/ref>)/;
// also absorbs comments to the sides of the ref
var regex = /(<!--(?:(?!-->)[\s\S])*?-->[ ]*)*<ref\b[^(/>)>]*(\/>|>(?:(?! y4bo4gche5h)[\s\S])*?<\/ref>)([ ]*<!--(?:(?!-->)[\s\S])*?-->)*/
var curRefNum = 0;
var num=0;
$("#refbox").append("<div id='reflist' style='font-size:90%'><b>References:</b> </div>");
$("#refbox").append($("<div style='border:0px solid red;'><textarea style='margin-top:.5em;width:98.5%;margin-left:.5em;height:7em;' id='refcontent'></textarea></div>"));
var t2 = new Date().getTime()
for (;;) {
curRefNum++;
var match = text.match(regex);
if (match==null) break;
var startR = match.index;
var endR = match[0].length + startR;
var rText = text.substring(startR,endR);
while(text.indexOf("["+curRefNum+"]")>=0) {
curRefNum++;
}
text = text.substring(0,startR) + "["+curRefNum+"]" + text.substring(endR,text.length);
refMap[""+curRefNum] = rText;
$("#reflist").append(
$("<span style='cursor:pointer;' id='ref-"+curRefNum+"'/>")
.text(curRefNum+", ")
.mouseenter(function(){
$(this).css("background","red");
var refnum = $(this).attr("id").substr(4);
$("#refcontent").val(refMap[refnum]);
var loc = $("#wpTextbox1").val().indexOf("["+refnum+"]");
$("#wpTextbox1").selectText(loc,loc+2+refnum.length);
}).mouseleave(function(){
$(this).css("background","none");
$("#refcontent").val("");
})
);
num++;
}
var t3 = new Date().getTime()
alert(t3-t2);
$("#wpTextbox1").text(text);
$("#refclean").text("DeReferenced "+num+" references. They will be added back upon save/preview/changes, unless [2][56] etc. are removed.");
$("#editform").submit(function() {
$("#refclean").text("Rereferencing...");
var text2 = $("#wpTextbox1").val();
for (var key in refMap) {
//TODO: notify missing refs.
text2 = text2.split("["+key+"]").join(refMap[key]);
}
$("#wpTextbox1").text(text2);
$("#refclean").text("Rereferenced");
return true;
});
}
// Adds a link below the page heading which replaces references of the following form:
// <ref name=aa/><ref>678</ref>
// <ref name="bb">
// </ref>
// with this form:
// [1][2]
// [3]
// The refs are re-inserted upon submit (unless these numbers are deleted).
//
//<ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref>
//<ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref>
//<ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref><ref>678</ref>