|
|
第1行: |
第1行: |
| if (mw.config.get('wgAction') === 'edit' || mw.config.get('wgAction') === 'submit') {
| | mw.loader.load("/index.php?title=OTTOWiki:Gadget-CodeMirror6.js&action=raw&ctype=text/javascript"); |
| mw.loader.load('https://cdn.jsdelivr.net/npm/@bhsd/codemirror-mediawiki/mediawiki.css', 'text/css');
| | |
| (async function() {
| | // 代码在[[OTTOWiki:Gadget-CodeMirror6.js]] 因为用了异步函数需要这样加载 话说不是说MW支持ES6了吗 |
| await mw.loader.using('mediawiki.Title');
| |
| if (!window.CodeMirror6) {
| |
| await import('https://cdn.jsdelivr.net/npm/@bhsd/codemirror-mediawiki/dist/wiki.min.js');
| |
| }
| |
| const textarea = document.getElementById('wpTextbox1');
| |
| if (textarea) {
| |
| const page = new mw.Title(mw.config.get('wgPageName'));
| |
| const getPageMode = (page) => {
| |
| const { namespace, title } = page;
| |
| const ext = page.getExtension()?.toLowerCase();
| |
| const isSubject = namespace % 2 === 0;
| |
| if (ext === 'css' && isSubject) {
| |
| return [2, 8, 2300].includes(namespace) ? 'css' : 'sanitized-css';
| |
| } else if (ext === 'js' && isSubject) {
| |
| return 'javascript';
| |
| } else if (ext === 'json' && isSubject) {
| |
| return 'json';
| |
| } else if (namespace === 828 && !title.endsWith('/doc')) {
| |
| return 'lua';
| |
| } else if (namespace === 274 && !title.endsWith('/doc')) {
| |
| return 'html';
| |
| }
| |
| return 'mediawiki';
| |
| };
| |
| const mode = getPageMode(page);
| |
| const cm = await CodeMirror6.fromTextArea(textarea, mode, page.getNamespaceId());
| |
| if (mode === 'mediawiki') {
| |
| try {
| |
| const config = mw.config.get('extCodeMirrorConfig');
| |
| if (config?.urlProtocols.includes('\\:')) {
| |
| config.urlProtocols = config.urlProtocols.replace(/\\:/g, ':');
| |
| cm.setLanguage('mediawiki', config);
| |
| }
| |
| } catch (error) {
| |
| console.error('Failed to load or apply mwConfig:', error.message, error.stack);
| |
| }
| |
| }
| |
| }
| |
| })();
| |
| }
| |
mw.loader.load("/index.php?title=OTTOWiki:Gadget-CodeMirror6.js&action=raw&ctype=text/javascript");
// 代码在[[OTTOWiki:Gadget-CodeMirror6.js]] 因为用了异步函数需要这样加载 话说不是说MW支持ES6了吗