User:Newslinger/Notifier.js
Appearance
From Wikipedia, the free encyclopedia
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
This code will be executed when previewing this page.
This user script seems to have a documentation page at User:Newslinger/Notifier.
// <nowiki> // Notifier (beta) // Notifies other talk pages of a discussion on the current page // Please provide feedback at [[User talk:Newslinger/Notifier]] $(function(){ varapi,log,pageName,texts,windowManager vardialogReady=false vartalkNamespaceRegex=/(Talk|User talk|Wikipedia|Wikipedia talk|File talk|MediaWiki talk|Template talk|Help talk|Category talk|Portal talk|Draft talk|TimedText talk|Module talk):/ functioninit(){ mw.loader.using('mediawiki.util',function(){ pageName=mw.config.get('wgPageName').replace(/_/g,' ') if(pageName==='Wikipedia:Reliable sources/Noticeboard'&&$('body.skin-vector').length){ markHeadings() }elseif(pageName.search(talkNamespaceRegex)===0){ varmarkLink=mw.util.addPortletLink('p-cactions','#','Notifier','ca-notifier', 'Notify other talk pages of a discussion on this page') $(markLink).click(function(event){ event.preventDefault() markHeadings() }) } }) } functioninitDialog(){ OO.inheritClass(Dialog,OO.ui.ProcessDialog) Dialog.static.name='notifierDialog' Dialog.static.title='Notifier' Dialog.static.actions=[ { action:'notify', label:'Notify', flags:['primary','progressive'] },{ label:'Cancel', flags:'safe' } ] Dialog.prototype.initialize=function(){ Dialog.super.prototype.initialize.call(this) this.$notifyingLabel=$('<div>Notifying of discussion:</div>') this.$discussionName=$('<div style="font-weight: bold" />') this.$info=$('<p />') this.$info.append(this.$notifyingLabel,this.$discussionName) this.targetPages=newOO.ui.MultilineTextInputWidget({rows:5}) this.targetPagesField=newOO.ui.FieldLayout(this.targetPages,{ label:'Pages to notify', align:'top', help:'One page per line (maximum 90 pages). Only pages in talk and project namespaces are allowed. Subject to the rate limit on your account (90 edits/minute if autoconfirmed, and 8 edits/minute if not). Edits exceeding the limit will fail.', helpInline:true }) this.targetPages.on('change',(function(value){ varpageCount=value.trim().split('\n').length varcountLabel='' if(pageCount>90){ countLabel=' ('+pageCount+' pages; exceeds maximum of 90)' }elseif(value){ countLabel=' ('+pageCount+' page'+(pageCount>1?'s':'')+')' } this.targetPagesField.setLabel('Pages to notify'+countLabel) }).bind(this)) this.messageTitle=newOO.ui.TextInputWidget({}) this.messageTitleField=newOO.ui.FieldLayout(this.messageTitle,{ label:'Message title', align:'top' }) this.message=newOO.ui.MultilineTextInputWidget({rows:5}) this.messageField=newOO.ui.FieldLayout(this.message,{ label:'Message', align:'top', help:'Signature not needed. Your signature will be added if not present at the end of the message.', helpInline:true }) this.log=newOO.ui.MultilineTextInputWidget({ readOnly:true, rows:5, value:log }) this.logField=newOO.ui.FieldLayout(this.log,{ label:'Log', align:'top' }) this.notificationTemplate=newOO.ui.TextInputWidget({readOnly:true}) this.notificationTemplateField=newOO.ui.FieldLayout(this.notificationTemplate,{ label:'Notification template', align:'top', help:'After notifying, paste this template into the end of the original discussion.', helpInline:true }) this.panel=newOO.ui.PanelLayout({ padded:true, expanded:false }) this.panel.$element.append(this.$info,this.targetPagesField.$element,this.messageTitleField.$element, this.messageField.$element,this.logField.$element,this.notificationTemplateField.$element) this.$body.append(this.panel.$element) } Dialog.prototype.getSetupProcess=function(data){ data=data||{} returnDialog.super.prototype.getSetupProcess.call(this,data) .next(function(){ this.$discussionName.text(pageName+' § '+texts[data.id]) this.message.setValue('{{slink|'+pageName+'|'+texts[data.id]+'}}') },this) } Dialog.prototype.getActionProcess=function(action){ if(action==='notify'){ sendMessages(this) } returnDialog.super.prototype.getActionProcess.call(this,action) } Dialog.prototype.getBodyHeight=function(){ returnthis.panel.$element.outerHeight(true) } windowManager=newOO.ui.WindowManager() $(document.body).append(windowManager.$element) dialogReady=true } functionDialog(config){ Dialog.super.call(this,config) } functionmarkHeadings(){ texts={} var$headings=$('#mw-content-text h2:not(.toctitle h2)') $headings.each(function(index){ var$headline=$(this).children('.mw-headline') varid=$headline.attr('id') vartext=$headline.contents().not($headline.children('.mw-headline-number')).text().trim() createLink($(this),id,text) texts[id]=text }) } functioncreateLink($heading,id,text){ var$link=$('<a href="#">notify</a>') var$leftBracket=$('<span class="mw-editsection-bracket">[</span>') var$rightBracket=$('<span class="mw-editsection-bracket">]</span>') var$editSection=$('<span class="mw-editsection" />') $link.click(function(event){ event.preventDefault() promptNotify(id) }) $editSection.append($leftBracket,$link,$rightBracket) $heading.append($editSection) return$link } functionpromptNotify(id){ mw.loader.using(['oojs','oojs-ui'],function(){ if(!dialogReady){ initDialog() } log='Ready to notify' vardialog=newDialog() windowManager.addWindows([dialog]) windowManager.openWindow(dialog,{id:id}) }) } functionsendMessages(dialog){ api=api||newmw.Api() vartargetPages=dialog.targetPages.getValue().trim().split('\n').map(function(targetPage){ returntargetPage.trim() }) varmessageTitle=dialog.messageTitle.getValue().trim() varmessage=dialog.message.getValue().trim() if(message.slice(-4)!=='~~'+'~~'){ message+=' ~~'+'~~' } varnotificationTemplate='{{subst'+':notified|'+targetPages.join('|')+'}}' dialog.notificationTemplate.setValue(notificationTemplate) vartasks=[] $.each(targetPages,function(index,targetPage){ addLog(dialog,'Notifying: '+targetPage) if(targetPage.search(talkNamespaceRegex)!=0){ addLog(dialog,'Failed to notify: '+targetPage) addLog(dialog,'Error: Pages outside of talk and project namespaces are not allowed.') return } vartask=api.newSection(targetPage,messageTitle,message,{redirect:true}) .then(function(){ addLog(dialog,'Successfully notified: '+targetPage) },function(error){ addLog(dialog,'Failed to notify: '+targetPage) addLog(dialog,'Error: '+error) }) tasks.push(task) }) $.when.apply(null,tasks).then(function(){ addLog(dialog,'Finished!') }) } functionaddLog(dialog,message){ log+='\n'+message dialog.log.setValue(log) } init() }) // </nowiki>