مدیاویکی:Gadget-HotCat.js: تفاوت میان نسخه‌ها

بدون خلاصۀ ویرایش
بدون خلاصۀ ویرایش
بدون خلاصۀ ویرایش
 
خط ۲۷: خط ۲۷:
// Don't use mw.config.get() as that takes a copy of the config, and so doesn't
// Don't use mw.config.get() as that takes a copy of the config, and so doesn't
// account for values changing, e.g. wgCurRevisionId after a VE edit
// account for values changing, e.g. wgCurRevisionId after a VE edit
var conf = mw.config.values;
var
        conf = mw.config.values,
        // when running on mobile domain - do not use wgServer.
        wgServer = window.location.host.indexOf('.m.') > -1 ?
            '//' + window.location.host : mw.config.get( 'wgServer' );


// Guard against double inclusions (in old IE/Opera element ids become window properties)
// Guard against double inclusions (in old IE/Opera element ids become window properties)
خط ۲۶۱: خط ۲۶۵:


function loadJS( page, callback ) {
function loadJS( page, callback ) {
load( conf.wgServer + conf.wgScript + '?title=' + encodeURIComponent( page ) + '&action=raw&ctype=text/javascript', callback );
load( wgServer + conf.wgScript + '?title=' + encodeURIComponent( page ) + '&action=raw&ctype=text/javascript', callback );
}
}


function loadURI( href, callback ) {
function loadURI( href, callback ) {
var url = href;
var url = href;
if ( url.substring( 0, 2 ) === '//' ) url = window.location.protocol + url; else if ( url.substring( 0, 1 ) === '/' ) url = conf.wgServer + url;
if ( url.substring( 0, 2 ) === '//' ) url = window.location.protocol + url; else if ( url.substring( 0, 1 ) === '/' ) url = wgServer + url;


load( url, callback );
load( url, callback );
خط ۲۸۴: خط ۲۸۸:


// Localization hook to localize HotCat messages, tooltips, and engine names for wgUserLanguage.
// Localization hook to localize HotCat messages, tooltips, and engine names for wgUserLanguage.
if ( window.hotcat_translations_from_commons && conf.wgServer.indexOf( '//commons' ) < 0 ) {
if ( window.hotcat_translations_from_commons && wgServer.indexOf( '//commons' ) < 0 ) {
loadURI( '//wikihaj.com/index.php?title=' +
loadURI( '//commons.wikimedia.org/w/index.php?title=' +
'MediaWiki:Gadget-HotCat.js/' + conf.wgUserLanguage +
'MediaWiki:Gadget-HotCat.js/' + conf.wgUserLanguage +
'&action=raw&ctype=text/javascript', loadTrigger.loaded );
'&action=raw&ctype=text/javascript', loadTrigger.loaded );
خط ۳۸۰: خط ۳۸۴:


var script = conf.wgScript + '?';
var script = conf.wgScript + '?';
if ( href.indexOf( script ) === 0 || href.indexOf( conf.wgServer + script ) === 0 || conf.wgServer.substring( 0, 2 ) === '//' && href.indexOf( document.location.protocol + conf.wgServer + script ) === 0 ) {
if ( href.indexOf( script ) === 0 || href.indexOf( wgServer + script ) === 0 || wgServer.substring( 0, 2 ) === '//' && href.indexOf( document.location.protocol + wgServer + script ) === 0 ) {
// href="/w/index.php?title=..."
// href="/w/index.php?title=..."
return param( 'title', href );
return param( 'title', href );
خط ۳۸۶: خط ۳۹۰:
// href="/wiki/..."
// href="/wiki/..."
var prefix = conf.wgArticlePath.replace( '$1', '' );
var prefix = conf.wgArticlePath.replace( '$1', '' );
if ( href.indexOf( prefix ) ) prefix = conf.wgServer + prefix; // Fully expanded URL?
if ( href.indexOf( prefix ) ) prefix = wgServer + prefix; // Fully expanded URL?


if ( href.indexOf( prefix ) && prefix.substring( 0, 2 ) === '//' ) prefix = document.location.protocol + prefix; // Protocol-relative wgServer?
if ( href.indexOf( prefix ) && prefix.substring( 0, 2 ) === '//' ) prefix = document.location.protocol + prefix; // Protocol-relative wgServer?
خط ۷۲۳: خط ۷۲۷:
if ( page.starttimestamp ) startTime = page.starttimestamp.replace( /\D/g, '' );
if ( page.starttimestamp ) startTime = page.starttimestamp.replace( /\D/g, '' );
pageWatched = typeof page.watched === 'string';
pageWatched = typeof page.watched === 'string';
editToken = page.edittoken;
if ( json.query.tokens ) editToken = json.query.tokens.csrftoken;
if ( page.langlinks && ( !json[ 'query-continue' ] || !json[ 'query-continue' ].langlinks ) ) {
if ( page.langlinks && ( !json[ 'query-continue' ] || !json[ 'query-continue' ].langlinks ) ) {
// We have interlanguage links, and we got them all.
// We have interlanguage links, and we got them all.
خط ۷۷۳: خط ۷۷۷:
// Must use Ajax here to get the user options and the edit token.
// Must use Ajax here to get the user options and the edit token.
$.getJSON(
$.getJSON(
conf.wgServer + conf.wgScriptPath + '/api.php?' +
wgServer + conf.wgScriptPath + '/api.php?' +
'format=json&action=query&rawcontinue=&titles=' + encodeURIComponent( conf.wgPageName ) +
'format=json&action=query&rawcontinue=&titles=' + encodeURIComponent( conf.wgPageName ) +
'&prop=info%7Crevisions%7Clanglinks&inprop=watched&intoken=edit&rvprop=content%7Ctimestamp%7Cids%7Cuser&lllimit=500' +
'&prop=info%7Crevisions%7Clanglinks&inprop=watched&rvprop=content%7Ctimestamp%7Cids%7Cuser&lllimit=500' +
'&rvlimit=2&rvdir=newer&rvstartid=' + conf.wgCurRevisionId + '&meta=siteinfo%7Cuserinfo&uiprop=options',
'&rvlimit=2&rvdir=newer&rvstartid=' + conf.wgCurRevisionId + '&meta=siteinfo%7Cuserinfo%7Ctokens&type=csrf&uiprop=options',
function ( json ) {
function ( json ) {
setPage( json );
setPage( json );
خط ۹۱۷: خط ۹۲۱:
var shortSummary = [];
var shortSummary = [];
// Deleted
// Deleted
for ( i = 0; i < deleted.length; i++ ) summary.push( '-' + substitute( HC.messages.short_catchange, [ null, deleted[ i ] ] ) );
for ( i = 0; i < deleted.length; i++ ) summary.push( '' + substitute( HC.messages.short_catchange, [ null, deleted[ i ] ] ) );


if ( deleted.length === 1 ) shortSummary.push( '-' + substitute( HC.messages.short_catchange, [ null, deleted[ 0 ] ] ) ); else if ( deleted.length ) shortSummary.push( '- ' + multiChangeMsg( deleted.length ) );
if ( deleted.length === 1 ) shortSummary.push( '' + substitute( HC.messages.short_catchange, [ null, deleted[ 0 ] ] ) ); else if ( deleted.length ) shortSummary.push( '' + multiChangeMsg( deleted.length ) );


// Added
// Added
خط ۱٬۰۶۰: خط ۱٬۰۶۴:
if ( i + 1 < toResolve.length ) args += '%7C';
if ( i + 1 < toResolve.length ) args += '%7C';
}
}
$.getJSON( conf.wgServer + conf.wgScriptPath + '/api.php?' + args,
$.getJSON( wgServer + conf.wgScriptPath + '/api.php?' + args,
function ( json ) {
function ( json ) {
resolveRedirects( toResolve, json );
resolveRedirects( toResolve, json );
خط ۱٬۸۲۰: خط ۱٬۸۲۴:
if ( next ) next.parentNode.removeChild( next );
if ( next ) next.parentNode.removeChild( next );
}
}
this.span.parentNode.removeChild( this.span );
if (this.span && this.span.parentNode) {
this.span.parentNode.removeChild( this.span );
}
for ( var i = 0; i < editors.length; i++ ) {
for ( var i = 0; i < editors.length; i++ ) {
if ( editors[ i ] === this ) {
if ( editors[ i ] === this ) {
خط ۲٬۰۸۲: خط ۲٬۰۸۸:
var re = new RegExp( '^(' + HC.category_regexp + '):' );
var re = new RegExp( '^(' + HC.category_regexp + '):' );
if ( re.test( v ) ) v = v.substring( v.indexOf( ':' ) + 1 ).replace( /^(\s|_)+/, '' );
if ( re.test( v ) ) v = v.substring( v.indexOf( ':' ) + 1 ).replace( /^(\s|_)+/, '' );
 
v = v.replace(/\u200E$/, ''); // Trim ending left-to-right mark
if ( HC.capitalizePageNames ) v = capitalize( v );
if ( HC.capitalizePageNames ) v = capitalize( v );


// Only update the input field if there is a difference. Various browsers otherwise
// Update the input field if needed, manually restoring the cursor afterwards
// reset the selection and cursor position after each value re-assignment.
// (but not the whole selection, so that further typing does not overwrite anything)
if ( this.text.value !== null && this.text.value !== v ) this.text.value = v;
if ( this.text.value !== null && this.text.value !== v ) {
var cursorPos = this.text.selectionStart;
this.text.value = v;
this.text.setSelectionRange( cursorPos, cursorPos );
}
},
},


خط ۲٬۱۹۹: خط ۲٬۲۰۹:
for ( var j = 0; j < engines.length; j++ ) {
for ( var j = 0; j < engines.length; j++ ) {
var engine = suggestionEngines[ engines[ j ] ];
var engine = suggestionEngines[ engines[ j ] ];
var url = conf.wgServer + conf.wgScriptPath + engine.uri.replace( /\$1/g, encodeURIComponent( cleanKey ) );
var url = wgServer + conf.wgScriptPath + engine.uri.replace( /\$1/g, encodeURIComponent( cleanKey ) );
this.makeCall( url, cb, engine, v, cleanKey );
this.makeCall( url, cb, engine, v, cleanKey );
}
}
خط ۲٬۹۰۱: خط ۲٬۹۱۱:
// Change the onsubmit handler
// Change the onsubmit handler
var form = document.getElementById( 'upload' ) || document.getElementById( 'mw-upload-form' );
var form = document.getElementById( 'upload' ) || document.getElementById( 'mw-upload-form' );
if ( form ) {
if ( form && ip && ip.insertRow ) {
var newRow = ip.insertRow( -1 );
var newRow = ip.insertRow( -1 );
newRow.appendChild( labelCell );
newRow.appendChild( labelCell );
خط ۲٬۹۷۷: خط ۲٬۹۸۷:
var result = $( scope ).find( tag + '.' + className );
var result = $( scope ).find( tag + '.' + className );
return ( result && result.length ) ? result[ 0 ] : null;
return ( result && result.length ) ? result[ 0 ] : null;
}
function errorAMC() {
alert( 'An error occurred. Unable to setup HotCat' );
}
function enableAMC() {
var api = new mw.Api();
return api.saveOption( 'mf_amc_optin', '1' ).then( function ( r ) {
if ( !r || r.options !== 'success' ) {
errorAMC();
return;
}
if ( window.confirm( 'Please reload your page to use hotcat.' ) ) {
location.reload();
}
}, function () {
errorAMC();
} );
}
function showWarning( text ) {
var warning = document.createElement( 'div' );
warning.setAttribute( 'style', 'padding: 20px; background: orange; color: #333; font-weight: bold; margin-top: 20px;' );
warning.textContent = text;
var btn = document.createElement( 'button' );
btn.classList.add( 'mw-ui-button', 'cdx-button' );
btn.style.display = 'block';
btn.textContent = 'Enable HotCat and AMC mode on this page';
btn.addEventListener( 'click', function () {
enableAMC();
} );
warning.appendChild( btn );
document.getElementById( 'mw-content-text' ).appendChild( warning );
}
}


خط ۲٬۹۹۴: خط ۳٬۰۳۸:
var hiddenCats = document.getElementById( 'mw-hidden-catlinks' );
var hiddenCats = document.getElementById( 'mw-hidden-catlinks' );
if ( !catLine ) {
if ( !catLine ) {
// Workaround for T24660
if ( mw.config.get('skin') === 'minerva' ) {
if ( document.body.classList.contains('mw-mf-amc-disabled') ) {
showWarning( 'HotCat requires AMC mode.' );
}
}
var footer = null;
var footer = null;
if ( !hiddenCats ) {
if ( !hiddenCats ) {
خط ۳٬۰۹۸: خط ۳٬۱۴۹:
'<input type="hidden" name="wpDiff" value="wpDiff">' +
'<input type="hidden" name="wpDiff" value="wpDiff">' +
'<input type="hidden" name="oldid" value="0">' +
'<input type="hidden" name="oldid" value="0">' +
'<input type="hidden" name="wpIgnoreBlankSummary" value="1">' +
'<input type="submit" name="hcCommit" value="hcCommit">' +
'<input type="submit" name="hcCommit" value="hcCommit">' +
'<input type="hidden" name="wpEditToken">' +
'<input type="hidden" name="wpEditToken">' +
خط ۳٬۱۱۶: خط ۳٬۱۶۸:
setup( createCommitForm );
setup( createCommitForm );
} else {
} else {
var url = conf.wgServer + conf.wgScriptPath + '/api.php?format=json&callback=HotCat.start&action=query&rawcontinue=&titles=' +
var url = wgServer + conf.wgScriptPath + '/api.php?format=json&callback=HotCat.start&action=query&rawcontinue=&titles=' +
encodeURIComponent( conf.wgPageName ) +
encodeURIComponent( conf.wgPageName ) +
'&prop=info%7Crevisions&rvprop=content%7Ctimestamp%7Cids&meta=siteinfo&rvlimit=1&rvstartid=' +
'&prop=info%7Crevisions&rvprop=content%7Ctimestamp%7Cids&meta=siteinfo&rvlimit=1&rvstartid=' +
خط ۳٬۲۳۴: خط ۳٬۲۸۶:
// Reload HotCat after (VE) edits (bug T103285)
// Reload HotCat after (VE) edits (bug T103285)
mw.hook( 'postEdit' ).add( function () {
mw.hook( 'postEdit' ).add( function () {
// Reset HotCat in case this is a soft reload (VE edit)
// Reset HotCat in case this is a soft reload (e.g. VisualEditor edit), unless the categories
// were not re-rendered and our interface is still there (e.g. DiscussionTools edit)
if ( document.querySelector( '#catlinks .hotcatlink' ) ) {
return;
}
catLine = null;
catLine = null;
editors = [];
editors = [];