「MediaWiki:Common.js」の版間の差分

提供: 小樽のじかん事典
編集の要約なし
編集の要約なし
1行目: 1行目:
/* ここにあるすべてのJavaScriptは、すべてのページ読み込みですべての利用者に対して読み込まれます */
/* 全ページで読み込むJavaScript:コメント欄を下部に移動し、投稿者名入力を反映 */
mw.loader.using('mediawiki.util', function () {
mw.loader.using('mediawiki.util', function () {


/* ===== 表示名入力欄を追加 ===== */
    /* ===== 表示名入力欄を追加 ===== */
function addNameField() {
    function addNameField() {
const form = document.querySelector('.comment-form');
        const form = document.querySelector('.comment-form');
if (!form || form.querySelector('.comment-name-input')) return;
        if (!form || form.querySelector('.comment-name-input')) return;


const wrapper = document.createElement('div');
        const wrapper = document.createElement('div');
wrapper.className = 'comment-name-input';
        wrapper.className = 'comment-name-input';


const input = document.createElement('input');
        const input = document.createElement('input');
input.type = 'text';
        input.type = 'text';
input.placeholder = '投稿者名(省略可)';
        input.placeholder = '投稿者名(省略可)';
input.style.width = '200px';
        input.style.width = '200px';


wrapper.appendChild(input);
        wrapper.appendChild(input);
form.prepend(wrapper);
        form.prepend(wrapper);
}
    }


/* ===== 送信時:本文に名前を埋め込む ===== */
    /* ===== 送信時:本文に名前を埋め込む ===== */
function hookSubmit() {
    function hookSubmit() {
const form = document.querySelector('.comment-form');
        const form = document.querySelector('.comment-form');
if (!form) return;
        if (!form) return;


const textarea = form.querySelector('textarea');
        const textarea = form.querySelector('textarea');
const input = form.querySelector('.comment-name-input input');
        const input = form.querySelector('.comment-name-input input');
const button = form.querySelector('button[type="submit"]');
        const button = form.querySelector('button[type="submit"]');


if (!textarea || !input || !button) return;
        if (!textarea || !input || !button) return;


button.addEventListener('click', function () {
        button.addEventListener('click', function () {
let name = input.value.trim();
            let name = input.value.trim();
if (!name) name = '名無しさん';
            if (!name) name = '名無しさん';


// すでに埋め込み済みなら何もしない
            if (!textarea.value.startsWith('【')) {
if (!textarea.value.startsWith('【')) {
                textarea.value = `【${name}】` + textarea.value;
textarea.value = `【${name}】` + textarea.value;
            }
}
        });
});
    }
}


/* ===== 表示時:名前を取り出して表示 ===== */
    /* ===== 表示時:名前を取り出して表示 ===== */
function applyDisplayNames() {
    function applyDisplayNames() {
document.querySelectorAll('.comment').forEach(function (comment) {
        document.querySelectorAll('.comment').forEach(function (comment) {


if (comment.querySelector('.comment-display-name')) return;
            if (comment.querySelector('.comment-display-name')) return;


const body = comment.querySelector('.comment-body, .comment-text');
            const body = comment.querySelector('.comment-body, .comment-text');
if (!body) return;
            if (!body) return;


const text = body.innerHTML;
            const text = body.innerHTML;
const match = text.match(/^【(.+?)】/);
            const match = text.match(/^【(.+?)】/);


let name = '名無しさん';
            let name = '名無しさん';


if (match) {
            if (match) {
name = match[1];
                name = match[1];
body.innerHTML = body.innerHTML.replace(/^【.+?】/, '');
                body.innerHTML = body.innerHTML.replace(/^【.+?】/, '');
}
            }


const header = comment.querySelector('.comment-header') || comment;
            const header = comment.querySelector('.comment-header') || comment;
const nameElem = document.createElement('div');
            const nameElem = document.createElement('div');
nameElem.className = 'comment-display-name';
            nameElem.className = 'comment-display-name';
nameElem.textContent = name;
            nameElem.textContent = name;


header.prepend(nameElem);
            header.prepend(nameElem);
});
        });
}
    }


/* ===== 実行 ===== */
    /* ===== コメント追加後も再適用 ===== */
addNameField();
    document.addEventListener('click', function () {
hookSubmit();
        setTimeout(applyDisplayNames, 500);
applyDisplayNames();
    });


/* コメント追加後にも再適用 */
    /* ===== 初期実行 ===== */
document.addEventListener('click', function () {
    addNameField();
setTimeout(applyDisplayNames, 500);
    hookSubmit();
});
    applyDisplayNames();
});
 
/* ===== コメント欄をページ下部に移動 ===== */
document.addEventListener('DOMContentLoaded', function () {
    const commentsContainer = document.querySelector('#mw-comments-container');
    if (commentsContainer) {
        document.body.appendChild(commentsContainer);
    }
});
});

2025年12月26日 (金) 03:50時点における版

/* 全ページで読み込む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);
        });
    }

    /* ===== コメント追加後も再適用 ===== */
    document.addEventListener('click', function () {
        setTimeout(applyDisplayNames, 500);
    });

    /* ===== 初期実行 ===== */
    addNameField();
    hookSubmit();
    applyDisplayNames();
});

/* ===== コメント欄をページ下部に移動 ===== */
document.addEventListener('DOMContentLoaded', function () {
    const commentsContainer = document.querySelector('#mw-comments-container');
    if (commentsContainer) {
        document.body.appendChild(commentsContainer);
    }
});