/**
* Tag files with {{Watermarked}}
* Based on [[User:Steinsplitter/tagPersonalityRight.js]]
* <nowiki>
*/
/*global mw:false, $:false, alert:false*/
$( document ).ready( function() {
'use strict';
var template2Search = 'Watermarked';
if ( mw.config.get( 'wgAction' ) === 'view' &&
mw.config.get( 'wgNamespaceNumber' ) === 6 &&
!$('#commons-template-watermarked').length ) {
$( mw.util.addPortletLink( 'p-tb', '#', '+watermarked' ) )
.click( function( event ) {
event.preventDefault();
mw.loader.using( [ 'mediawiki.util', 'mediawiki.api', 'user.options', 'ext.gadget.libWikiDOM' ], function() {
var i, section, data, licensingIDs;
// The h2 with the license has a different id for every language...
licensingIDs = [
// en
'Licensing',
'Licensing\\:',
// de
'Lizenz',
'Lizenz\\:'
];
for( i = 0; i < licensingIDs.length; i++ ) {
if ( $( '#' + licensingIDs[i] ).length ) {
section = '#' + licensingIDs[i];
break;
}
}
if ( section ) {
section = mw.util.getParamValue(
'section',
$( section )
.parent()
.find( 'span.mw-editsection a' )
.attr( 'href' )
);
}
data = {
title: mw.config.get( 'wgPageName' ),
action: 'raw'
};
if ( section ) {
data.section = section;
}
$.ajax( {
url: mw.config.get( 'wgServer' ) + mw.config.get( 'wgScript' ),
data: data,
cache: false
} )
.done( function( text ) {
var api = new mw.Api(),
dom = mw.libs.wikiDOM.parser.text2Obj( text ),
template2Add = '\n{{' + template2Search + '}}',
afterTemplates = [
'Information'
],
edit, inserted, insertText, insertLast;
if ( dom.nodesByType.template ) {
insertText = function() {
text += template2Add;
};
insertLast = function() {
var template = dom.nodesByType.template[dom.nodesByType.template.length - 1];
// Do not append something after localized messages
if ( 'h' === template.parent.type ) {
return insertText();
}
dom.nodesByType.template[dom.nodesByType.template.length - 1].after( template2Add );
text = mw.libs.wikiDOM.parser.obj2Text( dom );
};
if ( !section ) {
$.each( dom.nodesByType.template, function( i, tl ) {
var template = tl.parts[0][0];
if ( !template ) return;
template = mw.libs.wikiDOM.normalizeTitle( template.replace(/^[Tt]emplate:/, '') );
if ( $.inArray( template, afterTemplates ) === -1 ) return;
tl.after( template2Add );
text = mw.libs.wikiDOM.parser.obj2Text( dom );
inserted = true;
return false;
});
}
if ( !inserted ) {
insertLast();
}
} else {
insertText();
}
edit = {
action: 'edit',
title: mw.config.get( 'wgPageName' ),
text: text,
summary: '/* {{int:license-header}} */ +watermarked using [[User:CptViraj/tagWatermarked.js|tagWatermarked.js]]',
token: mw.user.tokens.get( 'csrfToken' ),
nocreate: true
};
if ( section ) {
edit.section = section;
}
api.post( edit )
.done( function() {
window.location.href =
mw.config.get( 'wgServer' ) + mw.config.get( 'wgArticlePath' ).replace( /\$1/g, mw.config.get( 'wgPageName' ) );
} )
.fail( function() {
alert( "An error occured while editing the page" );
} );
} )
.fail( function() {
alert( "An error occured while editing the page" );
} );
} );
} );
}
} );
// </nowiki>
// [[Category:User scripts]]