|
|
(未显示同一用户的44个中间版本) |
第1行: |
第1行: |
− | /**
| + | mw.loader.load('https://cdn.jsdelivr.net/npm/mediawiki-inpageedit@latest/dist/InPageEdit.min.js'); |
− | * MediaWiki JS Plugin: In Page Edit
| + | mw.hook('InPageEdit').add(function (ctx) { |
− | * Author: 机智的小鱼君
| + | var InPageEdit = ctx.InPageEdit, |
− | * Url: https://github.com/Dragon-Fish/wjghj-wiki/edit/master/Gadgets/in-page-edit
| + | _msg = ctx._msg, |
− | * Description: Let you edit page without open new tab. And edit Navebox via navbar, edit section via section edit link etc.
| + | wgPageName = mw.config.get('wgPageName'), |
− | **/
| + | wgRevisionId = mw.config.get('wgRevisionId'); |
− | function InPageEdit(option) {
| + | $('#ca-edit').after( |
− | // 只能存在一个窗口
| + | $('<li>', { |
− | if ($('#InPageEdit').length > 0) {
| + | id: 'ca-quick-edit', |
− | $('#InPageEdit').remove();
| + | 'class': 'collapsible' |
− | }
| + | }).append( |
− | // 开始执行任务
| + | $('<span>').append( |
− | $('body').addClass('action-in-page-edit');
| + | $('<a>', { |
− |
| + | href: 'javascript:void(0)', |
− | // Variables
| + | text: _msg('quick-edit') |
− | var origintext, | + | }).on('click', function () { |
− | inPageEditTarget = decodeURIComponent(option.target),
| + | InPageEdit.quickEdit({ |
− | inPageEditSection = option.section,
| + | page: wgPageName, |
− | inPageEditReason = option.reason,
| + | revision: wgRevisionId |
− | inPageEditTags = option.tags,
| + | }); |
− | inPageEditRefresh = option.refresh;
| + | }) |
− | if (inPageEditTarget === undefined || inPageEditTarget === ''){inPageEditTarget = mw.config.get('wgPageName')}
| + | ) |
− | if (inPageEditSection=== undefined || inPageEditSection === ''){inPageEditSection = ''}else{inPageEditSection=parseInt(inPageEditSection)-1}
| + | ) |
− | if (inPageEditReason === undefined || inPageEditReason === ''){inPageEditReason = ''}
| + | ); |
− | if (inPageEditRefresh === undefined || inPageEditRefresh == 'true' || inPageEditRefresh == '1'){inPageEditRefresh = true;}
| |
− | if (inPageEditTags === undefined || inPageEditTags === ''){inPageEditTags = ''}
| |
− |
| |
− | new mw.Api().get({
| |
− | action: "parse",
| |
− | page: inPageEditTarget,
| |
− | section: inPageEditSection,
| |
− | prop: "wikitext",
| |
− | format: "json"
| |
− | }).then(function(data) {
| |
− | origintext = '<!-- ⚠ Notice: Can’t get page content. Delete this line if you are creating a new page. -->\n';
| |
− | ajaxArea()
| |
− | }).fail(function() {
| |
− | origintext = '<!-- ⚠ Notice: Can’t get page content. Delete this line if you are creating a new page. -->\n';
| |
− | ajaxArea()
| |
− | }); | |
− | function ajaxArea() {
| |
− | // Create area & hide article
| |
− | $('#mw-content-text').hide();
| |
− | $('#mw-content-text').before( | |
− | '<div id="InPageEdit">' +
| |
− |
| |
− | '<h1 id="edit-title">in-page-edit-title</h1>' +
| |
− | '<textarea id="newcontent" style="width:100%;min-height:300px;max-height:1200px"></textarea>' + | |
− |
| |
− | '<div id="button-area">' +
| |
− | '<div id="normal"><button id="cancle-btn">Cancel</button> <button id="preview-btn">Preview</button> <label><input type="checkbox" id="is-minor"/> Minor edit</label> <div style="float:right"><input id="reason" placeholder="Summary" value="'+inPageEditReason+'"> <button id="submit-btn">Publish</button></div></div>' +
| |
− |
| |
− | '<center id="confirm" style="display:none;clear:both"><span id="code"></span><br/><button id="no">No</button> <button id="yes">Yes</button></center>' + | |
− |
| |
− | '</div>' +
| |
− |
| |
− | '<center id="info-area" style="display:none;"></center>' +
| |
− |
| |
− | '<h1>Preview</h1>' +
| |
− | '<div id="preview-area" style="padding:8px; border:2px dotted #aaa"></div>' +
| |
− |
| |
− | '</div>'
| |
− | );
| |
− | $('#InPageEdit #newcontent').val(origintext);
| |
− | $('#InPageEdit #edit-title').html('Editing: ' + inPageEditTarget); | |
− |
| |
− | // Cancle
| |
− | $('#InPageEdit #cancle-btn').click(function() {
| |
− | $('#InPageEdit #button-area #normal').hide();
| |
− | $('#InPageEdit #confirm').show(); $('#InPageEdit #confirm button').unbind();
| |
− | $('#InPageEdit #confirm #code').text('Are you sure to cancel the action?');
| |
− | $('#InPageEdit #confirm #no').click(function(){$('#InPageEdit #button-area #normal').show();$('#InPageEdit #confirm').hide();});
| |
− | $('#InPageEdit #confirm #yes').click(function(){
| |
− | $('body').removeClass('action-in-page-edit');
| |
− | $('#InPageEdit').remove();
| |
− | $('#mw-content-text').show();
| |
− | });
| |
− | });
| |
− |
| |
− | // Preview
| |
− | $('#InPageEdit #preview-btn').click(function() {
| |
− | new mw.Api().post({
| |
− | action: "parse",
| |
− | text: $('#InPageEdit #newcontent').val(),
| |
− | prop: "text",
| |
− | preview: true,
| |
− | format: "json"
| |
− | }).then(function(data) {
| |
− | var previewcontent = data.parse.text['*'];
| |
− |
| |
− | $('#InPageEdit #preview-area').html(previewcontent);
| |
− | });
| |
− | });
| |
− |
| |
− | // Submit
| |
− | $('#InPageEdit #submit-btn').click(function() {
| |
− | $('#InPageEdit #button-area #normal').hide();
| |
− | $('#InPageEdit #confirm').show(); $('#InPageEdit #confirm button').unbind();
| |
− | $('#InPageEdit #confirm #code').text('Are you sure to submit your edits?');
| |
− | $('#InPageEdit #confirm #no').click(function(){$('#InPageEdit #button-area #normal').show();$('#InPageEdit #confirm').hide();});
| |
− | $('#InPageEdit #confirm #yes').click(function(){ | |
− | // Hide elements
| |
− | $('#InPageEdit #newcontent').attr('readonly','readonly');
| |
− | $('#InPageEdit #button-area').hide();
| |
− | $('#InPageEdit #info-area').show().html('<div style="font-weight:bold;color:green;min-height: 50px;line-height: 50px;font-size: 36px;">Submitting <img src="https://wjghj.cn/images/9/98/Windows-loading.gif" style="height:36px;width:auto" /></div>');
| |
− |
| |
− | // Do post request
| |
− | var isMinor = $('#InPageEdit #is-minor').prop('checked');
| |
− | new mw.Api().post({ | |
− | action: 'edit',
| |
− | text: $('#InPageEdit #newcontent').val(),
| |
− | title: inPageEditTarget,
| |
− | section: inPageEditSection,
| |
− | minor: isMinor,
| |
− | tags: inPageEditTags,
| |
− | summary: $('#InPageEdit #reason').val(),
| |
− | token: mw.user.tokens.get('editToken')
| |
− | }).done(function() {
| |
− | $('#InPageEdit #info-area').html('<div style="font-weight:bold;color:green;min-height: 50px;line-height: 50px;font-size: 36px;">Successful</div>');
| |
− | window.location.href=window.location.href;
| |
− | }).fail(function(){
| |
− | // Show elements | |
− | $('#InPageEdit #submit-btn').html('Retry');
| |
− | $('#InPageEdit #newcontent').attr('readonly',false);
| |
− | $('#InPageEdit #button-area, #InPageEdit #button-area #normal').show();
| |
− | $('#InPageEdit #confirm').hide();
| |
− | Modal('<span class="error">Error post your request.</span>','InPageEdit Error');
| |
− | $('#InPageEdit #info-area').hide().html('');
| |
− | });
| |
− | });
| |
− | });
| |
− | }
| |
− | }
| |
− |
| |
− | /** Add button **/
| |
− | $(function() {
| |
− | if (wgIsArticle === false) {
| |
− | console.info('[InPageEdit] Not article page, plugin shut down.');
| |
− | return;
| |
− | }
| |
− | $('.action-view #p-userpagetools ul, #p-views .mw-portlet-body ul').append($('<li>').append($('<a>').addClass('in-page-edit-btn-link').attr('href', 'javascript:void(0)').text('快速编辑').click(function() {
| |
− | InPageEdit({target:mw.config.get('wgPageName'), reason:' //InPageEdit'})
| |
− | })));
| |
− | });
| |
− | /** Get links in ariticle **/
| |
− | $(function() {
| |
− | $('#mw-content-text a:not(.new)').each(function(i) {
| |
− | if ($(this).attr('href') === undefined) return;
| |
− | var url = $(this).attr('href');
| |
− | params = {}; | |
− | var vars = url.split('?').pop().split("&");
| |
− | for (var i=0;i<vars.length;i++) {
| |
− | var pair = vars[i].split("=");
| |
− | params[pair[0]] = pair[1];
| |
− | }
| |
− |
| |
− | // Not edit link of this wiki
| |
− | if (url.split('/')['2'] !== location.href.split('/')['2'] && url.substr(0, 1)!=='/') return;
| |
− | // Not url start with 'index.php?title=FOO'
| |
− | if (params.title === undefined) params.title = url.split('/wiki/')['1'].split('?')['0'];
| |
− | if (params.section === undefined) params.section = '';
| |
− | | |
− | var target = params.title,
| |
− | section = params.section;
| |
− |
| |
− | if (params.action === 'edit' && params.title !== undefined && params.section !== 'new') {
| |
− | $(this).after($('<a>').attr({
| |
− | 'href': 'javascript:void(0)',
| |
− | 'class': 'in-page-edit-article-link',
| |
− | 'data-ipe-target': params.title,
| |
− | 'data-ipe-section': params.section
| |
− | }).text('[QuickEdit]').click(function (){
| |
− | InPageEdit({target:target, reason:' //InPageEdit', section:section}); | |
− | })); | |
− | } | |
− | }); | |
| }); | | }); |