மீடியாவிக்கி:Gadget-NopInserter.js
Jump to navigation
Jump to search
குறிப்பு - சேமித்த பின்னர், நீங்கள் செய்த மாற்றங்களைக் காண்பதற்கு உங்கள் உலவியின் இடைமாற்று அகற்றப்பட வேண்டும்.
- மொஸில்லா பயர்பாக்ஸ் / சபாரி: Shift+Reload, அல்லது Ctrl-F5 அல்லது Ctrl-R (⌘-R Mac ல்)
- கூகிள் குரோம் Ctrl-Shift-R அழுத்தவும். (⌘-Shift-R Mac ல்) ;
- இண்டர்நெட் எக்ஸ்ப்ளோரர்: Ctrl-Refresh அல்லது Ctrl-F5 ஐ அழுத்தவும்.
- ஒபேரா: Tools → Preferences இல் இடைமாற்றை அகற்றவும்;
//<source lang="javascript">
// Inserts a NOP on the previous Page: page.
// Complain to User:Inductiveload
// Release 1.0 - 2012-04-23 - initial release
// 1.1 - 2012-04-25 - add user option to auto advance
//
// To automatically advance to the edited page after adding a {{nop}},
// add the following to your user JS:
//
// Automatically advance to the previous page after checking for nops
// mw.user.options.set({'nopinserter_auto_advance':true});
// Adapted for Tamil wikisource by User:Jayprakash12345. Code copied from பயனர்:Jayprakash12345/Gadget-NopInserter.js
NopInserter = function() {
var myself=this;
var match = /(.*?)\/([0-9]+)/.exec(mw.config.get( 'wgPageName' ));
if (match === null ){ return }
var basename = match[1];
var pagenum = parseInt(match[2]);
if (pagenum < 1 ){ return }
this.prev_page = basename + '/' + (pagenum - 1);
var portletLink = mw.util.addPortletLink(
'p-tb', '#', 'Prev. page {{nop}}', 't-check-prev-page-nop', 'Check if the previous page ends in a newline-{{nop}}.'
);
$( portletLink ).click( function ( e ) {
e.preventDefault();
myself.get_raw_page(myself.prev_page, myself.check_nop);
});
};
NopInserter.prototype.get_page_parts = function(data) {
//split up the page into header, body and footer
var match = /<noinclude>([\s\S]*)<\/noinclude>([\s\S]*?)<noinclude>([\s\S]*)<\/noinclude>/m.exec(data);
return match;
};
NopInserter.prototype.assemble_page_parts = function(parts) {
//re-assemble a page from the header, body and footer
return '<noinclude>'+parts[1]+'</noinclude>'+parts[2]+'<noinclude>' + parts[3] +'</noinclude>';
};
NopInserter.prototype.check_nop = function(parts) {
var match = /\n{{[Nn]op}}\s*$/.exec(parts[2]);
if (match!==null) {
alert('A trailing {{nop}} found on the previous page.');
this.go_to_page();
} else {
if (confirm('No trailing {{nop}} was found on the previous page. Add one?')) {
this.add_trailing_nop(parts);
}
}
};
NopInserter.prototype.add_trailing_nop = function(parts) {
//remove trailing space and add the nop
parts[2] = parts[2].replace(/\s*$/i,'') + '\n{{nop}}';
var new_text = this.assemble_page_parts(parts);
this.save_page(this.prev_page, new_text, 'Adding trailing {{nop}} to break paragraph at the page boundary.');
};
NopInserter.prototype.get_raw_page = function(pagetitle, callback) {
var myself = this,
api = new mw.Api();
//get the current page text
api.get( {
action: 'parse',
page: pagetitle,
prop: 'wikitext'
} ).done( function ( data ) {
var parts = myself.get_page_parts(data.parse.wikitext['*']);
$.proxy(callback, myself)(parts);
} ).fail( function () {
alert('Previous page could not be loaded: [[' + pagetitle + ']]');
} );
};
NopInserter.prototype.save_page = function(title, content, summary) {
var myself = this;
$.ajax({
url: mw.util.wikiScript( 'api' ),
data: {
format: 'json',
action: 'edit',
title: title,
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' ) {
$('#ca-prev').css({'outline':'2px solid green'});
setTimeout(function() {
$('#ca-prev').css({'outline':''});
}, 2000);
myself.go_to_page();
} 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.' );
}
});
};
NopInserter.prototype.go_to_page = function() {
//go to the previous page if the user's options specify to do so
if ( mw.user.options.exists( 'nopinserter_auto_advance' ) && mw.user.options.get('nopinserter_auto_advance') ) {
window.location = mw.config.get('wgArticlePath').replace('$1', this.prev_page);
}
};
$(document).ready( function() {
//Page namespace only
if( mw.config.get( 'wgNamespaceNumber' ) == 250 ) {
var NopInserterInstance = new NopInserter(96);
}
});
//</source>