「MediaWiki:Common.js」の版間の差分
提供: 小樽のじかん事典
ページの作成:「→ここにあるすべてのJavaScriptは、すべてのページ読み込みですべての利用者に対して読み込まれます: if (!name) name = '名無しさん';」 |
編集の要約なし |
||
| 1行目: | 1行目: | ||
/* ここにあるすべてのJavaScriptは、すべてのページ読み込みですべての利用者に対して読み込まれます */ | /* ここにあるすべてのJavaScriptは、すべてのページ読み込みですべての利用者に対して読み込まれます */ | ||
if (!name) name = '名無しさん'; | mw.loader.using('mediawiki.util', function () { | ||
/* ===== 表示名入力欄を追加 ===== */ | |||
function addNameField() { | |||
const form = document.querySelector('.comment-form'); | |||
if (!form || form.querySelector('.comment-name-input')) return; | |||
const wrapper = document.createElement('div'); | |||
wrapper.className = 'comment-name-input'; | |||
const input = document.createElement('input'); | |||
input.type = 'text'; | |||
input.placeholder = '投稿者名(省略可)'; | |||
input.style.width = '200px'; | |||
wrapper.appendChild(input); | |||
form.prepend(wrapper); | |||
} | |||
/* ===== 送信時:本文に名前を埋め込む ===== */ | |||
function hookSubmit() { | |||
const form = document.querySelector('.comment-form'); | |||
if (!form) return; | |||
const textarea = form.querySelector('textarea'); | |||
const input = form.querySelector('.comment-name-input input'); | |||
const button = form.querySelector('button[type="submit"]'); | |||
if (!textarea || !input || !button) return; | |||
button.addEventListener('click', function () { | |||
let name = input.value.trim(); | |||
if (!name) name = '名無しさん'; | |||
// すでに埋め込み済みなら何もしない | |||
if (!textarea.value.startsWith('【')) { | |||
textarea.value = `【${name}】` + textarea.value; | |||
} | |||
}); | |||
} | |||
/* ===== 表示時:名前を取り出して表示 ===== */ | |||
function applyDisplayNames() { | |||
document.querySelectorAll('.comment').forEach(function (comment) { | |||
if (comment.querySelector('.comment-display-name')) return; | |||
const body = comment.querySelector('.comment-body, .comment-text'); | |||
if (!body) return; | |||
const text = body.innerHTML; | |||
const match = text.match(/^【(.+?)】/); | |||
let name = '名無しさん'; | |||
if (match) { | |||
name = match[1]; | |||
body.innerHTML = body.innerHTML.replace(/^【.+?】/, ''); | |||
} | |||
const header = comment.querySelector('.comment-header') || comment; | |||
const nameElem = document.createElement('div'); | |||
nameElem.className = 'comment-display-name'; | |||
nameElem.textContent = name; | |||
header.prepend(nameElem); | |||
}); | |||
} | |||
/* ===== 実行 ===== */ | |||
addNameField(); | |||
hookSubmit(); | |||
applyDisplayNames(); | |||
/* コメント追加後にも再適用 */ | |||
document.addEventListener('click', function () { | |||
setTimeout(applyDisplayNames, 500); | |||
}); | |||
}); | |||
2025年12月26日 (金) 02:54時点における版
/* ここにあるすべてのJavaScriptは、すべてのページ読み込みですべての利用者に対して読み込まれます */
mw.loader.using('mediawiki.util', function () {
/* ===== 表示名入力欄を追加 ===== */
function addNameField() {
const form = document.querySelector('.comment-form');
if (!form || form.querySelector('.comment-name-input')) return;
const wrapper = document.createElement('div');
wrapper.className = 'comment-name-input';
const input = document.createElement('input');
input.type = 'text';
input.placeholder = '投稿者名(省略可)';
input.style.width = '200px';
wrapper.appendChild(input);
form.prepend(wrapper);
}
/* ===== 送信時:本文に名前を埋め込む ===== */
function hookSubmit() {
const form = document.querySelector('.comment-form');
if (!form) return;
const textarea = form.querySelector('textarea');
const input = form.querySelector('.comment-name-input input');
const button = form.querySelector('button[type="submit"]');
if (!textarea || !input || !button) return;
button.addEventListener('click', function () {
let name = input.value.trim();
if (!name) name = '名無しさん';
// すでに埋め込み済みなら何もしない
if (!textarea.value.startsWith('【')) {
textarea.value = `【${name}】` + textarea.value;
}
});
}
/* ===== 表示時:名前を取り出して表示 ===== */
function applyDisplayNames() {
document.querySelectorAll('.comment').forEach(function (comment) {
if (comment.querySelector('.comment-display-name')) return;
const body = comment.querySelector('.comment-body, .comment-text');
if (!body) return;
const text = body.innerHTML;
const match = text.match(/^【(.+?)】/);
let name = '名無しさん';
if (match) {
name = match[1];
body.innerHTML = body.innerHTML.replace(/^【.+?】/, '');
}
const header = comment.querySelector('.comment-header') || comment;
const nameElem = document.createElement('div');
nameElem.className = 'comment-display-name';
nameElem.textContent = name;
header.prepend(nameElem);
});
}
/* ===== 実行 ===== */
addNameField();
hookSubmit();
applyDisplayNames();
/* コメント追加後にも再適用 */
document.addEventListener('click', function () {
setTimeout(applyDisplayNames, 500);
});
});