User:Bluefire272/common.js

Source: Wikipedia, the free encyclopedia.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
$(document).ready(function() {
    $("a[title^='Edit section']").each(function(i) {
        $(this).addClass("pair" + i);
        $(this).attr("href", null);
        $(this).attr("onclick", "sectionToTextarea($(this))");
        $(this).closest("h1, h2, h3, h4, h5, h6").addClass("pair" + i);
        $(this).closest("p").addClass("pair" + i);
    });
});
var viewableText;

function sectionToTextarea(editlink) {
    var c = editlink.attr("class");
    var sectionNumber = c.charAt(c.length - 1);
    var textarea = $('<textarea id="temp" />');
    textarea.val = "== Another test ==\nAnother cake\n\n=== Small test ===\nSmall cake\n\nHello everyone, I am yet another new addition to Wikipedia! I have a bit (I mean a bit) of wiki experience most of which comes from being quite active on Wikia, specifically the RuneScape wiki. [http:\/\/www.runescape.wikia.com\/User:Bluefire2 This] is my RSW account. If you would like to request a userbox, please go [[User:Bluefire272\/Userboxes by me|here]]. If you want one on Wikia, go [http:\/\/www.runescape.wikia.com\/User:Bluefire2\/Userboxes here].\n\n{{boxboxtop|Userboxes}}\n{{Template:User BinaryPeople}}\n{{Template:User Pi}}\n{{User:FastLizard4\/Userboxing\/Russian Reversal}}\n{{User:Nefariousopus\/Userboxes\/DivideBy0}}\n{{User:Strdst grl\/ubx\/bouncing}}\n{{User:Strdst grl\/ubx\/mandelbrot}}\n{{User:Strdst grl\/ubx\/pointless anims}}\n{{boxboxbottom}}";
    var a = editlink;
    var p = $("p." + c);
    var header = $("." + c).filter(":header");
    //a.remove();
    header.remove();
    p.replaceWith(textarea);
}
function textareaBlurred() {
    var html = $(this).val();
    //viewableText = $('<div id="caek">Hai</div>');
    viewableText.html(html);
    $(this).replaceWith(viewableText);
	$(".break").remove();
	$("#submit").remove();
	$("#editlink").css("display", "inline");
    $(viewableText).click(divToTextArea);
}

function divToTextArea(div) {
	var textarea = $('<textarea />');
	var submit = $('<button id="submit" onclick="textareaBlurred()">Submit</button>');
	var br = $('<br class="break"/>');
	var anotherBr = $('<br class="break"/>');;
	var divHTML = div.html();
	textarea.val(divHTML);
	matchProperties(div, textarea);
	div.replaceWith(textarea);
	br.insertAfter(textarea);
	submit.insertAfter(br);
	anotherBr.insertAfter(submit);
	$("#editlink").css("display", "none");
	textarea.focus();
	textarea.blur(textareaBlurred);
}

function matchProperties(div, textarea) {
	var height = div.css("height");
	height = height.substring(0, height.length - 2);
	height = parseInt(height);
	var newHeight = height + parseInt(div.css("borderWidth"));
	textarea.css("height", newHeight);
	textarea.css("width", div.css("width"));
	textarea.css("position", div.css("position"));
}

function getSectionText(page, section) {
	$.getJSON(
        'http://en.wikipedia.org/w/api.php?', {
            action: 'query',
			prop: 'revisions',
			rvlimit: '1',
			rvsection: section,
			rvprop: 'content',
			format: 'xml',
			titles: page
        },
		function(data) {
			return data;
		}
    )
}

function editSection(summary, content, section) {
    $.ajax({
        url: mw.util.wikiScript('api'),
        data: {
            format: 'json',
            action: 'edit',
            title: mw.config.get('wgPageName'),
            section: '1',
            summary: summary,
            text: content,
            token: mw.user.tokens.get('csrfToken')
        },
        dataType: 'json',
        type: 'POST',
        success: function(data) {
            if (data && data.edit && data.edit.result == 'Success') {
				window.location.reload();
            } else if (data && data.error) {
                alert('Error: API returned error code "' + data.error.code + '": ' + data.error.info);
            } else {
                alert('Error: Unknown result from API.');
            }
        },
        error: function(xhr) {
            alert('Error: Request failed.');
        }
    });
}