LLWiki正在建设中,欢迎加入我们!
“MediaWiki:Gadget-inspect.js”的版本间差异
跳转到导航
跳转到搜索
小 |
|||
第1行: | 第1行: | ||
// 由[[special:mypage/vector.js]]调用,可以使用ES6语法 |
// 由[[special:mypage/vector.js]]调用,可以使用ES6语法 |
||
/** |
/** |
||
* @dependencies: mediawiki.api, mediawiki.util, oojs-ui-core, jquery.ui, ext.CodeMirror.lib, ext.CodeMirror.mode.mediawiki |
* @dependencies: mediawiki.api, mediawiki.util, oojs-ui-core, oojs-windows, oojs-styles.icons-movement, jquery.ui, ext.CodeMirror.lib, ext.CodeMirror.mode.mediawiki |
||
*/ |
*/ |
||
"use strict"; |
"use strict"; |
||
/*global mw, $, OO, CodeMirror*/ |
/*global mw, $, OO, CodeMirror*/ |
||
(() => { |
|||
⚫ | |||
let text |
let text, editor, wrapper; |
||
const css = mw.util.addCSS( '#mw-content-text > .mw-parser-output { display:flow-root; }' + |
const css = mw.util.addCSS( '#mw-content-text > .mw-parser-output { display:flow-root; }' + |
||
'#mw-content-text p { border:1px dashed; }'), |
'#mw-content-text p { border:1px dashed; }'), |
||
第13行: | 第13行: | ||
$content = $('#mw-content-text'), |
$content = $('#mw-content-text'), |
||
id = mw.config.get( 'wgArticleId' ), |
id = mw.config.get( 'wgArticleId' ), |
||
script = mw.config.get('wgScript'), |
|||
api = new mw.Api(), |
api = new mw.Api(), |
||
dialog = new OO.ui.MessageDialog(), |
|||
⚫ | |||
actionP = new OO.ui.ActionWidget({label: '是', target: '_blank', flags: 'progressive'}), |
|||
⚫ | |||
actionD = new OO.ui.ActionWidget({label: '否', flags: 'destructive'}), |
|||
urlDisplay = $('<a>', {href: "#"}).click(e => { e.preventDefault(); }), |
|||
⚫ | |||
⚫ | |||
.on('click', '.cm-mw-template-name', function() { |
|||
urlDisplay.text( `模板:${this.textContent}` ); |
|||
actionP.setHref( `${script}/template:${this.textContent}` ); |
|||
mw.windowManager.openWindow( dialog, {actions: [actionP, actionD]}).opening.then(() => { |
|||
actionP.$button.off( 'click' ).click(() => { dialog.close(); }); |
|||
}); |
|||
}).on('click', '.cm-mw-link-pagename', function() { |
|||
urlDisplay.text( this.textContent ); |
|||
actionP.setHref( `${script}/${this.textContent}` ); |
|||
mw.windowManager.openWindow( dialog, {actions: [actionP, actionD]}).opening.then(() => { |
|||
actionP.$button.off( 'click' ).click(() => { dialog.close(); }); |
|||
}); |
|||
}), |
|||
btns = [new OO.ui.ButtonWidget({label: '提交', flags: ['primary', 'progressive']}).on('click', () => { |
btns = [new OO.ui.ButtonWidget({label: '提交', flags: ['primary', 'progressive']}).on('click', () => { |
||
console.log('API request: 查询最新编辑时间戳'); |
console.log('API request: 查询最新编辑时间戳'); |
||
第47行: | 第63行: | ||
}), new OO.ui.ButtonWidget({flags: 'progressive', icon: 'next', href: `/?redirect=no&curid=${id + 1}`})]; |
}), new OO.ui.ButtonWidget({flags: 'progressive', icon: 'next', href: `/?redirect=no&curid=${id + 1}`})]; |
||
cssHide.disabled = true; |
cssHide.disabled = true; |
||
if (!mw.windowManager) { |
|||
⚫ | |||
mw.windowManager = new OO.ui.WindowManager(); |
|||
$('body').append( mw.windowManager.$element ); |
|||
} |
|||
mw.windowManager.addWindows( [dialog] ); |
|||
dialog.message.$label.html( ["要打开", urlDisplay, "吗?"] ); |
|||
mw.request = mw.request || api.get({action: 'query', revids: mw.config.get('wgRevisionId'), prop: 'revisions', |
mw.request = mw.request || api.get({action: 'query', revids: mw.config.get('wgRevisionId'), prop: 'revisions', |
||
rvprop: 'content', curtimestamp: 1, formatversion: 2}); |
rvprop: 'content', curtimestamp: 1, formatversion: 2}); |
||
⚫ | |||
⚫ | |||
outer.removeClass( 'mw-ajax-loader' ); |
|||
⚫ | |||
editor = new CodeMirror(outer[0], {mode: 'text/mediawiki', mwConfig: config, lineWrapping: true}); |
|||
⚫ | |||
editor. |
wrapper = $( editor.getWrapperElement() ); |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
editor.setValue( text ); |
|||
⚫ | |||
}); |
|||
⚫ |
2020年12月19日 (六) 12:19的版本
// 由[[special:mypage/vector.js]]调用,可以使用ES6语法 /** * @dependencies: mediawiki.api, mediawiki.util, oojs-ui-core, oojs-windows, oojs-styles.icons-movement, jquery.ui, ext.CodeMirror.lib, ext.CodeMirror.mode.mediawiki */ "use strict"; /*global mw, $, OO, CodeMirror*/ (() => { let text, editor, wrapper; const css = mw.util.addCSS( '#mw-content-text > .mw-parser-output { display:flow-root; }' + '#mw-content-text p { border:1px dashed; }'), cssHide = mw.util.addCSS( '.inspector > div:last-child { margin-bottom:0.5em; }' + '.inspector { position:fixed; right:24px; left:unset !important; width:calc(50% - 7rem - 0.5px); }' ), $content = $('#mw-content-text'), id = mw.config.get( 'wgArticleId' ), script = mw.config.get('wgScript'), api = new mw.Api(), dialog = new OO.ui.MessageDialog(), actionP = new OO.ui.ActionWidget({label: '是', target: '_blank', flags: 'progressive'}), actionD = new OO.ui.ActionWidget({label: '否', flags: 'destructive'}), urlDisplay = $('<a>', {href: "#"}).click(e => { e.preventDefault(); }), outer = $('<div>', {class: 'inspector mw-ajax-loader'}).resizable({handles: 'w', minWidth: 350}) .insertBefore( $content.children( '.mw-parser-output, #mw-imagepage-content' ) ) .on('click', '.cm-mw-template-name', function() { urlDisplay.text( `模板:${this.textContent}` ); actionP.setHref( `${script}/template:${this.textContent}` ); mw.windowManager.openWindow( dialog, {actions: [actionP, actionD]}).opening.then(() => { actionP.$button.off( 'click' ).click(() => { dialog.close(); }); }); }).on('click', '.cm-mw-link-pagename', function() { urlDisplay.text( this.textContent ); actionP.setHref( `${script}/${this.textContent}` ); mw.windowManager.openWindow( dialog, {actions: [actionP, actionD]}).opening.then(() => { actionP.$button.off( 'click' ).click(() => { dialog.close(); }); }); }), btns = [new OO.ui.ButtonWidget({label: '提交', flags: ['primary', 'progressive']}).on('click', () => { console.log('API request: 查询最新编辑时间戳'); const now = mw.now(); api.get({action: 'query', prop: 'revisions', pageids: id, rvlimit: 1, rvprop: 'timestamp', formatversion: 2}) .then(res => { console.log(`End API request: 已获得最新编辑时间戳,用时 ${mw.now() - now} ms`); if (new Date(res.query.pages[0].revisions[0].timestamp) > new Date(mw.request.curtimestamp)) { mw.notify('编辑冲突!', {type: 'error'}); return; } api.postWithEditToken({action: 'edit', pageid: id, text: editor.getValue()}).then(() => { window.location.reload(); }, reason => { mw.notify(`编辑失败!错误信息:${reason}`, {type: 'error'}); }); }, reason => { mw.notify(`无法获得最新编辑时间戳!错误信息:${reason}`, {type: 'error'}); }); }), new OO.ui.ButtonWidget({label: '预览'}).on('click', () => { console.log('API request: 请求预览'); const now = mw.now(); api.parse( editor.getValue(), { disablelimitreport: 1, disableeditsection: 1} ).then(html => { console.log(`End API request: 已生成预览,用时 ${mw.now() - now} ms`); $content.children( '.mw-parser-output' ).replaceWith( html ); mw.hook( 'wikipage.content' ).fire($content); }, reason => { mw.notify(`无法生成预览!错误信息:${reason}`, {type: 'error'}); }); }), new OO.ui.ButtonWidget({label: '还原', flags: 'destructive'}).on('click', () => { editor.setValue( text ); }), new OO.ui.ButtonWidget({label: '隐藏'}).on('click', () => { css.disabled = !css.disabled; cssHide.disabled = !cssHide.disabled; wrapper.toggle(); }), new OO.ui.ButtonWidget({flags: 'progressive', icon: 'next', href: `/?redirect=no&curid=${id + 1}`})]; cssHide.disabled = true; if (!mw.windowManager) { mw.windowManager = new OO.ui.WindowManager(); $('body').append( mw.windowManager.$element ); } mw.windowManager.addWindows( [dialog] ); dialog.message.$label.html( ["要打开", urlDisplay, "吗?"] ); mw.request = mw.request || api.get({action: 'query', revids: mw.config.get('wgRevisionId'), prop: 'revisions', rvprop: 'content', curtimestamp: 1, formatversion: 2}); $.getJSON( '/zh?title=mediawiki:gadget-CodeMirror.json&action=raw&ctype=application/json', config => { outer.removeClass( 'mw-ajax-loader' ); editor = new CodeMirror(outer[0], {mode: 'text/mediawiki', mwConfig: config, lineWrapping: true}); wrapper = $( editor.getWrapperElement() ); $('<div>', {html: btns.map(ele => ele.$element)}).appendTo( outer ); mw.request.then(data => { console.log('End API request: 已获得页面Wikitext'); text = data.query.pages[0].revisions[0].content; editor.setValue( text ); }, reason => { mw.notify(`无法获得页面Wikitext!错误原因${reason}`, {type: 'error'}); }); }); }) ();