跳至內容

MediaWiki:Common.js:修訂間差異

出自zine-tw.com
BraveYe留言 | 貢獻
建立內容為「// <![CDATA[ (function() { // 仅在包含 inputbox 的页面执行,避免全站性能损耗 var inputBox = document.querySelector('.mw-inputbox'); if (!inputBox) return; var today = new Date(); var day = today.getDate(); var month = (today.getMonth() + 1).toString().padStart(2, '0'); var year = today.getFullYear(); // 精准定位 InputBox 生成的输入框 var targetInput = inputBox.querySelector('input[type="text"]…」的新頁面
標籤行動版編輯 行動版網頁編輯 進階行動版編輯
 
BraveYe留言 | 貢獻
無編輯摘要
標籤行動版編輯 行動版網頁編輯 進階行動版編輯
第1行: 第1行:
// <![CDATA[
// <![CDATA[
(function() {
(function() {
     // 仅在包含 inputbox 的页面执行,避免全站性能损耗
     // 等待 DOM 載入完成
     var inputBox = document.querySelector('.mw-inputbox');
     if (document.readyState === 'loading') {
     if (!inputBox) return;
        document.addEventListener('DOMContentLoaded', init);
     } else {
        init();
    }


     var today = new Date();
     function init() {
    var day = today.getDate();
        // 嘗試多種可能的 container class
    var month = (today.getMonth() + 1).toString().padStart(2, '0');
        var container = document.querySelector('.mw-inputbox') ||
    var year = today.getFullYear();
                        document.querySelector('.wb-inputbox') ||
                        document.querySelector('form[action*="action=edit"] input[type="text"]');
       
        if (!container) return;


    // 精准定位 InputBox 生成的输入框
        // 確保只對 inputbox 有效
    var targetInput = inputBox.querySelector('input[type="text"]');
        if (!container.closest && container.tagName !== 'FORM') return;
    if (!targetInput) return;


    // 仅当用户未手动修改时自动填入
         var input = container.querySelector('input[type="text"]');
    if (!targetInput.dataset.autoFilled) {
         if (!input) return;
         targetInput.value = day + '日';
        targetInput.placeholder = '例如:' + day + '日';
         targetInput.dataset.autoFilled = 'true';


         // 同步更新 prefix 中的年月(防止跨月/跨年失效)
         // 防重複執行
         var prefix = '日誌/' + year + '年/' + month + '月/';
        if (input.dataset.autoFilled) return;
         var form = inputBox.querySelector('form');
 
         if (form && form.action.includes('title=')) {
        var d = new Date();
            form.action = form.action.replace(/日誌\/\d+年\/\d+月\//, prefix);
        var day = d.getDate();
         var mon = ('0' + (d.getMonth() + 1)).slice(-2);
        var yr = d.getFullYear();
 
        // 填入日期
        input.value = day + '日';
        input.placeholder = '例如:' + day + '日';
        input.dataset.autoFilled = 'true';
 
        // 修正 form action 中的年月
         var form = input.closest('form');
         if (form) {
            var action = form.getAttribute('action') || '';
            if (action.includes('日誌/') && !action.includes('日誌/' + yr + '年/' + mon + '月/')) {
                form.setAttribute('action', action.replace(/日誌\/\d{4}年\/\d{2}月\//, '日誌/' + yr + '年/' + mon + '月/'));
            }
         }
         }
     }
     }
})();
})();
// ]]>
// ]]>

2026年9月20日 (日) 01:25的版本

// <![CDATA[
(function() {
    // 等待 DOM 載入完成
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }

    function init() {
        // 嘗試多種可能的 container class
        var container = document.querySelector('.mw-inputbox') || 
                        document.querySelector('.wb-inputbox') ||
                        document.querySelector('form[action*="action=edit"] input[type="text"]');
        
        if (!container) return;

        // 確保只對 inputbox 有效
        if (!container.closest && container.tagName !== 'FORM') return;

        var input = container.querySelector('input[type="text"]');
        if (!input) return;

        // 防重複執行
        if (input.dataset.autoFilled) return;

        var d = new Date();
        var day = d.getDate();
        var mon = ('0' + (d.getMonth() + 1)).slice(-2);
        var yr = d.getFullYear();

        // 填入日期
        input.value = day + '日';
        input.placeholder = '例如:' + day + '日';
        input.dataset.autoFilled = 'true';

        // 修正 form action 中的年月
        var form = input.closest('form');
        if (form) {
            var action = form.getAttribute('action') || '';
            if (action.includes('日誌/') && !action.includes('日誌/' + yr + '年/' + mon + '月/')) {
                form.setAttribute('action', action.replace(/日誌\/\d{4}年\/\d{2}月\//, '日誌/' + yr + '年/' + mon + '月/'));
            }
        }
    }
})();
// ]]>