MediaWiki:ManageReferences/code.js

function editRef(ref) { refID = /cite_note-(\d+)/.exec(ref)[1]; $.showCustomModal('Edit Reference',' ',{       id: 'edit-reference-form',        callback: function {            $.getJSON('/api.php?action=query&prop=revisions&pageids=' + wgArticleId + '&indexpageids&rvprop=content|timestamp&format=json', function(data) { starttimestamp = (new Date).toISOString; rev = data.query.pages[data.query.pageids[0]].revisions[0]; pageContent = rev['*']; pattern = /(()(.*)<\/ref>|)/g; refs = pageContent.match(pattern); match = pattern.exec(refs[refID]); console.log(match); if(match.length >= 5 && typeof match[4] != 'undefined' && match[4].length) { refOrigContent = match[4]; $('#edit-reference-content').val(refOrigContent); }           });        },        buttons: [{            message: 'Cancel',            handler: function { $('#edit-reference-form').closeModal; },            defaultButton:  false,            id: 'edit-bio-cancel'        },{            message: 'Save',            handler: function {                refNewContent = $('#edit-reference-form #edit-reference-content').val;				saveRef(pageContent.replace(match[0],match[2] + refNewContent + ' '));            },            defaultButton: true,            id: 'edit-bio-save'        }]    }); console.log(ref); } function saveRef(content) { $.post('/api.php?action=edit',{		title: encodeURIComponent(wgPageName),		summary: 'edit ref ' + refID + '; Replace "' + refOrigContent + '" with "' + refNewContent + '"',		text: content,		basetimestamp: rev.timestamp,		starttimestamp: starttimestamp,		tags: 'apiedit',		token: mw.user.tokens.get('editToken'),		format: 'json'	},function(res) {		console.log('post successful',res.edit);		if(res.edit.result == 'Success' && !res.edit.hasOwnProperty('nochange')) {           $('#edit-reference-form .modalToolbar').prepend( $('',{class: 'wikia-button secondary', id: 'edit-reference-rev', href: wgServer + wgArticlePath.replace('$1',res.edit.title) + '?diff=' + res.edit.newrevid + '&oldid=' + res.edit.oldrevid, target: '_blank'}).text('Check changes') );			$.get('/api.php?action=parse&text=' + refNewContent + '&contentmodel=wikitext&format=json',function(res) { refParsedContent = $.parseHTML(res.parse.text['*']).find('p').html; $('#edit-reference-form #edit-bio-cancel').replaceWith(                   $('',{class: 'wikia-button secondary', id: 'edit-reference-ok'}).text('OK').click(function { $('#edit-reference-form').closeModal; $('ol.references li#cite_note-' + refID + ' .reference-text').html(refParsedContent); })               );            });        }    },'json'); } function deleteRef(ref) { refID = /cite_note-(\d+)/.exec(ref)[1]; $.getJSON('/api.php?action=query&prop=revisions&pageids=' + wgArticleId + '&indexpageids&rvprop=content|timestamp&format=json', function(data) {       starttimestamp = (new Date).toISOString;        rev = data.query.pages[data.query.pageids[0]].revisions[0];        pageContent = rev['*'];        pattern = /(()(.*)<\/ref>|)/g;        refs = pageContent.match(pattern);        match = pattern.exec(refs[refID]);        console.log(match);        if(match.length >= 5 && typeof match[4] != 'undefined' && match[4].length) {            refOrigContent = match[4];			saveRef(pageContent.replace(match[0],''));        }    }); } $('ol.references > li').each(function {	$(this).append( $(' ',{class: 'reference-buttons'}).append(			$(' ').html(' ').click(editRef.bind(this,$(this).attr('id'))),			$(' ').html('    ').click(deleteRef.bind(this,$(this).attr('id'))) )	); });