User:DangerousPanda/notifier.js

// Original source borrowed from User:Ioeth/friendlywelcome.js // // If FriendlyConfig aint exist. if( typeof( FriendlyConfig ) == 'undefined' ) { FriendlyConfig = {}; }

/** FriendlyConfig.summaryAd ( string ) If ad should be added or not to summary, default Friendly */ if( typeof( FriendlyConfig.summaryAd ) == 'undefined' ) { FriendlyConfig.summaryAd = " using Friendly"; }

/** FriendlyConfig.topWelcomes ( boolean ) */ if( typeof( FriendlyConfig.topWelcomes ) == 'undefined' ) { FriendlyConfig.topWelcomes = false; }

/** FriendlyConfig.watchWelcomes ( boolean ) */ if( typeof( FriendlyConfig.watchWelcomes ) == 'undefined' ) { FriendlyConfig.watchWelcomes = false; }

/** FriendlyConfig.insertHeadings ( boolean ) */ if( typeof( FriendlyConfig.insertHeadings ) == 'undefined' ) { FriendlyConfig.insertHeadings = true; }

/** FriendlyConfig.welcomeHeading ( string ) */ if( typeof( FriendlyConfig.welcomeHeading ) == 'undefined' ) { FriendlyConfig.welcomeHeading = "== Discussion notice =="; }

/** FriendlyConfig.insertUsername ( boolean ) */ if( typeof( FriendlyConfig.insertUsername ) == 'undefined' ) { FriendlyConfig.insertUsername = true; }

/** FriendlyConfig.insertSignature ( boolean ) */ if( typeof( FriendlyConfig.insertSignature ) == 'undefined' ) { FriendlyConfig.insertSignature = true; }

/** FriendlyConfig.markWelcomesAsMinor ( boolean ) */ if( typeof( FriendlyConfig.markWelcomesAsMinor ) == 'undefined' ) { FriendlyConfig.markWelcomesAsMinor = true; }

/** FriendlyConfig.quickWelcomeMode ( String ) */ if( typeof( FriendlyConfig.quickWelcomeMode ) == 'undefined' ) { FriendlyConfig.quickWelcomeMode = "auto"; }

/** FriendlyConfig.quickWelcomeTemplate ( String ) */ if( typeof( FriendlyConfig.quickWelcomeTemplate ) == 'undefined' ) { FriendlyConfig.quickWelcomeTemplate = "Welcome"; }

/** FriendlyConfig.maskTemplateInSummary ( boolean ) */ if( typeof( FriendlyConfig.maskTemplateInSummary ) == 'undefined' ) { FriendlyConfig.maskTemplateInSummary = true; }

BWnotifier = { auto: function { if( QueryString.get( 'action' ) != 'edit' ) { // userpage not empty, aborting auto-welcome return; }		return BWnotifier.welcome; },	semiauto: function { BWnotifier.callback( wgTitle.split( '/' )[0].replace( /\"/, "\\\"") ); },	normal: function { if( QueryString.exists( 'diff' ) ) { var oXPath = '//div[@id="mw-diff-otitle2"]/span[@class="mw-usertoollinks"]/a[1][@class="new"]'; var nXPath = '//div[@id="mw-diff-ntitle2"]/span[@class="mw-usertoollinks"]/a[1][@class="new"]'; var oList = document.evaluate( oXPath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null ); var nList = document.evaluate( nXPath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null ); if( oList.snapshotLength > 0 || nList.snapshotLength > 0 ) { var spanTag = function( color, content ) { var span = document.createElement( 'span' ); span.style.color = color; span.appendChild( document.createTextNode( content ) ); return span; }

var welcomeNode = document.createElement('strong'); var welcomeLink = document.createElement('a'); welcomeLink.appendChild( spanTag( 'Black', '[' ) ); welcomeLink.appendChild( spanTag( 'Goldenrod', 'welcome' ) ); welcomeLink.appendChild( spanTag( 'Black', ']' ) ); welcomeNode.appendChild(welcomeLink);

if( oList.snapshotLength > 0 ) { var oTalkNode = oList.snapshotItem(0); var oHref = document.evaluate( '@href', oTalkNode, null, XPathResult.STRING_TYPE, null ).stringValue; var oWelcomeNode = welcomeNode.cloneNode( true ); oWelcomeNode.firstChild.setAttribute( 'href', oHref + '&' + QueryString.create( { 'BWnotifier': 'norm' } ) + '&' + QueryString.create( { 'vanarticle': wgPageName.replace(/_/g, ' ') } ) ); oTalkNode.parentNode.parentNode.appendChild( document.createTextNode( ' ' ) ); oTalkNode.parentNode.parentNode.appendChild( oWelcomeNode ); }				if( nList.snapshotLength > 0 ) { var nTalkNode = nList.snapshotItem(0); var nHref = document.evaluate( '@href', nTalkNode, null, XPathResult.STRING_TYPE, null ).stringValue; var nWelcomeNode = welcomeNode.cloneNode( true ); nWelcomeNode.firstChild.setAttribute( 'href', nHref + '&' + QueryString.create( { 'BWnotifier': 'norm' } ) + '&' + QueryString.create( { 'vanarticle': wgPageName.replace(/_/g, ' ') } ) ); nTalkNode.parentNode.parentNode.appendChild( document.createTextNode( ' ' ) ); nTalkNode.parentNode.parentNode.appendChild( nWelcomeNode ); }			}		}		if( wgNamespaceNumber == 3 ) { var username = wgTitle.split( '/' )[0].replace( /\"/, "\\\""); // only first part before any slashes

addPortletLink( 'p-cactions', "javascript:BWnotifier.callback(\"" + username + "\")", "Wel", "friendly-welcome", "Welcome user", ""); }	} }

BWnotifier.welcome = function welcomeUser { Status.init( document.getElementById('bodyContent') );

var params = { value: FriendlyConfig.quickWelcomeTemplate, article: QueryString.exists( 'vanarticle' ) ? QueryString.get( 'vanarticle' ) : '', mode: 'auto' };

var query = { 'title': wgPageName, 'action': 'submit' };	Wikipedia.actionCompleted.redirect = wgPageName; Wikipedia.actionCompleted.notice = "Welcoming complete, reloading talk page in some seconds"; var wikipedia_wiki = new Wikipedia.wiki( 'User talk page modification', query, BWnotifier.callbacks.main ); wikipedia_wiki.params = params; wikipedia_wiki.get; }

BWnotifier.callback = function BWnotifierCallback( uid ) { var Window = new SimpleWindow( 600, 400 ); Window.setTitle( "Choose a notifier" ); var form = new QuickForm( BWnotifier.callback.evaluate, 'change' );

form.append( {			type: 'input',			name: 'article',			label: 'Linked article (if supported by template)',			value:( QueryString.exists( 'vanarticle' ) ? QueryString.get( 'vanarticle' ) : '' ),			tooltip: 'An article might be linked to the welcome if the template supports it. Leave empty for no artice to be linked. Templates that support a linked article are marked with an asterisk.  Ignored for templates that do not support a linked article.',			event: function( event ) {				event.stopPropagation;			}		}	);

form.append( { type:'header', label:'noticeboards' } ); form.append( { type: 'radio', name: 'noticeboard', list: BWnotifier.standardList } );

var result = form.render; Window.setContent( result ); Window.display; }

BWnotifier.standardList = [ {		label: ': ANI*', value: 'ANI' }, { 		label: ': WQA', value: 'WQA', ]

// Set to true if template does not already have heading BWnotifier.headingHash = { 'ANI': false, 'WQA': false, }

// Set to true if template already has signature BWnotifier.signatureHash = { 'ANI': false, 'WQA': false, }

/* Set to true if template supports article * name from art template parameter */ BWnotifier.artHash = { 'ANI': true, 'WQA': true, }

/* Set to true if template supports article * name from vanarticle template parameter */ BWnotifier.vandalHash = { 'ANI': false, 'WQA': false, }

BWnotifier.callbacks = { main: function( self ) { var form = self.responseXML.getElementById( 'editform' ); // abort if mode is auto and form is not empty if( form.wpTextbox1.value != '\n' && self.params.mode == 'auto' ) { Status.info( 'Warning', 'User talk page not empty; aborting automatic welcome' ); Wikipedia.actionCompleted.event; return; }		var text = ''; Status.info( 'Info', 'Will add the notification to the '				+ ( FriendlyConfig.topWelcomes ? 'top' : 'bottom' )				+ ' of the user\'s talk page.' ); if( !FriendlyConfig.topWelcomes ) { text += form.wpTextbox1.value + '\n'; }		if( BWnotifier.headingHash[ self.params.value ] && FriendlyConfig.insertHeadings ) { Status.info( 'Info', 'Will create a new heading for the notification' ); text += FriendlyConfig.welcomeHeading + "\n"; }		Status.info( 'Info', 'Will substitute the notification' ); text += '\{\{subst:' + self.params.value; if( BWnotifier.artHash[ self.params.value ] ) { if( FriendlyConfig.insertUsername && self.params.value.substring(2,0) != 'W-' ) { Status.info( 'Info', 'Will add your username to the notification' ); text += '|' + wgUserName; }			if( self.params.article != '' ) { Status.info( 'Info', 'Will add article link to the notification' ); text += '|art=' + self.params.article; }		} else if( BWnotifier.vandalHash[ self.params.value ] ) { if( self.params.article != '' ) { Status.info( 'Info', 'Will add vandalized article link to the notification' ); }			text += '|' + self.params.article; if( FriendlyConfig.insertUsername ) { Status.info( 'Info', 'Will add your username to the template' ); text += '|' + wgUserName; }		} else if( FriendlyConfig.insertUsername ) { Status.info( 'Info', 'Will add your username to the notification' ); text += '|' + wgUserName; } 		text += '\}\}'; if( !BWnotifier.signatureHash[ self.params.value ] && FriendlyConfig.insertSignature ) { Status.info( 'Info', 'Will add your signature after the notification' ); text += ' \n\~\~\~\~'; }		if( FriendlyConfig.topWelcomes ) { text += '\n\n' + form.wpTextbox1.value; }		var postData = { 'wpMinoredit': FriendlyConfig.markWelcomesAsMinor ? 1 : undefined, 'wpWatchthis': form.wpWatchthis.checked ? 1 : (FriendlyConfig.watchWelcomes ? 1 : undefined), 'wpStarttime': form.wpStarttime.value, 'wpEdittime': form.wpEdittime.value, 'wpAutoSummary': form.wpAutoSummary.value, 'wpEditToken': form.wpEditToken.value, 'wpSummary': 'Added ' + ( FriendlyConfig.maskTemplateInSummary ? 'welcome' : ( '\{\{' + self.params.value + '\}\}' ) ) + ' template to user talk page' + FriendlyConfig.summaryAd, 'wpTextbox1': text };		self.post( postData ); } }

BWnotifier.callback.evaluate = function BWnotifierCallbackEvaluate(e) { // Ignore if a change to the text field triggered this event if( e.target.name == 'article' ) { return; }	var params = { value: e.target.value, article: e.target.form.article.value, mode: 'manual' };

Status.init( e.target.form ); var query = { 'title': wgPageName, 'action': 'submit' };	Wikipedia.actionCompleted.redirect = wgPageName; Wikipedia.actionCompleted.notice = "Welcoming complete, reloading talk page in some seconds"; var wikipedia_wiki = new Wikipedia.wiki( 'User talk page modification', query, BWnotifier.callbacks.main ); wikipedia_wiki.params = params; wikipedia_wiki.get; }

$( function {		if( QueryString.exists( 'BWnotifier' ) ) {			if( QueryString.get( 'BWnotifier' == 'auto' ) ) {				BWnotifier.auto;			} else {				BWnotifier.semiauto;			}		} else {			BWnotifier.normal;		}	} ); //