Bước tới nội dung

Khác biệt giữa bản sửa đổi của “MediaWiki:Common.js”

Từ Từ nguyên Tiếng Việt
Không có tóm lược sửa đổi
Thẻ: Đã bị lùi lại
Không có tóm lược sửa đổi
Thẻ: Lùi lại thủ công
 
Dòng 1: Dòng 1:
// Optimized & Modernized Version (with your requested behaviors preserved)
// Example columns
function excol() {
    var dl = document.getElementsByTagName('dl');
    var maxwidth = 0;
    var cnt = 0;
    for (var i = 0; i < dl.length; i++) {
        cnt = 0;
        var dd = dl[i].getElementsByTagName('dd');
        for (j = 0; j < dd.length; j++) {
            dd[j].style.setProperty('min-width', 'max-content');
            dd[j].style.setProperty('column-span', 'unset');
            if (dd[j].clientWidth + parseFloat(window.getComputedStyle(dd[j]).marginLeft) > dl[i].clientWidth / 2) {
                dd[j].style.setProperty('min-width', 'calc(100% - 1.6em)');
                dd[j].style.setProperty('column-span', 'all');
            }
            if (maxwidth < dd[j].clientWidth && dd[j].style.minWidth != 'calc(100% - 1.6em)') {
                maxwidth = dd[j].clientWidth + parseFloat(window.getComputedStyle(dd[j]).marginLeft) + 5;
            }
        console.log(maxwidth);
        cnt++;
        }
        if (cnt >= 6 && maxwidth * 3 < dl[i].clientWidth) {
            dl[i].style.columns = 3
        } else if (cnt >= 6 && maxwidth * 2 < dl[i].clientWidth && maxwidth * 3 > dl[i].clientWidth) {
            dl[i].style.columns = 2
        } else {
            dl[i].style.columns = 1
        }
    }
}


const optimizeColumns = () => {
// Cognates columns
    // Example columns (excol)
function cogcol() {
     document.querySelectorAll('dl').forEach(dl => {
     var cognates = document.querySelectorAll('.cognates ul');
         let maxWidth = 0;
    for (var c = 0; c < cognates.length; c++) {
         const dds = dl.querySelectorAll('dd');
         var widest = null;
         const containerWidth = dl.clientWidth;
        var widestwidth = 0;
 
         var coglis = cognates[c].querySelectorAll('li');
        dds.forEach(dd => {
         for (var l = 0; l < coglis.length; l++) {
            dd.style.setProperty('min-width', 'max-content');
             if (widest == null) {
            dd.style.setProperty('column-span', 'unset');
                 widestwidth = coglis[l].clientWidth;
 
                 widest = l;
            const ddWidth = dd.clientWidth + parseFloat(getComputedStyle(dd).marginLeft || 0);
             } else if (widestwidth < coglis[l].clientWidth) {
 
                 widestwidth = coglis[l].clientWidth;
             if (ddWidth > containerWidth / 2) {
                widest = l;
                 dd.style.setProperty('min-width', 'calc(100% - 1.6em)');
                 dd.style.setProperty('column-span', 'all');
             } else if (maxWidth < ddWidth) {
                 maxWidth = ddWidth + 5;
             }
             }
         });
         }


         const count = dds.length;
         if (document.querySelector('.cognates .reference-text').clientWidth > (widestwidth + parseFloat(window.getComputedStyle(document.querySelector('.reference-text ol'))['marginLeft'])) * 3 + 10 && cognates[c].querySelectorAll('li').length > 7) {
        if (count >= 6) {
             cognates[c].style.columnCount = 3;
             if (maxWidth * 3 < containerWidth) dl.style.columnCount = 3;
        } else if (document.querySelector('.cognates .reference-text').clientWidth > (widestwidth + parseFloat(window.getComputedStyle(document.querySelector('.reference-text ol'))['marginLeft'])) * 2 + 10 && cognates[c].querySelectorAll('li').length > 4) {
            else if (maxWidth * 2 < containerWidth) dl.style.columnCount = 2;
             cognates[c].style.columnCount = 2;
             else dl.style.columnCount = 1;
         } else {
         } else {
             dl.style.columnCount = 1;
             cognates[c].style.columnCount = 1;
         }
         }
     });
     }
}
excol();
cogcol();
window.addEventListener('resize', () => {
    cogcol();
    excol();
})


    // Cognates columns
if (document.querySelector('.notelistalpha') != null && document.querySelector('.notelistalpha').childElementCount < 2) { document.querySelector('.notelistalpha').style.visibility = 'hidden' };
    document.querySelectorAll('.cognates ul').forEach(ul => {
        const lis = ul.querySelectorAll('li');
        if (!lis.length) return;
 
        let widestWidth = 0;
        lis.forEach(li => widestWidth = Math.max(widestWidth, li.clientWidth));
 
        const refText = document.querySelector('.cognates .reference-text');
        if (!refText) return;
 
        const refOl = refText.querySelector('ol');
        const marginLeft = refOl ? parseFloat(getComputedStyle(refOl).marginLeft || 0) : 0;
        const containerWidth = refText.clientWidth;
        const itemWidth = widestWidth + marginLeft + 10;
 
        if (lis.length > 7 && containerWidth > itemWidth * 3) {
            ul.style.columnCount = 3;
        } else if (lis.length > 4 && containerWidth > itemWidth * 2) {
            ul.style.columnCount = 2;
        } else {
            ul.style.columnCount = 1;
        }
    });
};


// Wikipedia links
// Wikipedia links
const cleanWikiLinks = () => {
var wlinks = document.getElementsByClassName('extiw');
    document.querySelectorAll('.extiw').forEach(link => {
for (var i = 0; i < wlinks.length; i++) {
        link.title = link.title.replace('wikipedia:vi:', '');
    wlinks[i].title = wlinks[i].title.replace('wikipedia:vi:', '')
    });
}
};


// Keyboard shortcuts
// CTRL + shortcuts
const setupShortcuts = () => {
document.addEventListener("keydown", function(e) {
    document.addEventListener('keydown', e => {
    if (e.key === 's' && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
        const isMac = navigator.platform.includes('Mac');
        e.preventDefault();
        if (!(isMac ? e.metaKey : e.ctrlKey)) return;
         document.querySelector('[value="Save changes"]').click();
 
    };
         const map = {
    if (e.key === 'p' && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
            's': '[value="Save changes"]',
        e.preventDefault();
            'p': '[value="Show preview"]',
        document.querySelector('[value="Show preview"]').click();
            'e': 'a[accesskey="e"]',
    };
            'r': 'input[accesskey="r"]',
    if (e.key === 'e' && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
            'g': 'input[accesskey="g"]'
        e.preventDefault();
        };
        document.querySelector('a[accesskey="e"]').click();
 
     };
        const selector = map[e.key];
    if (e.key === 'r' && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
        if (selector) {
        e.preventDefault();
            e.preventDefault();
        document.querySelector('input[accesskey="r"]').click();
            var el = document.querySelector(selector);
     };
            if (el) el.click();
    if (e.key === 'g' && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
        }
        e.preventDefault();
     });
        document.querySelector('input[accesskey="g"]').click();
};
    };
 
}, false);
// Clean citations
const cleanCitations = () => {
    const citerefs = document.querySelectorAll('[id^="cite_ref-"]');
     citerefs.forEach(ref => {
        if (ref.childNodes[0] && ref.childNodes[0].textContent === ' ') {
            ref.childNodes[0].textContent = '';
        }
    });


    if (citerefs.length) {
// Remove space before citation
        const parent = citerefs[0].parentElement;
var citerefs = document.querySelectorAll('[id^="cite_ref-"]');
         if (parent) {
if (citerefs.length > 0) {
             parent.innerHTML = parent.innerHTML.replaceAll(' <sup id="cite_ref-', '<sup id="cite_ref-');
    for (var i = 0; i < citerefs.length; i++) {
         if (citerefs[i].childNodes[0].textContent == ' ') {
             citerefs[i].childNodes[0].textContent = '';
         }
         }
     }
     }
};
    citerefs[0].parentElement.innerHTML = citerefs[0].parentElement.innerHTML.replaceAll(' <sup id="cite_ref-', '<sup id="cite_ref-');
}


// Line-break arrows - Preserved original fallback entries[0] case
// Line-break arrows
const processEntryArrows = () => {
var entries = '';
    if (window.location.href.search('index.php') >= 0 || window.location.href.lastIndexOf(':') !== 5) return;
if (window.location.href.search('index.php') < 0 && window.location.href.lastIndexOf(':') == 5) {
 
     entries = document.querySelector('ol').querySelectorAll('li');
     var ol = document.querySelector('ol');
     for (i = 0; i < entries.length; i++) {
    const entries = ol ? ol.querySelectorAll('li') : null;
         if (entries[i].innerHTML.match(/> →/g) != null) {
     if (!entries) return;
            var entry = entries[i].innerHTML.split('> →');
 
            entries[i].innerHTML = '';
    entries.forEach((entryEl, i) => {
            entries[i].innerHTML += entry[0] + 'b>';
         if (!entryEl.innerHTML.includes('> →')) return;
            for (j = 1; j < entry.length - 1; j++) {
 
                entries[i].innerHTML += '<br><span style="display:inline-block;margin-left:calc(' + (j + 1) + 'em + 0.5ch);text-indent: calc(-1em - 0.5ch)">' + '<arrow>↳ </arrow>' + entry[j] + '></span>';
        const parts = entryEl.innerHTML.split('> →');
            }
        let newHTML = parts[0] + 'b>';
             if (entry[entry.length - 1].search('dl') > -1 && entry[entry.length - 1].split('<dl')[0].slice(0, entry[entry.length - 1].split('<dl')[0].lastIndexOf(' ')).slice(entry[entry.length - 1].split('<dl')[0].slice(0, entry[entry.length - 1].split('<dl')[0].lastIndexOf(' ')).length - 2) != '<a') {
        let didFallback = false;  // FIX: flag to track if entries[0] fallback was used
                 entries[i].innerHTML += '<br><span style="display:inline-block;margin-left:calc(' + entry.length + 'em + 0.5ch);text-indent: calc(-1em - 0.5ch)">' + '<arrow>↳ </arrow>' + entry[entry.length - 1].split('<dl')[0].slice(0, entry[entry.length - 1].split('<dl')[0].lastIndexOf(' ')) + '&nbsp;' + entry[entry.length - 1].split('<dl')[0].slice(entry[entry.length - 1].split('<dl')[0].lastIndexOf(' ') + 1) + '</span><dl' + entry[entry.length - 1].split('<dl')[1];
 
            } else if (entry[entry.length - 1].slice(entry[entry.length - 1].lastIndexOf(' ') - 2, entry[entry.length - 1].lastIndexOf(' ')) != '<a') {
        for (let j = 1; j < parts.length; j++) {
                entries[i].innerHTML += '<br><span style="display:inline-block;margin-left:calc(' + entry.length + 'em + 0.5ch);text-indent: calc(-1em - 0.5ch)">' + '<arrow>↳ </arrow>' + entry[entry.length - 1].slice(0, entry[entry.length - 1].lastIndexOf(' ')) + '&nbsp;' + entry[entry.length - 1].slice(entry[entry.length - 1].lastIndexOf(' ') + 1) + '</span>';
             if (j < parts.length - 1) {
                 newHTML += `<br><span style="display:inline-block;margin-left:calc(${j+1}em + 0.5ch);text-indent:calc(-1em - 0.5ch)"><arrow>↳ </arrow>${parts[j]}</span>`;
             } else {
             } else {
                 // Last part - keep original fallback logic
                 entries[0].innerHTML += '<br><span style="display:inline-block;margin-left:calc(' + entry.length + 'em + 0.5ch);text-indent: calc(-1em - 0.5ch)">' + '<arrow>↳ </arrow>' + entry[entry.length - 1]
                if (parts[j].search('dl') > -1) {
                    const beforeDl = parts[j].split('<dl')[0];
                    const lastSpaceIdx = beforeDl.lastIndexOf(' ');
                    const textBefore = beforeDl.slice(0, lastSpaceIdx);
                    const lastWord = beforeDl.slice(lastSpaceIdx + 1);
 
                    newHTML += `<br><span style="display:inline-block;margin-left:calc(${parts.length}em + 0.5ch);text-indent:calc(-1em - 0.5ch)"><arrow>↳ </arrow>${textBefore}&nbsp;${lastWord}</span><dl${parts[j].split('<dl')[1]}`;
                } else if (parts[j].slice(parts[j].lastIndexOf(' ') - 2, parts[j].lastIndexOf(' ')) !== '<a') {
                    const lastSpace = parts[j].lastIndexOf(' ');
                    newHTML += `<br><span style="display:inline-block;margin-left:calc(${parts.length}em + 0.5ch);text-indent:calc(-1em - 0.5ch)"><arrow>↳ </arrow>${parts[j].slice(0, lastSpace)}&nbsp;${parts[j].slice(lastSpace + 1)}</span>`;
                } else {
                    // Original fallback: append to first entry
                    entries[0].innerHTML += `<br><span style="display:inline-block;margin-left:calc(${parts.length}em + 0.5ch);text-indent:calc(-1em - 0.5ch)"><arrow>↳ </arrow>${parts[j]}`;
                    didFallback = true;  // FIX: mark that we used the fallback
                }
             }
             }
         }
         }
    }
}
// Remove double spaces & replace Word Connectors * replace → arrow
if (window.location.href.indexOf('MediaWiki') == -1) {
    document.getElementById('mw-content-text').innerHTML = document.getElementById('mw-content-text').innerHTML.replaceAll('  ', ' ');
}
if (window.location.href.indexOf('index') == -1) {
    document.getElementById('mw-content-text').innerHTML = document.getElementById('mw-content-text').innerHTML.replaceAll(' &gt; ', '<con> &gt; </con>').replaceAll(' ~ ', '<con> ~ </con>');
}


        // FIX: only overwrite innerHTML if we didn't use the entries[0] fallback
// Zoom Videos
        if (!didFallback) {
var zoombtn = document.getElementsByClassName("enlarge");
            entryEl.innerHTML = newHTML;
for (var i = 0; i < zoombtn.length; i++) {
        }
    zoombtn[i].setAttribute("onclick", "enlarge(" + i + ")");
     });
     zoombtn[i].setAttribute("title", "Phóng to");
};
}
var video = document.querySelectorAll('[title="Play video"]');


// Video zoom
function enlarge(vid) {
const setupVideoZoom = () => {
    video[vid].width *= 2;
     const zoombtns = document.getElementsByClassName('enlarge');
    video[vid].height *= 2;
     const videos = document.querySelectorAll('[title="Play video"]');
    video[vid].style.setProperty("height", video[vid].height + "px");
    video[vid].style.setProperty("width", video[vid].width + "px");
    video[vid].parentElement.style.setProperty("height", video[vid].height + "px");
    video[vid].parentElement.style.setProperty("width", video[vid].width + "px");
    video[vid].parentElement.parentElement.style.setProperty("width", video[vid].width + "px");
     video[vid].parentElement.parentElement.style.setProperty("width", video[vid].width + "px");
     video[vid].parentElement.parentElement.parentElement.style.setProperty("width", video[vid].width + "px");
    video[vid].parentElement.parentElement.parentElement.style.setProperty("width", video[vid].width + "px");
    zoombtn[vid].setAttribute("onclick", "smaller(" + vid + ")");
    zoombtn[vid].setAttribute("title", "Thu nhỏ");
}


    const resizeVideo = (index, multiply) => {
function smaller(vid) {
        const vid = videos[index];
    video[vid].width /= 2;
        if (!vid) return;
    video[vid].height /= 2;
 
    video[vid].style.setProperty("height", video[vid].height + "px");
        vid.width *= multiply;
    video[vid].style.setProperty("width", video[vid].width + "px");
        vid.height *= multiply;
    video[vid].parentElement.style.setProperty("height", video[vid].height + "px");
 
    video[vid].parentElement.style.setProperty("width", video[vid].width + "px");
        const w = vid.width + 'px';
    video[vid].parentElement.parentElement.style.setProperty("width", video[vid].width + "px");
        const h = vid.height + 'px';
    video[vid].parentElement.parentElement.style.setProperty("width", video[vid].width + "px");
 
    video[vid].parentElement.parentElement.parentElement.style.setProperty("width", video[vid].width + "px");
        vid.style.width = w; vid.style.height = h;
     video[vid].parentElement.parentElement.parentElement.style.setProperty("width", video[vid].width + "px");
 
     zoombtn[vid].setAttribute("onclick", "enlarge(" + vid + ")");
        let parent = vid.parentElement;
     zoombtn[vid].setAttribute("title", "Thu nhỏ");
        while (parent && parent !== document.body) {
}
            parent.style.width = w;
            parent.style.height = h;
            parent = parent.parentElement;
        }
 
        const btn = zoombtns[index];
        if (btn) {
            const isEnlarge = multiply > 1;
            btn.onclick = () => resizeVideo(index, isEnlarge ? 0.5 : 2);
            btn.title = isEnlarge ? 'Thu nhỏ' : 'Phóng to';
        }
    };
 
    Array.from(zoombtns).forEach((btn, i) => {
        btn.onclick = () => resizeVideo(i, 2);
        btn.title = 'Phóng to';
    });
};
 
// Zoomed text
// FIX: wrap HTMLCollection with Array.from() before calling .forEach()
const setupZoomText = () => {
     const wrapClass = (className) => {
        Array.from(document.getElementsByClassName(className)).forEach(el => {
            const html = el.innerHTML;
            el.innerHTML = `<div class="tttext">${html}<span class="ttzoom"> ${html}</span></div>`;
        });
     };
 
    wrapClass('Hani');
     wrapClass('notHani');


    document.querySelectorAll('.ttzoom').forEach(span => {
// Display zoomed text
        if (span.getBoundingClientRect().right + 20 > window.innerWidth) {
var ht = document.getElementsByClassName('Hani');
            span.style.left = '-800%';
if (ht.length > 0) {
        }
     for (var i = 0; i < ht.length; i++) {
    });
         ht[i].innerHTML = '<div class="tttext">' + ht[i].innerHTML + '<span class="ttzoom"> ' + ht[i].innerHTML + '</span></div>';
};
 
// Hide empty ruby
const hideEmptyRuby = () => {
    document.querySelectorAll('rt').forEach(rt => {
        if (rt.innerText.trim() === '') rt.style.display = 'none';
    });
};
 
// Replace textimg - original i++ behavior preserved (skips every other element)
const setTextImgPlaceholders = () => {
    const textimg = document.querySelectorAll('[class*="textimg"]');
     for (let i = 0; i < textimg.length; i++) {
         const img = textimg[i].querySelector('img');
        if (img) img.src = 'https://www.tunguyentiengviet.com/images/4/47/Placeholder.png';
        i++;  // original manual i++ kept exactly as requested
     }
     }
};
}
 
var hc = document.getElementsByClassName('notHani');
// Highlight entry
if (hc.length > 0) {
const highlightEntry = () => {
    for (var i = 0; i < hc.length; i++) {
    if (window.location.href.includes('#entry')) {
        hc[i].innerHTML = '<div class="tttext">' + hc[i].innerHTML + '<span class="ttzoom"> ' + hc[i].innerHTML + '</span></div>';
        const id = window.location.href.substring(window.location.href.indexOf('#') + 1);
        const el = document.getElementById(id);
        if (el && el.parentElement) {
            el.parentElement.style.background = 'rgba(255,222,100,0.4)';
        }
     }
     }
};
}
 
// Main init
const init = () => {
    optimizeColumns();
    cleanWikiLinks();
    setupShortcuts();
    cleanCitations();
    processEntryArrows();
    setupVideoZoom();
    setupZoomText();
    hideEmptyRuby();
    setTextImgPlaceholders();
    highlightEntry();


    // Empty notelistalpha
// Zoomed text to left if overflow
    const notelist = document.querySelector('.notelistalpha');
var ttzoom = document.querySelectorAll('.ttzoom')
     if (notelist && notelist.childElementCount < 2) {
for (tt = 0; tt < ttzoom.length; tt++) {
         notelist.style.visibility = 'hidden';
     if (ttzoom[tt].getBoundingClientRect().right + 20 > window.innerWidth) {
         ttzoom[tt].style.left = '-800%'
     }
     }
 
}
    // Global replaces
// Hide empty ruby text
    const content = document.getElementById('mw-content-text');
var rt = document.getElementsByTagName('rt');
    if (content) {
if (rt.length > 0) {
        if (window.location.href.indexOf('MediaWiki') === -1) {
    for (var i = 0; i < rt.length; i++) {
            content.innerHTML = content.innerHTML.replaceAll('  ', ' ');
         if (rt[i].innerText == '') { rt[i].style.display = 'none' };
        }
         if (window.location.href.indexOf('index') === -1) {
            content.innerHTML = content.innerHTML
                .replaceAll(' &gt; ', '<con> &gt; </con>')
                .replaceAll(' ~ ', '<con> ~ </con>');
        }
     }
     }
}


    // Debounced resize
    let timeout;
    window.addEventListener('resize', () => {
        clearTimeout(timeout);
        timeout = setTimeout(optimizeColumns, 120);
    });
};


if (document.readyState === 'loading') {
// Replace textimg
     document.addEventListener('DOMContentLoaded', init);
var textimg = document.querySelectorAll('[class*="textimg"]');
} else {
if (textimg.length > 0) {
     init();
     for (var i = 0; i < textimg.length; i++) {
        textimg[i].querySelector('img').src = 'https://www.tunguyentiengviet.com/images/4/47/Placeholder.png';
        i++;
     }
}
}


document.querySelectorAll('.timedmediaplayer').forEach(el => el.classList.remove('notheme'));
// Highlight entry
if (window.location.href.includes('#entry')) {
    document.getElementById(window.location.href.substring(window.location.href.indexOf('#')+1,window.location.href.length)).parentElement.style.background = "rgba(255,222,100,0.4)"
}

Bản mới nhất lúc 10:38, ngày 30 tháng 4 năm 2026

// Example columns
function excol() {
    var dl = document.getElementsByTagName('dl');
    var maxwidth = 0;
    var cnt = 0;
    for (var i = 0; i < dl.length; i++) {
        cnt = 0;
        var dd = dl[i].getElementsByTagName('dd');
        for (j = 0; j < dd.length; j++) {
            dd[j].style.setProperty('min-width', 'max-content');
            dd[j].style.setProperty('column-span', 'unset');
            if (dd[j].clientWidth + parseFloat(window.getComputedStyle(dd[j]).marginLeft) > dl[i].clientWidth / 2) {
                dd[j].style.setProperty('min-width', 'calc(100% - 1.6em)');
                dd[j].style.setProperty('column-span', 'all');
            }
            if (maxwidth < dd[j].clientWidth && dd[j].style.minWidth != 'calc(100% - 1.6em)') {
                maxwidth = dd[j].clientWidth + parseFloat(window.getComputedStyle(dd[j]).marginLeft) + 5;
            }
        console.log(maxwidth);
        cnt++;
        }
        if (cnt >= 6 && maxwidth * 3 < dl[i].clientWidth) {
            dl[i].style.columns = 3
        } else if (cnt >= 6 && maxwidth * 2 < dl[i].clientWidth && maxwidth * 3 > dl[i].clientWidth) {
            dl[i].style.columns = 2
        } else {
            dl[i].style.columns = 1
        } 
    }
}

// Cognates columns
function cogcol() {
    var cognates = document.querySelectorAll('.cognates ul');
    for (var c = 0; c < cognates.length; c++) {
        var widest = null;
        var widestwidth = 0;
        var coglis = cognates[c].querySelectorAll('li');
        for (var l = 0; l < coglis.length; l++) {
            if (widest == null) {
                widestwidth = coglis[l].clientWidth;
                widest = l;
            } else if (widestwidth < coglis[l].clientWidth) {
                widestwidth = coglis[l].clientWidth;
                widest = l;
            }
        }

        if (document.querySelector('.cognates .reference-text').clientWidth > (widestwidth + parseFloat(window.getComputedStyle(document.querySelector('.reference-text ol'))['marginLeft'])) * 3 + 10 && cognates[c].querySelectorAll('li').length > 7) {
            cognates[c].style.columnCount = 3;
        } else if (document.querySelector('.cognates .reference-text').clientWidth > (widestwidth + parseFloat(window.getComputedStyle(document.querySelector('.reference-text ol'))['marginLeft'])) * 2 + 10 && cognates[c].querySelectorAll('li').length > 4) {
            cognates[c].style.columnCount = 2;
        } else {
            cognates[c].style.columnCount = 1;
        }
    }
}
excol();
cogcol();
window.addEventListener('resize', () => {
    cogcol();
    excol();
})

if (document.querySelector('.notelistalpha') != null && document.querySelector('.notelistalpha').childElementCount < 2) { document.querySelector('.notelistalpha').style.visibility = 'hidden' };

// Wikipedia links
var wlinks = document.getElementsByClassName('extiw');
for (var i = 0; i < wlinks.length; i++) {
    wlinks[i].title = wlinks[i].title.replace('wikipedia:vi:', '')
}

// CTRL + shortcuts
document.addEventListener("keydown", function(e) {
    if (e.key === 's' && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
        e.preventDefault();
        document.querySelector('[value="Save changes"]').click();
    };
    if (e.key === 'p' && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
        e.preventDefault();
        document.querySelector('[value="Show preview"]').click();
    };
    if (e.key === 'e' && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
        e.preventDefault();
        document.querySelector('a[accesskey="e"]').click();
    };
    if (e.key === 'r' && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
        e.preventDefault();
        document.querySelector('input[accesskey="r"]').click();
    };
    if (e.key === 'g' && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
        e.preventDefault();
        document.querySelector('input[accesskey="g"]').click();
    };
}, false);

// Remove space before citation
var citerefs = document.querySelectorAll('[id^="cite_ref-"]');
if (citerefs.length > 0) {
    for (var i = 0; i < citerefs.length; i++) {
        if (citerefs[i].childNodes[0].textContent == ' ') {
            citerefs[i].childNodes[0].textContent = '';
        }
    }
    citerefs[0].parentElement.innerHTML = citerefs[0].parentElement.innerHTML.replaceAll(' <sup id="cite_ref-', '<sup id="cite_ref-');
}

// Line-break arrows
var entries = '';
if (window.location.href.search('index.php') < 0 && window.location.href.lastIndexOf(':') == 5) {
    entries = document.querySelector('ol').querySelectorAll('li');
    for (i = 0; i < entries.length; i++) {
        if (entries[i].innerHTML.match(/> →/g) != null) {
            var entry = entries[i].innerHTML.split('> →');
            entries[i].innerHTML = '';
            entries[i].innerHTML += entry[0] + 'b>';
            for (j = 1; j < entry.length - 1; j++) {
                entries[i].innerHTML += '<br><span style="display:inline-block;margin-left:calc(' + (j + 1) + 'em + 0.5ch);text-indent: calc(-1em - 0.5ch)">' + '<arrow>↳ </arrow>' + entry[j] + '></span>';
            }
            if (entry[entry.length - 1].search('dl') > -1 && entry[entry.length - 1].split('<dl')[0].slice(0, entry[entry.length - 1].split('<dl')[0].lastIndexOf(' ')).slice(entry[entry.length - 1].split('<dl')[0].slice(0, entry[entry.length - 1].split('<dl')[0].lastIndexOf(' ')).length - 2) != '<a') {
                entries[i].innerHTML += '<br><span style="display:inline-block;margin-left:calc(' + entry.length + 'em + 0.5ch);text-indent: calc(-1em - 0.5ch)">' + '<arrow>↳ </arrow>' + entry[entry.length - 1].split('<dl')[0].slice(0, entry[entry.length - 1].split('<dl')[0].lastIndexOf(' ')) + '&nbsp;' + entry[entry.length - 1].split('<dl')[0].slice(entry[entry.length - 1].split('<dl')[0].lastIndexOf(' ') + 1) + '</span><dl' + entry[entry.length - 1].split('<dl')[1];
            } else if (entry[entry.length - 1].slice(entry[entry.length - 1].lastIndexOf(' ') - 2, entry[entry.length - 1].lastIndexOf(' ')) != '<a') {
                entries[i].innerHTML += '<br><span style="display:inline-block;margin-left:calc(' + entry.length + 'em + 0.5ch);text-indent: calc(-1em - 0.5ch)">' + '<arrow>↳ </arrow>' + entry[entry.length - 1].slice(0, entry[entry.length - 1].lastIndexOf(' ')) + '&nbsp;' + entry[entry.length - 1].slice(entry[entry.length - 1].lastIndexOf(' ') + 1) + '</span>';
            } else {
                entries[0].innerHTML += '<br><span style="display:inline-block;margin-left:calc(' + entry.length + 'em + 0.5ch);text-indent: calc(-1em - 0.5ch)">' + '<arrow>↳ </arrow>' + entry[entry.length - 1]
            }
        }
    }
}
// Remove double spaces & replace Word Connectors * replace → arrow
if (window.location.href.indexOf('MediaWiki') == -1) {
    document.getElementById('mw-content-text').innerHTML = document.getElementById('mw-content-text').innerHTML.replaceAll('  ', ' ');
}
if (window.location.href.indexOf('index') == -1) {
    document.getElementById('mw-content-text').innerHTML = document.getElementById('mw-content-text').innerHTML.replaceAll(' &gt; ', '<con> &gt; </con>').replaceAll(' ~ ', '<con> ~ </con>');
}

// Zoom Videos
var zoombtn = document.getElementsByClassName("enlarge");
for (var i = 0; i < zoombtn.length; i++) {
    zoombtn[i].setAttribute("onclick", "enlarge(" + i + ")");
    zoombtn[i].setAttribute("title", "Phóng to");
}
var video = document.querySelectorAll('[title="Play video"]');

function enlarge(vid) {
    video[vid].width *= 2;
    video[vid].height *= 2;
    video[vid].style.setProperty("height", video[vid].height + "px");
    video[vid].style.setProperty("width", video[vid].width + "px");
    video[vid].parentElement.style.setProperty("height", video[vid].height + "px");
    video[vid].parentElement.style.setProperty("width", video[vid].width + "px");
    video[vid].parentElement.parentElement.style.setProperty("width", video[vid].width + "px");
    video[vid].parentElement.parentElement.style.setProperty("width", video[vid].width + "px");
    video[vid].parentElement.parentElement.parentElement.style.setProperty("width", video[vid].width + "px");
    video[vid].parentElement.parentElement.parentElement.style.setProperty("width", video[vid].width + "px");
    zoombtn[vid].setAttribute("onclick", "smaller(" + vid + ")");
    zoombtn[vid].setAttribute("title", "Thu nhỏ");
}

function smaller(vid) {
    video[vid].width /= 2;
    video[vid].height /= 2;
    video[vid].style.setProperty("height", video[vid].height + "px");
    video[vid].style.setProperty("width", video[vid].width + "px");
    video[vid].parentElement.style.setProperty("height", video[vid].height + "px");
    video[vid].parentElement.style.setProperty("width", video[vid].width + "px");
    video[vid].parentElement.parentElement.style.setProperty("width", video[vid].width + "px");
    video[vid].parentElement.parentElement.style.setProperty("width", video[vid].width + "px");
    video[vid].parentElement.parentElement.parentElement.style.setProperty("width", video[vid].width + "px");
    video[vid].parentElement.parentElement.parentElement.style.setProperty("width", video[vid].width + "px");
    zoombtn[vid].setAttribute("onclick", "enlarge(" + vid + ")");
    zoombtn[vid].setAttribute("title", "Thu nhỏ");
}

// Display zoomed text
var ht = document.getElementsByClassName('Hani');
if (ht.length > 0) {
    for (var i = 0; i < ht.length; i++) {
        ht[i].innerHTML = '<div class="tttext">' + ht[i].innerHTML + '<span class="ttzoom"> ' + ht[i].innerHTML + '</span></div>';
    }
}
var hc = document.getElementsByClassName('notHani');
if (hc.length > 0) {
    for (var i = 0; i < hc.length; i++) {
        hc[i].innerHTML = '<div class="tttext">' + hc[i].innerHTML + '<span class="ttzoom"> ' + hc[i].innerHTML + '</span></div>';
    }
}

// Zoomed text to left if overflow
var ttzoom = document.querySelectorAll('.ttzoom')
for (tt = 0; tt < ttzoom.length; tt++) {
    if (ttzoom[tt].getBoundingClientRect().right + 20 > window.innerWidth) {
        ttzoom[tt].style.left = '-800%'
    }
}
// Hide empty ruby text
var rt = document.getElementsByTagName('rt');
if (rt.length > 0) {
    for (var i = 0; i < rt.length; i++) {
        if (rt[i].innerText == '') { rt[i].style.display = 'none' };
    }
}


// Replace textimg
var textimg = document.querySelectorAll('[class*="textimg"]');
if (textimg.length > 0) {
    for (var i = 0; i < textimg.length; i++) {
        textimg[i].querySelector('img').src = 'https://www.tunguyentiengviet.com/images/4/47/Placeholder.png';
        i++;
    }
}

// Highlight entry
if (window.location.href.includes('#entry')) {
    document.getElementById(window.location.href.substring(window.location.href.indexOf('#')+1,window.location.href.length)).parentElement.style.background = "rgba(255,222,100,0.4)"
}