Site Notice

hello, world

MediaWiki:Gadget-ShortUrl.js

From Project-EPB Commons
Revision as of 16:52, 12 January 2021 by 机智的小鱼君 (talk | contribs) ([InPageEdit] 没有编辑摘要)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Invoke this: https://common.wjghj.cn/

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/**
 * @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.
 */
!(function () {
  window.shortUrl = window.shortUrl || '';
  // 缓存 mw 变量
  var config = mw.config.get();
  // 判断是否存在文章ID
  if (config.wgArticleId > 0) {
    shortUrl = config.wgServer + '/-/' + config.wgArticleId;
    // 在文章后插入段落
    $('#firstHeading').after(
      $('<div>', { class: 'shortUrl-block' }).append(
        $('<span>', { class: 'shortUrl-description' }).append(
          $('<span>', { text: '您还可以通过此链接分享页面:' }),
          $('<strong>', { text: shortUrl + ' ' }),
          $('<a>', { text: '复制', 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('已复制!');
            setTimeout(function () {
              $this.text('复制');
            }, 1500);
          })
        )
      )
    );
  }
  // 将短链接替换进文章
  $('.shortUrl').text(shortUrl);
  $('.shortUrl-link').html(
    $('<a>', { href: shortUrl, text: shortUrl })
  );
}());