Användare:Notwist/twinklewarn.js
OBS: Efter du har publicerat sidan kan du behöva tömma din webbläsares cache för att se ändringarna.
- Firefox / Safari: Håll ned Skift och klicka på Uppdatera sidan eller tryck Ctrl-F5 eller Ctrl-R (⌘-R på Mac)
- Google Chrome: Tryck Ctrl-Skift-R (⌘-Skift-R på Mac)
- Edge Håll ned Ctrl och klicka på Uppdatera eller tryck Ctrl-F5.
- Opera: Tryck Ctrl-F5.
// Hämtad från http://en.wikipedia.org/wiki/User:AzaToth/twinklewarn.js den 22 november 2008
// Text kopierad/modifierad enligt GFDL. Information om originalförfattare finns på adressen ovan.
// If TwinkleConfig aint exist.
if( typeof( TwinkleConfig ) == 'undefined' ) {
TwinkleConfig = {};
}
/**
TwinkleConfig.defaultGroup (int)
if true, watch the page which has been dispatched an warning or notice, if false, default applies
*/
if( typeof( TwinkleConfig.defaultWarningGroup ) == 'undefined' ) {
TwinkleConfig.defaultWarningGroup = 1;
}
/**
TwinkleConfig.showSharedIPNotice may take arguments:
true: to show shared ip notice if an IP address
false: to not print the notice
*/
if( typeof( TwinkleConfig.showSharedIPNotice ) == 'undefined' ) {
TwinkleConfig.showSharedIPNotice = true;
}
/**
TwinkleConfig.watchWarnings (boolean)
if true, watch the page which has been dispatched an warning or notice, if false, default applies
*/
if( typeof( TwinkleConfig.watchWarnings ) == 'undefined' ) {
TwinkleConfig.watchWarnings = true;
}
/**
TwinkleConfig.blankTalkpageOnIndefBlock (boolean)
if true, blank the talk page when issuing an indef block notice (per [[WP:UW#Indefinitely blocked users]])
*/
if( typeof( TwinkleConfig.blankTalkpageOnIndefBlock ) == 'undefined' ) {
TwinkleConfig.blankTalkpageOnIndefBlock = false;
}
/**
TwinkleConfig.summaryAd (string)
If ad should be added or not to summary, default [[WP:TWINKLE|TWINKLE]]
*/
if( typeof( TwinkleConfig.summaryAd ) == 'undefined' ) {
TwinkleConfig.summaryAd = " ([[Wikipedia:Twinkle|T]])";
}
function twinklewarn() {
if( mw.config.get('wgNamespaceNumber') == 3 ) {
mw.util.addPortletLink( 'p-cactions', (twinkleConfigExists ? "javascript:twinklewarn.callback()" : 'javascript:alert("Ditt konto är för nyskapat för att använda Twinkle");'), "warn", "tw-warn", "Varna/uppmärksamma användare", "");
}
}
$(twinklewarn);
twinklewarn.callback = function twinklewarnCallback() {
var Window = new SimpleWindow( 600, 400 );
Window.setTitle( "Varna/uppmärksamma användare" );
var form = new QuickForm( twinklewarn.callback.evaluate );
var main_select = form.append( {
type:'field',
label:'Välj typ av varning/notifiering',
tooltip:'Välj först en huvudgrupp för varningar, sedan den specifika varningen.'
} );
var main_group = main_select.append( {
type:'select',
name:'main_group',
event:twinklewarn.callback.change_category
} );
main_group.append( { type:'option', label:'Hjälpmallar (1)', value:'level1', selected: ( TwinkleConfig.defaultWarningGroup == 1 || TwinkleConfig.defaultWarningGroup < 1 || ( userIsInGroup( 'sysop' ) ? TwinkleConfig.defaultWarningGroup > 8 : TwinkleConfig.defaultWarningGroup > 7 ) ) } );
main_group.append( { type:'option', label:'Varningsmallar (2)', value:'level2', selected: ( TwinkleConfig.defaultWarningGroup == 2 ) } );
if( userIsInGroup( 'sysop' ) ) {
main_group.append( { type:'option', label:'Blockering', value:'block', selected: ( TwinkleConfig.defaultWarningGroup == 3 ) } );
}
main_select.append( { type:'select', name:'sub_group', event:twinklewarn.callback.change_subcategory } ); //Will be empty to begin with.
form.append( {
type:'input',
name:'article',
label:'Linked article',
value:( QueryString.exists( 'vanarticle' ) ? QueryString.get( 'vanarticle' ) : '' ),
tooltip:'An article might be linked to the notice, either it was a revert to said article that dispatched this notice. Leave empty for no article to be linked'
} );
var more = form.append( { type:'field', label:'Fill in an optional reason and hit \"Submit\"' } );
more.append( { type:'textarea', label:'More:', name:'reason', tooltip:'Perhaps a reason, or that a more detailed notice must be appended' } );
more.append( { type:'submit', label:'Submit' } );
var result = form.render();
Window.setContent( result );
Window.display();
result.main_group.root = result;
// We must init the first choice (General Note);
var evt = document.createEvent( "Event" );
evt.initEvent( 'change', true, true );
result.main_group.dispatchEvent( evt );
}
// This is all the messages that might be dispatched by the code
twinklewarn.messages = {
level1: {
"logga in": {
label:"logga in",
summary:"Mall för oinloggade bidragsgivare"
},
"nybörjare": {
label:"nybörjare",
summary:"Mall för nya användare"
},
"test": {
label:"test",
summary:"Användare som testat att redigera"
},
"ofri": {
label:"ofri",
summary:"Användare som lagt in ofria bilder"
},
"användarsida": {
label:"användarsida",
summary:"Uppmaning till att skapa användarsida"
},
},
level2: {
"k1": {
label:"k1",
summary:"Första klottervarning"
},
"k2": {
label:"k2",
summary:"Andra klottervarning"
},
"k3": {
label:"k3",
summary:"Tredje klottervarning, blockering"
},
"k4": {
label:"k4",
summary:"Fjärde klottervarning, blockering"
},
"k5": {
label:"k5",
summary:"Femte klottervarning, blockering"
},
"k6": {
label:"k6",
summary:"Sjätte klottervarning, blockering"
},
"vandalvarning": {
label:"vandalvarning",
summary:"Varning för vandalkonto"
},
"vandalkonto": {
label:"vandalkonto",
summary:"Kontot blockerat, vandalkonto"
},
},
};
twinklewarn.callback.change_category = function twinklewarnCallbackChangeCategory(e) {
var value = e.target.value;
var sub_group = e.target.root.sub_group;
var messages = twinklewarn.messages[ value ];
sub_group.main_group = value;
var old_subvalue = sub_group.value;
if( old_subvalue ) {
old_subvalue = old_subvalue.replace(/\d*(im)?$/, '' );
var old_subvalue_re = new RegExp( RegExp.escape( old_subvalue ) + "(\\d*(?:im)?)$" );
}
while( sub_group.hasChildNodes() ){
sub_group.removeChild( sub_group.firstChild );
}
for( var i in messages ) {
var selected = false;
if( old_subvalue && old_subvalue_re.test( i ) ) {
selected = true;
}
var elem = new QuickForm.element( { type:'option', label:"[" + i + "]: " + messages[i].label, value:i, selected: selected } );
sub_group.appendChild( elem.render() );
}
if( value == 'block' ) {
var more = new QuickForm.element( {
type: 'input',
name: 'block_timer',
label: 'Period of blocking: ',
tooltip: 'The period the blocking is due for, for example 24 hours, 2 weeks, indefinite etc...'
} );
e.target.root.insertBefore( more.render(), e.target.root.lastChild );
e.target.root.article.disabled = true;
} else if( e.target.root.block_timer ) {
e.target.root.removeChild( e.target.root.block_timer.parentNode );
e.target.root.article.disabled = false;
e.target.root.reason.disabled = false;
}
}
twinklewarn.callback.change_subcategory = function twinklewarnCallbackChangeSubcategory(e) {
var value = e.target.value;
var main_group = e.target.main_group;
if( main_group == 'block' ) {
if( value == 'uw-block3' || value == 'uw-spamublock' || value == 'uw-soablock' || value == 'uw-vaublock' || value == 'uw-voablock' || value == 'uw-ublock' || value == 'uw-uhblock' || value == 'uw-lblock' ) {
e.target.form.block_timer.disabled = true;
} else if( e.target.form.block_timer.disabled ) {
e.target.form.block_timer.disabled = false;
}
if( value == 'uw-3block' || value == 'uw-ewblock' ) {
e.target.form.article.disabled = false;
} else if( !e.target.form.article.disabled ) {
e.target.form.article.disabled = true;
}
if( value == 'uw-block1' || value == 'uw-block2' || value == 'uw-block3' || value == 'uw-ablock' ) {
e.target.form.reason.disabled = false;
} else if( !e.target.form.reason.disabled ) {
e.target.form.reason.disabled = true;
}
}
}
twinklewarn.callbacks = {
main: function( self ) {
var form = self.responseXML.getElementById( 'editform' );
var text = form.wpTextbox1.value;
var history_re = /\<\!\-\-\ Template\:(uw\-.*?)\ \-\-\>.*?(\d{1,2}:\d{1,2}, \d{1,2} \w+ \d{4}) \(UTC\)/g;
var history = {};
var latest = { date:new Date( 0 ), type:'' };
var current;
while( ( current = history_re.exec( text ) ) != undefined ) {
var current_date = new Date( current[2] + ' UTC' );
if( !( current[1] in history ) || history[ current[1] ] < current_date ) {
history[ current[1] ] = current_date;
}
if( current_date > latest.date ) {
latest.date = current_date;
latest.type = current[1];
}
}
var date = new Date();
if( self.params.sub_group in history ) {
var temp_time = new Date( history[ self.params.sub_group ] );
temp_time.setUTCHours( temp_time.getUTCHours() + 24 );
if( temp_time > date ) {
Status.info( 'Info', "an identical " + self.params.sub_group + " has been issued in the last 24 hours" );
if( !confirm( "Would you still like to add a warning/notice?" ) ) {
self.statelem.info( 'aborted per user request' );
return;
}
}
}
latest.date.setUTCMinutes( latest.date.getUTCMinutes() + 1 ); // after long debate, one minute is max
if( latest.date > date ) {
Status.info('Info', "a " + latest.type + " has been issued in the last minute" );
if( !confirm( "Would you still like to add a warning/notice?" ) ) {
self.statelem.info( 'aborted per user request' );
return;
}
}
var mainheaderRe = /==+\\s*Warnings\\s*==+/;
var headerRe = new RegExp( "^==+\\s*(?:" + date.getUTCMonthName() + '|' + date.getUTCMonthNameAbbrev() + ")\\s+" + date.getUTCFullYear() + "\\s*==+", 'm' );
if( text.length > 0 ) {
text += "\n";
}
if( !headerRe.exec( text ) ) {
Status.info( 'Info', 'Skapar header för denna månaden' );
text += "== " + date.getUTCMonthName() + " " + date.getUTCFullYear() + " ==\n";
}
if( self.params.main_group == 'block' ) {
var article = '', time = null;
if( TwinkleConfig.blankTalkpageOnIndefBlock && ( /indef|\*|max/.exec( self.params.block_timer ) || self.params.sub_group == 'uw-block3' || self.params.sub_group == 'uw-spamublock' || self.params.sub_group == 'uw-soablock' || self.params.sub_group == 'uw-vaublock' || self.params.sub_group == 'uw-voablock' || self.params.sub_group == 'uw-ublock' || self.params.sub_group == 'uw-uhblock' || self.params.sub_group == 'uw-lblock' ) ) {
Status.info( 'Info', 'Blanking talk page per preferences and createing a new level 2 heading for the date' );
text = "== " + date.getUTCMonthName() + " " + date.getUTCFullYear() + " ==\n";
}
if( ( self.params.sub_group == 'uw-3block' || self.params.sub_group == 'uw-ewblock' ) && self.params.article ) {
article = '|1=' + self.params.article;
}
if( /te?mp|^\s*$|min/.exec( self.params.block_timer ) ) {
time = '';
} else if( /indef|\*|max/.exec( self.params.block_timer ) ) {
time = '|indef=yes';
} else {
time = '|time=' + self.params.block_timer;
}
text += "\{\{subst:" + self.params.sub_group + article + time + (self.params.reason ? '|reason=' + self.params.reason : '' ) + "|sig=true\}\}";
} else if( self.params.sub_group == 'uw-username' ) {
// ugly hack
text += "\{\{subst:" + self.params.sub_group + ( self.params.reason ? '|1=' + self.params.reason : '' ) + "\}\} \~\~\~\~";
} else {
text += "\{\{subst:" + self.params.sub_group + ( self.params.article ? '|1=' + self.params.article : '' ) + "\}\}" + (self.params.reason ? " ''" + self.params.reason + "'' ": ' ' ) + "\~\~\~\~";
}
var wgTitle = mw.config.get('wgTitle')
if ( TwinkleConfig.showSharedIPNotice && isIPAddress( wgTitle ) ) {
Status.info( 'Info', 'Lägger till notis för delat IP' );
switch( QueryString.get( 'type' ) ) {
case 'vand':
text += "\n:''Om detta är en delad [[IP-adress]], överväg att [[Special:Inloggning|skapa ett konto]] så kan du undvika framtida irrelevanta varningar eller meddelanden.'' ";
break;
default:
text += "\n:''Om detta är en delad [[IP-adress]], överväg att [[Special:Inloggning|skapa ett konto]] så kan du undvika framtida irrelevanta varningar eller meddelanden.'' ";
break;
}
}
var postData = {
'wpMinoredit': form.wpMinoredit.checked ? 1 : undefined,
'wpWatchthis': (form.wpWatchthis.checked || TwinkleConfig.watchWarnings) ? 1 : undefined,
'wpStarttime': form.wpStarttime.value,
'wpEdittime': form.wpEdittime.value,
'wpAutoSummary': form.wpAutoSummary.value,
'wpEditToken': form.wpEditToken.value,
'wpSection': '',
'wpSummary': twinklewarn.messages[self.params.main_group][self.params.sub_group].summary + ( self.params.article ? ' on [[' + self.params.article + ']]' : '' ) + '.' + TwinkleConfig.summaryAd,
'wpTextbox1': text
};
self.post( postData );
}
}
twinklewarn.callback.evaluate = function twinklewarnCallbackEvaluate(e) {
// First, grab all the values provided by the form
var params = {
reason: e.target.reason.value,
main_group: e.target.main_group.value,
sub_group: e.target.sub_group.value,
article: e.target.article.value.replace( /^(Image|Category):/i, ':$1:' ),
block_timer: e.target.block_timer ? e.target.block_timer.value : null
}
Status.init( e.target );
var wgPageName = mw.config.get('wgPageName');
var query = {
'title': wgPageName,
'action': 'submit'
};
Wikipedia.actionCompleted.redirect = wgPageName;
Wikipedia.actionCompleted.notice = "Mallen har lagts in på diskussionssidan. Laddar om sidan om ett par sekunder";
var wikipedia_wiki = new Wikipedia.wiki( 'User talk page modification', query, twinklewarn.callbacks.main );
wikipedia_wiki.params = params;
wikipedia_wiki.get();
}