User:The Evil IP address/debug.js

/** * Allows for one click modification of section headings when viewing a page * add importScript( 'User:The Evil IP address/hdedit.js' ); // Backlink:

/**
* Allows for one click modification of section headings when viewing a page
* add importScript( 'User:The Evil IP address/hdedit.js' ); // Backlink: [[User:The Evil IP address/hdedit.js]] to your .js file to use it
* @author Janko Hoener (The Evil IP address)
*/
console.log('debug script loaded');
/**
* Allows for one click modification of section headings when viewing a page
* add the following line to your .js file to use it:
  importScript( 'User:The Evil IP address/hdedit.js' ); // Backlink: [[User:The Evil IP address/hdedit.js]]
* @author Janko Hoener (The Evil IP address)
* @version 1.1
*/
mw.loader.load('mediawiki.api');
window.hdedit = {
	
    openeditform: function ($e) {
    	console.log($e);
        hdedit.anchor = $e.attr('id');
        hdedit.pagename = mw.config.get('wgPageName');
        hdedit.api = new mw.Api();
        hdedit.api.get( {
                action: 'parse',
                page: this.pagename,
                prop: 'sections',
                format: 'json'
        }).done( function (data) {
        	error = false;
        	error_msg = '';
            $.each(data.parse.sections, function (i, v) {
                    if (v.anchor == hdedit.anchor) {
                    	if (v.index !== '') {
                        	hdedit.index = v.index;
                    	}
                    	else {
                    		error = true;
                    		error_msg = 'This section cannot be edited automatically ' +
                    		'because it uses HTML formatting.';
                    	}
                        if (v.fromtitle && (v.fromtitle != hdedit.pagename)) {
                        	error = true;
                        	error_msg += 'This section cannot be edited automatically ' +
                        	'because it is transcluded from a template.';
                        }
                        return false;
                    }
            });
            if (error) {
            	hdedit.seterror(error_msg);
            	return;
            }
            if (hdedit.$e) {
            	hdedit.cancel();
        	}
        	hdedit.$e = $e;
            hdedit.api.get({
                    action: 'parse',
                    page: hdedit.pagename,
                    section: hdedit.index,
                    prop: 'wikitext',
                    format: 'json'
            }).done( function (obj) {
                hdedit.wikitext = obj.parse.wikitext['*'];
                hdedit.section_wikitext = hdedit.wikitext.replace(/^(=+)\s*(.+?)\s*\1[\s\S]*$/, '$2');
                hdedit.inputsize = hdedit.section_wikitext.length*1.5;
                var form = $('<form>').css('display', 'inline').submit(hdedit.save);
                var input = $('<input>').attr('id', 'hdedit_input').attr('size', hdedit.inputsize).val(hdedit.section_wikitext);
                var button1 = $('<button>').attr('id', 'hdedit_submit').attr('type', 'submit').text('Save');
                var button2 = $('<button>').attr('type', 'button').attr('id', 'hdedit_cancel').text('Cancel').click(hdedit.cancel);
                $(form).append(input).append(button1).append(button2);
                hdedit.form = form;
                $e.after(form);
                $e.hide();
            }
            );
        }
        );
    },
    
    save: function () {
        hdedit.newheading = $(this).parent().find('input').val();
        if (hdedit.newheading == hdedit.section_wikitext) {
            return false;
        }
        $('#hdedit_input, #hdedit_submit, #hdedit_cancel').attr('disabled', 'disabled');
        hdedit.api.edit(hdedit.pagename, function () {
        		return {
        			action: 'edit',
        			format: 'json',
        			section: hdedit.index,
        			minor: true,
        			summary: 'Section heading change: ' + hdedit.section_wikitext + ' → ' + hdedit.newheading 
        			+ ' using a [[User:The Evil IP address/hdedit|script]]',
        			text: hdedit.wikitext.replace(/^(=+)(\s*).+?(\s*)\1(\s*)$/m, '$1$2' + hdedit.newheading 
        				+ '$3$1$4'),
        		};
        }).then(
        function (resp) {
        	if (resp.result == 'Success') {
        		window.location.reload();
        	}
        	else if (resp.result == 'Failure') {
        		hdedit.seterror('API returned error code ' + data.error.code + ': ' + data.error.info);
        	}
        	else {
        		hdedit.seterror('Unknown API error.');
        	}
        });
        return false;
    },
    
    cancel: function () {
        hdedit.$e.show();
        hdedit.form.remove();
    },
    
    seterror: function (msg) { 
    	mw.notify(msg + '\nPlease edit the section heading manually.', {title: 'Error!', autoHide: false});
    },
    
    getpref: function (name) {
    	if (window.hdedit_prefs !== undefined && name in window.hdedit_prefs) {
    		return window.hdedit_prefs[name];
    	}
    	else {
    		return hdedit.defaultprefs[name];
    	}
    },
    
    defaultprefs: {
        'eventToActivate': 'click'
    },
    
};

if (mw.config.get('wgNamespaceNumber') >= 0              // disable on special pages
    && mw.config.get('wgAction') == 'view'               // activate on view only
    && mw.config.get('wgIsProbablyEditable')             // only if the page is probably editable
    && !mw.config.get('wgIsMainPage')                    // disable on Main Page
    && !('sysop' in mw.config.get('wgRestrictionEdit'))) { // disable on protected pages
    // they should be edited manually per https://en.wikipedia.org/w/index.php?oldid=982545683 

    var eventname = hdedit.getpref('eventToActivate');
    var selector = '';
    var oldmarkup = $('h1 span.mw-headline, h2 span.mw-headline, h3 span.mw-headline, h4 span.mw-headline, h5 span.mw-headline, h6 span.mw-headline');
    var newmarkup = $('#bodyContent h1, #bodyContent h2, #bodyContent h3, #bodyContent h4, #bodyContent h5, #bodyContent h6');
    if (oldmarkup.length) {
    	console.log('old markup found');
    	selector = oldmarkup;
    }
	else if (newmarkup.length) {
		console.log('new markup found');
		selector = newmarkup;
	}
    $(selector).bind(eventname, 
	    function () {
		    hdedit.openeditform($(this));
	    }
    );
}

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.