MediaWiki:Gadget-CopyCodeBlock.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: CC-BY-SA-4.0
* _addText: '{{Gadget Header|license=CC-BY-SA-4.0}}'
*
* @base {@link https://en.wikipedia.org/wiki/User:Nardog/CopyCodeBlock.js}
* @source {@link https://git.qiuwen.net.cn/InterfaceAdmin/QiuwenGadgets/src/branch/master/src/CopyCodeBlock}
* @license CC-BY-SA-4.0 {@link https://www.qiuwenbaike.cn/wiki/H:CC-BY-SA-4.0}
*/
/**
* +------------------------------------------------------------+
* | === 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/CopyCodeBlock/CopyCodeBlock.js
function _createForOfIteratorHelper(r, e) {
var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
if (!t) {
if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) {
t && (r = t);
var n = 0, F = function() {
};
return { s: F, n: function() {
return n >= r.length ? { done: true } : { done: false, value: r[n++] };
}, e: function(r2) {
throw r2;
}, f: F };
}
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var o, a = true, u = false;
return { s: function() {
t = t.call(r);
}, n: function() {
var r2 = t.next();
return a = r2.done, r2;
}, e: function(r2) {
u = true, o = r2;
}, f: function() {
try {
a || null == t.return || t.return();
} finally {
if (u) throw o;
}
} };
}
function _unsupportedIterableToArray(r, a) {
if (r) {
if ("string" == typeof r) return _arrayLikeToArray(r, a);
var t = {}.toString.call(r).slice(8, -1);
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
}
}
function _arrayLikeToArray(r, a) {
(null == a || a > r.length) && (a = r.length);
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
return n;
}
//! src/CopyCodeBlock/modules/CopyCodeBlock.module.less
var button = "CopyCodeBlock-module__button_74i1Da";
var codeBlock = "CopyCodeBlock-module__codeBlock_74i1Da";
//! src/CopyCodeBlock/modules/addCopyListener.ts
var import_ext_gadget2 = require("ext.gadget.Clipboard");
//! src/CopyCodeBlock/modules/i18n.ts
var import_ext_gadget = require("ext.gadget.i18n");
var getI18nMessages = () => {
return {
Copy: (0, import_ext_gadget.localize)({
en: "Copy to clipboard",
ja: "クリップボードにコピー",
"zh-hans": "复制至剪贴板",
"zh-hant": "拷貝至剪貼簿"
}),
Copied: (0, import_ext_gadget.localize)({
en: "Copied",
ja: "コピーが成功しました",
"zh-hans": "已复制",
"zh-hant": "已拷貝"
}),
Failed: (0, import_ext_gadget.localize)({
en: "Copy failed",
ja: "コピーに失敗しました",
"zh-hans": "复制失败",
"zh-hant": "拷貝失敗"
})
};
};
var i18nMessages = getI18nMessages();
var getMessage = (key) => {
return i18nMessages[key] || key;
};
//! src/CopyCodeBlock/modules/addCopyListener.ts
var import_ext_gadget3 = require("ext.gadget.Toastify");
var addCopyListener = ($pres) => {
var _iterator = _createForOfIteratorHelper($pres), _step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done; ) {
const pre = _step.value;
const clipboard = new import_ext_gadget2.Clipboard(pre.querySelector(".".concat(button)), {
text() {
var _pre$textContent;
return (_pre$textContent = pre.textContent) !== null && _pre$textContent !== void 0 ? _pre$textContent : "";
}
});
clipboard.on("success", () => {
(0, import_ext_gadget3.toastify)({
text: getMessage("Copied")
}, "success");
});
clipboard.on("error", () => {
(0, import_ext_gadget3.toastify)({
text: getMessage("Failed")
}, "error");
});
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
};
//! src/CopyCodeBlock/modules/addButton.ts
var import_ext_gadget4 = require("ext.gadget.Tippy");
var addButton = ($pres) => {
const copyButton = new OO.ui.ButtonWidget({
classes: [button],
framed: false,
icon: "copy"
});
const $copyButton = copyButton.$element;
$copyButton.attr("aria-label", getMessage("Copy"));
$pres.addClass(codeBlock).append($copyButton);
(0, import_ext_gadget4.tippy)($copyButton.get(0), {
arrow: true,
content: $copyButton.attr("aria-label"),
placement: "bottom"
});
addCopyListener($pres);
};
//! src/CopyCodeBlock/CopyCodeBlock.ts
mw.hook("wikipage.content").add(function copyCodeBlock($content) {
const $pres = $content.find("pre");
if (!$pres.length) {
return;
}
addButton($pres);
});
})();