MediaWiki:Gadget-DiffLink.js
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
- Opera:按 Ctrl-F5。
/**
* SPDX-License-Identifier: GPL-3.0-or-later
* _addText: '{{Gadget Header|license=GPL-3.0-or-later}}'
*
* @source {@link https://git.qiuwen.net.cn/InterfaceAdmin/QiuwenGadgets/src/branch/master/src/DiffLink}
* @author 安忆 <i@anyi.in>
* @license GPL-3.0-or-later {@link https://www.qiuwenbaike.cn/wiki/H:GPL-3.0}
*/
/**
* Copyright (C) 安忆
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* +------------------------------------------------------------+
* | === WARNING: GLOBAL GADGET FILE === |
* +------------------------------------------------------------+
* | All changes should be made in the repository, |
* | otherwise they will be lost. |
* +------------------------------------------------------------+
* | Changes to this page may affect many users. |
* | Please discuss changes by opening an issue before editing. |
* +------------------------------------------------------------+
*/
/* <nowiki> */
(() => {
"use strict";
// dist/DiffLink/DiffLink.js
function asyncGeneratorStep(n, t, e, r, o, a, c) {
try {
var i = n[a](c), u = i.value;
} catch (n2) {
return void e(n2);
}
i.done ? t(u) : Promise.resolve(u).then(r, o);
}
function _asyncToGenerator(n) {
return function() {
var t = this, e = arguments;
return new Promise(function(r, o) {
var a = n.apply(t, e);
function _next(n2) {
asyncGeneratorStep(a, r, o, _next, _throw, "next", n2);
}
function _throw(n2) {
asyncGeneratorStep(a, r, o, _next, _throw, "throw", n2);
}
_next(void 0);
});
};
}
//! src/DiffLink/modules/util/checkDiffPage.ts
var checkDiffPage = ($content) => {
const {
wgCurRevisionId
} = mw.config.get();
return $content.find("table").hasClass("diff") && !!wgCurRevisionId;
};
//! src/DiffLink/modules/util/checkRevisionPage.ts
var checkRevisionPage = ($content) => {
return ($content.find("#contentSub #mw-revision-nav").length || $content.find("main#content>.pre-content #mw-revision-nav").length) > 0;
};
//! src/DiffLink/modules/addPortletLink.ts
var import_ext_gadget2 = require("ext.gadget.Util");
//! src/DiffLink/modules/i18n.ts
var import_ext_gadget = require("ext.gadget.i18n");
var getI18nMessages = () => {
return {
"Network error": (0, import_ext_gadget.localize)({
ja: "ネットワークエラー",
"zh-hans": "网络异常",
"zh-hant": "網路異常"
}),
CopyDiff: (0, import_ext_gadget.localize)({
en: "Copy the link to the diff version (wiki syntax)",
ja: "現在の版間の差分リンクをコピーします(ウィキ記法)",
"zh-hans": "复制链接到当前差异版本的维基语法",
"zh-hant": "複製連結到當前差異版本的維基語法"
}),
CopyPermanent: (0, import_ext_gadget.localize)({
en: "Copy the permanent link to the current version (wiki syntax)",
ja: "このページのこの版への固定リンクをコピーします(ウィキ記法)",
"zh-hans": "复制链接到当前修订版本的维基语法",
"zh-hant": "複製連結到當前修訂版本的維基語法"
}),
Diff: (0, import_ext_gadget.localize)({
en: "Diff link",
ja: "現在の版間の差分リンク",
"zh-hans": "当前差异链接",
"zh-hant": "當前差異連結"
}),
DiffVersion: (0, import_ext_gadget.localize)({
en: "diff version",
ja: "版間の差分",
"zh-hans": "差异版本",
"zh-hant": "差異版本"
}),
Permanent: (0, import_ext_gadget.localize)({
en: "Permanent link",
ja: "この版への固定リンク",
"zh-hans": "当前修订链接",
"zh-hant": "當前修訂連結"
}),
PermanentVersion: (0, import_ext_gadget.localize)({
en: "permanent version",
ja: "版への固定リンク",
"zh-hans": "固定版本链接",
"zh-hant": "固定版本連結"
})
};
};
var i18nMessages = getI18nMessages();
var getMessage = (key) => {
return i18nMessages[key] || key;
};
//! src/DiffLink/modules/addPortletLink.ts
/*!
* 若想自定义复制结果中的文本可通过向自己的common.js中加入以下内容:
* window.DiffLink = ['版本差异', '固定版本'];
* 若想使用默认值1,但自定值2,请将值1留空,如下例:
* window.DiffLink = ['', '固定版本'];
*/
var defaultTextArray = [getMessage("DiffVersion"), getMessage("PermanentVersion")];
if (window.DiffLink && Object.prototype.toString.call(window.DiffLink) === "[object Array]") {
const textArray = window.DiffLink;
if (textArray[0] && Object.prototype.toString.call(textArray[0]) === "[object String]") {
[defaultTextArray[0]] = textArray;
}
if (textArray[1] && Object.prototype.toString.call(textArray[1]) === "[object String]") {
[defaultTextArray[1]] = textArray;
}
}
var eventListener = {
remove: () => {
}
};
var addPortletLink = ({
text,
tooltip,
link,
defaultTextArrayIndex,
isPermaLink
}) => {
var _element$firstElement;
let element = document.querySelector("#t-difflink");
if (!element) {
const portletId = document.querySelector("#p-cactions") ? "p-cactions" : "p-tb";
element = mw.util.addPortletLink(portletId, "#", text, "t-difflink", tooltip);
}
if (!element) {
return;
}
const clickListener = (event) => {
event.preventDefault();
const $element = $("<div>");
const hash = isPermaLink ? decodeURIComponent(location.hash) : "";
for (var _i = 0, _arr = [link, "[[".concat(link).concat(hash, "]]"), "[[".concat(link).concat(hash, "|").concat(defaultTextArray[defaultTextArrayIndex], "]]")]; _i < _arr.length; _i++) {
const value = _arr[_i];
$element.append(
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-explicit-any
new mw.widgets.CopyTextLayout({
align: "top",
copyText: value
}).$element
);
}
void OO.ui.alert($element, {
size: "medium"
});
};
eventListener.remove();
eventListener = (0, import_ext_gadget2.addEventListenerWithRemover)({
target: (_element$firstElement = element.firstElementChild) !== null && _element$firstElement !== void 0 ? _element$firstElement : element,
type: "click",
listener: clickListener
});
};
//! src/DiffLink/modules/buildLink.ts
var buildLink = (oldId, diffId) => {
let link = "Special:Diff/";
if (oldId) {
link += "".concat(oldId, "/");
}
link += diffId;
addPortletLink({
link,
text: getMessage("Diff"),
tooltip: getMessage("CopyDiff"),
defaultTextArrayIndex: 0
});
};
//! src/DiffLink/options.json
var version = "2.0";
//! src/DiffLink/modules/api.ts
var import_ext_gadget3 = require("ext.gadget.Util");
var api = (0, import_ext_gadget3.initMwApi)("DiffLink/".concat(version));
//! src/DiffLink/modules/util/queryApi.ts
var import_ext_gadget4 = require("ext.gadget.Toastify");
var parameters = {
action: "compare",
format: "json",
formatversion: "2",
prop: "ids",
torelative: "prev"
};
var queryApi = /* @__PURE__ */ function() {
var _ref = _asyncToGenerator(function* (diffId) {
try {
return yield api.get({
...parameters,
fromrev: diffId
});
} catch (error) {
console.error("[DiffLink] Ajax error:", error);
(0, import_ext_gadget4.toastify)({
text: getMessage("Network error"),
close: true,
duration: -1
}, "error");
return {};
}
});
return function queryApi2(_x) {
return _ref.apply(this, arguments);
};
}();
//! src/DiffLink/modules/compareWithRemoteDiffId.ts
var compareWithRemoteDiffId = /* @__PURE__ */ function() {
var _ref2 = _asyncToGenerator(function* (diffId) {
var _response$compare;
const response = yield queryApi(diffId);
const {
wgDiffNewId,
wgDiffOldId
} = mw.config.get();
if (diffId === wgDiffNewId && ((_response$compare = response["compare"]) === null || _response$compare === void 0 ? void 0 : _response$compare.fromrevid) === wgDiffOldId) {
buildLink(0, diffId);
}
});
return function compareWithRemoteDiffId2(_x2) {
return _ref2.apply(this, arguments);
};
}();
//! src/DiffLink/modules/processId.ts
var processId = (isRevisionPage, {
diffId,
oldId,
revisionId
}) => {
if (diffId) {
buildLink(oldId, diffId);
if (oldId) {
void compareWithRemoteDiffId(diffId);
}
} else if (revisionId && isRevisionPage) {
addPortletLink({
text: getMessage("Permanent"),
tooltip: getMessage("CopyPermanent"),
link: "Special:PermaLink/".concat(revisionId),
defaultTextArrayIndex: 1,
isPermaLink: true
});
}
};
//! src/DiffLink/DiffLink.ts
mw.hook("wikipage.content").add(function diffLink($content) {
if ($content.attr("id") !== "mw-content-text") {
return;
}
const isDiffPage = checkDiffPage($content);
const isRevisionPage = checkRevisionPage($content);
if (!isDiffPage && !isRevisionPage) {
return;
}
const {
wgDiffNewId,
wgDiffOldId,
wgRevisionId
} = mw.config.get();
processId(isRevisionPage, {
diffId: wgDiffNewId !== null && wgDiffNewId !== void 0 ? wgDiffNewId : null,
oldId: wgDiffOldId !== null && wgDiffOldId !== void 0 ? wgDiffOldId : null,
revisionId: wgRevisionId !== null && wgRevisionId !== void 0 ? wgRevisionId : null
});
});
})();