User:SunAfterRain/js/blib/inverse.js
< User:SunAfterRain | js
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// <nowiki>
$.when(
$.ready,
mw.loader.using(['mediawiki.api', 'ext.gadget.HanAssist'])
).then((_$, require) => {
if (
!mw.config.get('wgPageName')?.startsWith( 'User:Bluedecklibrary/' )
|| !mw.config.get('wgRevisionId')
|| mw.config.get('wgAction') !== 'view'
) {
return;
}
const libTemplateName = 'user:bluedeck/infr/library.card.js';
const gadgetName = 'blib-inverse.js';
const $buttonAttachPoint = $('.bluedeck-dom-attach-endpoint-blib-inverse-23b613580bdf0518380bb6f762685e1c-604471d83ca088bbb3c58b8d5df312e7');
let $textAreaAttachPoint = $('.bluedeck-dom-attach-endpoint-blib-inverse-d6cdf46ebbf2b58950b91edfcec63587-4eb2060a815398e4165f94f6e7af32df');
if (!$buttonAttachPoint.length) {
return;
} else if (!$textAreaAttachPoint.length) {
const $tmp = $buttonAttachPoint.closest('div.mw-parser-output > div');
if ($tmp.length) {
$textAreaAttachPoint = $('<div>').insertAfter($tmp);
} else {
return;
}
}
const HanAssist = require('ext.gadget.HanAssist');
let $link;
const api = new mw.Api();
const messages = HanAssist.batchConv({
inverseButton: { hans: '取得原始维基源代码', hant: '取得原始維基原始碼' },
pageMissing: { hans: '抱歉,页面不存在,无法取得原始维基源代码。', hant: '抱歉,頁面不存在,無法取得原始維基原始碼。' },
fetching: { hans: '正在加载...', hant: '正在載入...' },
fetched: { hans: '处理完毕', hant: '處理完畢' },
fetchFail: { hans: '取得原始维基源代码失败:$1', hant: '取得原始維基原始碼失敗:$1' },
copy: { hans: '[复制到剪贴簿]', hant: '[複製到剪貼簿]' },
copied: { hans: '已复制到剪贴簿', hant: '已複製到剪貼簿' },
copyFail: { hans: '复制到剪贴簿失败:$1', hant: '複製到剪貼簿失敗:$1' },
textAreaHeader: { hans: '原始维基源代码', hant: '原始維基原始碼' },
});
function notify(type, message, options = {}) {
mw.notify(
message,
{
title: gadgetName,
tag: gadgetName,
autoHide: !['warn', 'error'].includes(type),
type,
...options
}
);
}
function inverseArchiveWikitext(contentText) {
const cardStart = contentText.indexOf(`{{${libTemplateName}`);
if (cardStart > -1) {
const cardEnd = contentText.indexOf('}}', cardStart);
if (cardStart > -1) {
contentText = contentText.slice(0, cardStart) + contentText.slice(cardEnd + 2 /* '}}'.length */);
}
}
return contentText
.split('{{((}}')
.join('{{')
.replace(/\[\[(.*?)(\|.*?)?\]\]/g, (orig, title, text) => {
if (title.startsWith(':')) {
const mTitle = mw.Title.newFromText(title.trim());
if (mTitle?.namespace === 14) {
return `[[${title.trim().slice(1)}${text || ''}]]`;
}
}
return orig;
});
}
function spliText(input) {
return input.split(/\r?\n/g)
.map((line) => [document.createTextNode(line), document.createElement('br')])
.flat()
.slice(0, -1);
}
async function main() {
$link
.off('click')
.prop('disabled', true)
.text(messages.fetching)
.css({
background: 'transparent',
opacity: 0.7
});
try {
const pageData = await api.get({
action: 'query',
revids: [mw.config.get('wgCurRevisionId')],
prop: 'revisions',
rvprop: ['content'],
rvslots: 'main',
formatversion: '2'
});
const page = pageData?.query?.pages?.[0];
const revision = page?.revisions?.[0];
if (!page || page.missing || !revision) {
notify('warn', messages.pageMissing);
return;
}
const inverseText = inverseArchiveWikitext(revision.slots.main.content);
console.log(inverseText);
$link.text(messages.fetched);
let $copy = $();
if (navigator.clipboard) {
$copy = $copy
.add(
$('<span>').text(' ')
)
.add(
$('<a>')
.attr({
href: '#'
})
.text(messages.copy)
.on('click', (ev) => {
ev.preventDefault();
navigator.clipboard.writeText(inverseText)
.then(() => {
notify('success', messages.copied);
})
.catch((err) => {
console.error(err);
notify('error', $('<div>').append(...spliText(mw.format(messages.copyFail, String(error)))));
});
})
);
}
$textAreaAttachPoint
.replaceWith(
$('<div>')
.css('max-height', '400px')
.css('overflow-x', 'scroll')
.append(
$('<span>').text(messages.textAreaHeader),
$copy,
$('<pre>').text(inverseText),
),
$('<hr>')
);
} catch (error) {
console.error(error);
notify('error', $('<div>').append(...spliText(mw.format(messages.fetchFail, String(error)))));
}
}
$link = $('<button>')
.css({
'border-radius': '99em',
'border': '2px solid #fff;',
'background': 'rgba(255,255,255,0.2)',
'padding': '0.1em 0.6em',
'color': '#fff'
})
.text(messages.inverseButton)
.on('click', (ev) => {
ev.preventDefault();
main();
});
$buttonAttachPoint.prop('class', '').empty().append($link);
});
// </nowiki>