跳转到内容

MediaWiki:Gadget-shortURL.js

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:Ctrl-F5
/**
 * @name Short URL
 * @author 机智的小鱼君
 *
 * @description Get the "fake" short link provided by MediaWiki.
 *              Solve the very long link of the pages that name contain non-ASCII words.
 */
// Originated from [https://github.com/Wjghj-Project/wjghj-wiki/blob/master/gadgets/Share-btn/script.js here].
!(function () {
  var i18n = {
  	shareDescription: wgULS('您还可以通过此链接分享页面:','您還可以透過此連結分享頁面:'),
  	copy: wgULS('复制','複製'),
  	copied: wgULS('已复制!','已複製!'),
  }
  // 对周报页面进行特别处理
  var isTeahouseWeekly = $('body').hasClass("rootpage-茶馆周报");
  var isLakeus = $('body').hasClass("skin-lakeus");
  if (isTeahouseWeekly) {
    console.log("短链接将尝试添加到茶馆周报页眉。");
  }
  window.shortUrl = window.shortUrl || ''
  // 缓存 mw 变量
  var config = mw.config.get()
  // 判断是否存在文章ID
  if (config.wgArticleId > 0) {
    shortUrl = config.wgServer + '/-/' + config.wgArticleId
    // 插入短链接
    var shortURLParagraph = $('<div>', { class: 'shortUrl-block', style: 'margin: 1em;' }).append(
        $('<span>', { class: 'shortUrl-description' }).append(
          $('<span>', { text: i18n.shareDescription }),
          $('<strong>', { text: shortUrl + ' ' }),
          $('<br>'),
          $('<a>', { text: i18n.copy , href: 'javascript:;' }).click(function () {
            // 创建 input 元素,选中复制,然后销毁
            var $this = $(this),
              surlInput = $('<input>', { id: 'shortUrl-copy', value: shortUrl, style: 'z-index: -1; opacity: 0; position: absolute; left: -200vw;', readonly: 'readonly' })
            $this.append(surlInput)
            surlInput.select()
            document.execCommand('copy')
            surlInput.remove()
            $this.text(i18n.copied)
            setTimeout(function () {
              $this.text(i18n.copy)
            }, 1500)
          })
        )
      )
  
  // A PopupButtonWidget.
  var popupButton = new OO.ui.PopupButtonWidget( {
      icon: 'link',
      flags: isTeahouseWeekly ? ['progressive'] : [],
	  framed: false,
	  label: 'Short URL',
	  invisibleLabel: true,
	  popup: {
        label: 'Short URL',
	    $content: shortURLParagraph,
	    align: 'center'
	  }
    } );
  	// Append the button to the DOM.
    if(isTeahouseWeekly) {
      $( '.weekly-header-id' ).after( " |", popupButton.$element );
    } else if(isLakeus) {
      $( '.mw-indicators' ).append( popupButton.$element );
    } else {
      $( '#ca-view' ).after( popupButton.$element );
    }
  }
  
  // 将短链接替换进文章
  $('.shortUrl').text(shortUrl)
  $('.shortUrl-link').html($('<a>', { href: shortUrl, text: shortUrl }))
})()
Cookie帮助我们提供我们的服务。通过使用我们的服务,您同意我们使用cookie。