跳转到内容

User:Tommyang/Script/wikilove.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
//[[en:User:Kaldari/wikilove.js]]<nowiki>
if (( wgCanonicalNamespace == 'User_talk' && wgAction == 'view' && skin == 'vector' )) {
  // Script depends on jQuery dialog widget
  mw.loader.using( 'jquery.ui', function() {
    // Construct object (to prevent namespace conflicts)
    wikiLove = {
 
      displayProgress: function( form, message ) {
        $('#'+form+' div').hide(); // remove everything else from the dialog box
        $('#'+form).append ( $('<div style="text-align:center;margin:3em 0;"></div>').html( message+'<br/><img src="http://upload.wikimedia.org/wikipedia/commons/4/42/Loading.gif" />' ) );
      },
 
      displayError: function( form, error ) {
        $('#'+form+' div').hide(); // remove everything else from the dialog box
        $('#'+form).append ( $('<div style="color:#990000;margin-top:0.4em;"></div>').html( '错误: '+error ) );
      },
 
      getEditToken: function() {
        $.ajax({
          url: wgScriptPath + '/api.php?',
          data: 'action=query&prop=info&intoken=edit&titles=Main%20Page&indexpageids&format=json',
          dataType: 'json',
          success: function( data ) {
            if ( data.query.pages && data.query.pageids ) {
              var pageid = data.query.pageids[0];
              wikiLove.editToken = data.query.pages[pageid].edittoken;
            }
          }
        });
      },
 
      getEmailable: function() {
        // Test to see if the 'E-mail this user' link exists
        wikiLove.emailable = $('#t-emailuser').length ? true : false;
      },
 
      editPage: function( form, summary, template ) {
        $.ajax({
          url: wgScriptPath + '/api.php?',
          data: 'action=edit&title='+encodeURIComponent(mw.config.get('wgPageName'))+'&section=new&summary='+encodeURIComponent(summary)+'&text='+encodeURIComponent(template)+'&format=json&token='+encodeURIComponent(wikiLove.editToken),
          dataType: 'json',
          type: 'POST',
          success: function( data ) {
            if ( data.edit.result == "Success" ) {
              window.location.reload();
            } else {
              wikiLove.displayError( form, 'API返回结果未知' );
            }
          },
          error: function( xhr ) {
            wikiLove.displayError( form, '编辑错误' );
            //console.debug( xhr.responseText );
          }
        });
      },
 
      sendEmail: function( form, subject, text ) {
        $.ajax({
          url: wgScriptPath + '/api.php?',
          data: 'action=emailuser&target='+encodeURIComponent(wgTitle)+'&subject='+encodeURIComponent(subject)+'&text='+encodeURIComponent(text)+'&format=json&token='+encodeURIComponent(wikiLove.editToken),
          dataType: 'json',
          type: 'POST'
        });
      },
 
      launchDialog: function( interface, emailOption ) {
        // If user is emailable, add email checkbox to forms that request it
        if ( emailOption && typeof wikiLove.emailable !== 'undefined' && wikiLove.emailable ) {
          if ( typeof interface.data('emailCheckboxAppended') == 'undefined' ) {
            interface.append( $('<div style="margin-top:0.4em;"></div>').html('<input type="checkbox" name="notify"/> 同时通过Email通知') );
            interface.data('emailCheckboxAppended', true);
          }
        }
        // Open the dialog box
        interface.dialog('open');
        // Retrieve the edit token so it will be available when the form is submitted
        wikiLove.getEditToken();
      },
 
      initialize: function() {
 
        // Function to make a list of images selectable
        $.fn.selectItem = function() {
          return this.each(function() {
            var containerId = this.id;
            var customId = 0;
            $('#'+containerId+' img').css('border', '8px solid transparent');
            $('#'+containerId+' img').each(function() {
              if (this.id == '') {
                this.id = 'select_img_' + customId;
                customId++;
              }
              $('#'+this.id).click(function() {
                  $('#'+containerId+'Selected').val(this.title);
                  $('#'+containerId+' img').css('border', '8px solid transparent');
                  $('#'+this.id).css('border', '8px solid #AED0EA');
              })
            });
          });
        };
 
        // List of all available barnstars
        var $barnstarList = [
          '原星章',
          '编辑星章',
          '机器人星章',
          '小星章',
          '超现实星章',
          '摄影师星章',
          '作家星章',
          '绘图師星章',
          '音频星章',
          '译者星章',
          '校对者星章',
          '特別星章',
          '清理星章',
          '参考星章',
          '友善星章',
          '勤奋星章',
          '真幽默星章',
          '用户页星章',
          '不倦星章',
          '隐形星章',
          '好主意星章',
          '和平星章',
          '帮手星章',
          '管理员星章',
          '共享资源大使星章',
          '反破坏星章',
          '达芬奇星章',
          '重定向星章',
          '內部链接星章',
          '时事星章',
          'LGBT星章',
          '模板星章'		  
        ];
 
        // Define barnstar interface
        $wlBarnstars = $('<div id="barnstarForm" style="position:relative;"></div>')
          .append( $('<div style="margin-top:0.4em;"></div>').html( '选择星章: ' ).append( $('<select id="barnstarSelect" style="padding:1px;vertical-align:baseline;"></select>') ) )
          .append( $('<div style="margin-top:0.4em;"></div>').html( '留言内容(不带签名)<br/>' ).append( $('<textarea name="message" id="barnstarMessage" style="width:99%" rows="4" cols="60"></textarea>') ) )
          .dialog({
            width: 500,
            autoOpen: false,
            title: '赠送星章',
            modal: true,
            buttons: { "赠送星章": function() { 
              $(this).dialog({buttons:{}});
              if ( typeof wikiLove.editToken !== 'undefined' ) {
                if ( $('#barnstarForm input[name="notify"]').attr('checked') ) {
                  // Send email
                  wikiLove.sendEmail( 'barnstarForm', '维基百科星章', '你好 '+wgTitle+'!\n\n我刚刚送给你一枚'+$('#barnstarSelect').val()+' 。' );
                }
                // Perform edit to page
                wikiLove.displayProgress( 'barnstarForm', '处理中,请稍候' );
                var template = '{{subst:'+$('#barnstarSelect').val()+'|'+$('#barnstarMessage').val() +' ~~~~}}';
                wikiLove.editPage( 'barnstarForm', '星章', template );
              } else {
                wikiLove.displayError( 'barnstarForm', '发生错误' );
              }
            }}
          });
 
        // Populate barnstar select list
        $.each( $barnstarList, function(index, value) { $('#barnstarSelect').append( $('<option></option>').val(value).html(value) ); });
 
        // Insert new tab into page
        var menuBarnstar = '<li id="wl-barnstar"><span><a onclick="wikiLove.launchDialog( $wlBarnstars, true ); return false;" href="#">星章</a></span></li>';
        var wikiLoveMenu = '<div class="vectorMenu" id="p-wikilove"><h5><span>WikiLove</span><a href="#" style="padding:0 0.4em;width:auto;"><span style="display:inline-block;height:2.5em;width:16px;margin-right:14px;background-position:0 60%;background-image:url(http://upload.wikimedia.org/wikipedia/commons/b/bd/Silk_heart_icon.png);background-repeat:no-repeat;">&nbsp;</span></a></h5><div class="menu"><ul>'+menuBarnstar+'</ul></div></div>';
        $('#p-cactions').after(wikiLoveMenu);
 
      } // close initialize function
 
    } // close wikiLove object
    wikiLove.initialize();
    wikiLove.getEmailable();
  }) // close mw.loader
} // close if
//</nowiki>