跳转到内容

User:AnYiLin/js/PurgePageCache.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
 * Copyright (c) 2020-present, 安忆.
 *
 * @author 安忆 [[zh:U:安忆]]
 * @file PurgePageCache.js
 * @license GPL v3
 */
$(function purgePageCache() {
  if (RLCONF.wgAction === 'view' && RLCONF.wgIsArticle && RLCONF.wgCurRevisionId !== 0 && RLCONF.wgRevisionId !== 0 && RLCONF.wgCurRevisionId === RLCONF.wgRevisionId) {
    mw.loader.using(['ext.gadget.ding', 'ext.gadget.site-lib', 'mediawiki.api', 'mediawiki.util']).then(function () {
      var isMinerva = document.body.classList.contains('skin-minerva');
      var pos = 'p-cactions';
      if (isMinerva) {
        pos = 'p-tb';
        var style = document.createElement('style');
        style.id = 'css-purgepagecache';
        style.appendChild(document.createTextNode('.mw-ui-icon-portletlink-ca-purge:before{background:url("data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%2220%22 height=%2220%22 viewBox=%220 0 20 20%22%3E%3Cg fill=%22%2354595d%22%3E%3Cpath d=%22M7 0a2 2 0 00-2 2h9a2 2 0 012 2v12a2 2 0 002-2V2a2 2 0 00-2-2z%22/%3E%3Cpath d=%22M13 20H4a2 2 0 01-2-2V5a2 2 0 012-2h9a2 2 0 012 2v13a2 2 0 01-2 2zm-6.5-3.5l.41-1.09L8 15l-1.09-.41-.41-1.09-.41 1.09L5 15l1.09.41.41 1.09zm2.982-.949l.952-2.561 2.53-.964-2.53-.964L9.482 8.5l-.952 2.562-2.53.964 2.53.964.952 2.561zM6 10.5l.547-1.453L8 8.5l-1.453-.547L6 6.5l-.547 1.453L4 8.5l1.453.547L6 10.5z%22/%3E%3C/g%3E%3C/svg%3E")}'));
        document.head.appendChild(style);
      }
      var purgePageCacheMain = function purgePageCacheMain(event, title) {
        event.preventDefault();
        bldkDingExposedInterface('正在清除…', 'default', 'long');
        new mw.Api().post({
          action: 'purge',
          format: 'json',
          formatversion: '2',
          forcelinkupdate: true,
          titles: title
        }).then(function () {
          localStorage.removeItem('MediaWikiModuleStore:' + mw.config.get('wgWikiID'));
          location.reload();
        })['catch'](function () {
          bldkDingExposedInterface(wgULS('清除失败,请重试', '清除失敗,請重試'), 'warning', 'long');
        });
      };
      var _element = mw.util.addPortletLink(pos, '#', wgULS('清除缓存', '清除快取'), 'ca-purge', wgULS('更新服务器缓存', '更新伺服器快取'));
      if (_element) {
        var element = _element.querySelector('a');
        if (element) {
          element.addEventListener('click', function (event) {
            purgePageCacheMain(event, mw.config.get('wgPageName'));
          });
        }
      }
      Array.prototype.forEach.call(document.querySelectorAll('a[href*="action=purge"]'), function (_element) {
        var params = new URL(_element.href).searchParams;
        var title = mw.config.get('wgPageName') || params.get('title');
        _element.addEventListener('click', function (event) {
          purgePageCacheMain(event, title);
        });
      });
    });
  }
});