User:Smith609/refToolbar.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:Smith609/refToolbar. |
// Based on the refToolbar script from the user preferences.
var numforms = 0;
var wikEdAutoUpdateUrl;
function refbuttons() {
if (mwCustomEditButtons && document.getElementById('toolbar') && wikEdAutoUpdateUrl == null) {
button = document.createElement('a');
button.href = "javascript:easyCiteMain()";
button.title = "Insert Citation";
buttonimage = document.createElement('img');
buttonimage.src = "/media/wikipedia/commons/e/ea/Button_easy_cite.png";
buttonimage.alt = "Insert Citation";
button.appendChild(buttonimage);
document.getElementById('toolbar').appendChild(button);
citeJournal()
}
}
function addOption(script, text) {
option = document.createElement('input');
option.setAttribute('type', 'button');
option.setAttribute('onclick', script);
option.setAttribute("value", text);
return option;
}
function hideInitial() {
document.getElementById('citeselect').style.display = 'none';
if (document.getElementById('citediv1') ) {
document.getElementById('citediv'+numforms).style.display = 'none';
}
}
function easyCiteMain() {
document.getElementById('citeselect').style.display = '';
}
function getTime() {
var time = new Date();
var nowdate = time.getUTCDate();
if (nowdate<10) { nowdate = "0"+ nowdate.toString(); }
var nowmonth = time.getUTCMonth()+1;
if (nowmonth<10) { nowmonth = "0"+ nowmonth.toString(); }
var nowyear = time.getUTCFullYear();
newtime = nowyear + '-' + nowmonth + '-' + nowdate;
return (newtime);
}
function citeJournal() {
if (numforms != 0) {
document.getElementById('citediv'+numforms).style.display = 'none';
}
template = "cite journal";
numforms++;
form = '<div id="citediv'+numforms+'">'+
'<fieldset><legend>Cite book source</legend>'+
'<table cellspacing="5">'+
'<input type="hidden" value="'+template+'" id="template">'+
'<tr><td width="120"><label for="pages"> doi: </label></td>'+
'<td width="400"><input type="text" style="width:100%" id="doi"></td>'+
'<td width="120"><label for="refname"> Reference name: </label></td>'+
'<td width="400"><input type="text" style="width:100%" id="refname"></td></tr>'+
'<tr><td width="120"><label for="author"> Author: </label></td>'+
'<td width="400"><input type="text" style="width:100%" id="author"></td>'+
'<td width="120"><label for="year"> Year: </label></td>'+
'<td width="400"><input type="text" style="width:100%" id="year"></td></tr>'+
'</table>'+
'<input type="button" value="Add citation" onClick="addcites()">'+
'</fieldset></div>';
document.getElementById('citeselect').innerHTML += form;
}
function addcites(template) {
cites = document.getElementById('citediv'+numforms).getElementsByTagName('input');
var citebegin = '<ref';
var citename = '';
var citeinner = '';
for (var i=0; i<cites.length-1; i++) {
if (cites[i].value != '' && cites[i].id != "refname" && cites[i].id != "template") {
citeinner += "|" + cites[i].id + "=" + cites[i].value;
}
else if (cites[i].value != '' && cites[i].id == "refname" && cites[i].id != "template") {
citebegin += ' name="' + cites[i].value + '"';
}
else if (cites[i].value != '' && cites[i].id != "refname" && cites[i].id == "template") {
citename = '>{{' + cites[i].value;
}
}
cite = citebegin + citename + citeinner + "}}</ref>";
insertTags(cite, '', '');
document.getElementById('citediv'+numforms).style.display = 'none';
}
function getNamedRefs() {
text = document.getElementById('wpTextbox1').value;
regex = /<\s*?ref\s+?name\s*?=\s*?(('([^']*?)')|("([^"]*?)"))\s*?>/gi //'
var namedrefs = new Array();
var i=0;
var nr=true;
do {
ref = regex.exec(text);
if(ref != null){
namedrefs[i] = ref[5];
i++;
} else {
nr=false;
}
} while (nr==true);
return namedrefs;
}
function citeNamedRef() {
namedrefs = getNamedRefs();
if (namedrefs == '') {
if (numforms != 0) {
document.getElementById('citediv'+numforms).style.display = 'none';
}
numforms++;
out = '<div id="citediv'+numforms+'"><fieldset>'+
'<legend>References in text</legend>There are no named refs (<tt><ref name="Name"></tt>) in the text</fieldset></div>';
document.getElementById('citeselect').innerHTML += out;
}
else {
if (numforms != 0) {
document.getElementById('citediv'+numforms).style.display = 'none';
}
numforms++;
form = '<div id="citediv'+numforms+'">'+
'<fieldset><legend>References in article</legend>'+
'<table cellspacing="5">'+
'<tr><td><label for="namedrefs"> Named references in text</label></td>'+
'<td><select name="namedrefs" id="namedrefs">';
for (var i=0;i<namedrefs.length;i++) {
form+= '<option value="'+namedrefs[i]+'">'+namedrefs[i]+'</option>';
}
form+= '</select>'+
'</td></tr></table>'+
'<input type="button" value="Add citation" onClick="addnamedcite()">'+
'</fieldset></div>';
document.getElementById('citeselect').innerHTML += form;
}
}
function addnamedcite() {
name = document.getElementById('citediv'+numforms).getElementsByTagName('select')[0].value;
ref = '<ref name="'+name+'" />';
insertTags(ref, '', '');
document.getElementById('citediv'+numforms).style.display = 'none';
}
hookEvent("load", refbuttons);