利用者:DrakoBot/botreq 削除依頼ログ.js
表示
お知らせ: 保存した後、ブラウザのキャッシュをクリアしてページを再読み込みする必要があります。
多くの Windows や Linux のブラウザ
- Ctrl を押しながら F5 を押す。
Mac における Safari
Mac における Chrome や Firefox
- ⌘ Cmd と ⇧ Shift を押しながら R を押す。
詳細についてはWikipedia:キャッシュを消すをご覧ください。
//<nowiki>
$.when(
mw.loader.using('mediawiki.api'),
$.ready
).then(() => {
if (mw.config.get('wgAction') !== 'edit') return;
const /** @type {HTMLInputElement?} */ wpSummary = document.querySelector('#wpSummary');
const /** @type {HTMLInputElement?} */ wpMinoredit = document.querySelector('#wpMinoredit');
const /** @type {HTMLInputElement?} */ wpWatchthis = document.querySelector('#wpWatchthis');
if (wpSummary && wpMinoredit && wpWatchthis) {
if (mw.config.get('wgPageName') !== '利用者:DrakoBot/botreq_削除依頼ログ') {
wpSummary.value += '削除済みノートの修正 ([[Special:PermaLink/96388726#削除済みノートをテンプレートへ置換|BOTREQ]])';
wpMinoredit.checked = true;
wpWatchthis.checked = false;
} else {
wpSummary.value += 'update';
wpMinoredit.checked = false;
wpWatchthis.checked = true;
}
mw.notify('編集フィールドの値を自動設定しました。');
}
const api = new mw.Api();
/**
* @param {string[]} pagetitles
* @returns {Promise<Object.<string, string>>} pagetitles as keys and 1st revision timestamps as values
*/
const getCreationDates = async (pagetitles) => {
/**
* @param {string} title
* @returns {JQueryPromise<object>} API response or empty object
*/
const req = (title) => {
return api.get({
action: 'query',
titles: title,
prop: 'revisions',
rvprop: 'timestamp',
rvlimit: 1,
rvdir: 'newer',
formatversion: '2'
}).then((res) => {
return res || {};
}).catch((code, err) => {
console.log(err);
return {};
});
};
const deferreds = [];
pagetitles.forEach((t) => {
deferreds.push(req(t));
});
const result = await Promise.all(deferreds);
return result.reduce((acc, res, i) => {
let resPg;
if (res && res.query && (resPg = res.query.pages) && resPg[0]) {
const resRv = resPg[0].revisions;
let ts = resRv && resRv[0] && resRv[0].timestamp || '';
let m;
if (ts && (m = ts.match(/^(\d{4})-(\d{2})-(\d{2})T/))) {
ts = `${m[1]}年${m[2].replace(/^0/, '')}月${m[3].replace(/^0/, '')}日`;
}
acc[pagetitles[i]] = ts;
} else {
acc[pagetitles[i]] = '';
}
return acc;
}, Object.create(null));
};
/**
* @returns {string[]?}
*/
const getTitlesFromSelection = () => {
const sel = window.getSelection();
if (!sel) return null;
const text = sel.toString();
const regex = /\[\[(.+?)(?:\|.*?)?\]\]/g;
const ret = [];
let m;
while ((m = regex.exec(text))) { // Parse wikilinks
ret.push(m[1]);
}
return ret.length ? ret : null;
};
const editor = document.querySelector('#editform');
if (editor && editor.parentElement) {
const div = document.createElement('div');
const output = document.createElement('div');
output.style.height = '10em';
output.style.overflowY = 'scroll';
output.style.border = '1px solid silver';
div.appendChild(output);
const p = document.createElement('p');
output.appendChild(p);
const btn = document.createElement('input');
btn.type = 'button';
btn.value = 'Query page existence';
btn.addEventListener('click', async function(e) {
const titles = getTitlesFromSelection();
if (!titles) {
mw.notify('Failed to parse pagetitles from the selection.');
return;
}
this.disabled = true;
const dates = await getCreationDates(titles);
p.innerHTML = JSON.stringify(dates, null, 4).replace(/^[\s{]*|[\s}]*$/g, '').replace(/\n/g, '<br/>');
this.disabled = false;
});
div.appendChild(btn);
editor.parentElement.insertBefore(div, editor);
}
});
//</nowiki>