User:SunAfterRain/js/articleInfo.js

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// from https://zh.wikipedia.org/wiki/User:AnYiLin/js/ArticleInfo-wikimirror.js?oldid=64787252
$( () => {
	if ( !(
		window.wgXtoolArticleInfoForceLoad
		|| window.location.host.match( /^(.*\.(wiki(pedia|books|news|quote|source|versity|voyage)|wiktionary)|(www|m)\.(wikidata|mediawiki)|(meta|commons|species)\.(m\.)?wikimedia)\.org$/ )
	) ) {
		return;
	}
	
	let selector = '#contentSub';
	if ( mw.config.get( 'skin' ) === 'minerva' ) {
		if ( mw.config.get( 'wgIsMainPage' ) ) {
			return;
		}
		selector = '#mw-content-subtitle';
	}

	let $dom = $( selector );
	if (
		!$dom.length ||
		mw.config.get( 'wgAction' ) !== 'view' ||
		!mw.config.get( 'wgIsArticle' ) ||
		!mw.config.get( 'wgCurRevisionId' )  ||
		!mw.config.get( 'wgRevisionId' ) ||
		!!mw.config.get( 'wgDiffNewId' )  ||
		mw.config.get( 'wgCurRevisionId' ) !== mw.config.get( 'wgRevisionId' )
	) {
		return;
	}
	let url = new URL( 'https://xtools.wmcloud.org/' );
	url.pathname = '/api/page/articleinfo/' + mw.config.get( 'wgDBname' ) + '/' + mw.config.get( 'wgPageName' );
	url.searchParams.append( 'format', 'html' );
	url.searchParams.append( 'uselang', mw.config.get( 'wgUserLanguage' ) );
	url.searchParams.append( '_origin', window.location.origin ); // 避免行動版的快取和網頁版的快取混淆

	fetch( url )
		.then( ( res ) => res.status === 200 ? res.text() : '<strong class="error">Xtool Article Info Load Fail!</strong>' )
		.catch( ( error ) => ( Promise.reject( error ), '<strong class="error">Xtool Article Info Load Fail!</strong>' ) )
		.then( ( html ) => $dom.before(
			$( '<div>' )
				.attr( {
					id: 'xtools',
					style: 'font-size: 85%; line-height: 1.2em; width:auto;'
				} ).html( html )
		) );
} );