MediaWiki:Common.js: Difference between revisions

From Emmy The Robot Fandom Wiki
Content added Content deleted
No edit summary
No edit summary
Line 13: Line 13:
searchInput.setAttribute('type', 'text');
searchInput.setAttribute('type', 'text');
searchInput.setAttribute('id', 'mediawiki-text-parser-input');
searchInput.setAttribute('id', 'mediawiki-text-parser-input');
searchInput.setAttribute('placeholder', 'Type to search1');
searchInput.setAttribute('placeholder', 'Type to search');
document.getElementById('mw-content-text').insertBefore(searchInput, document.getElementById('mw-content-text').firstChild);
document.getElementById('mw-content-text').insertBefore(searchInput, document.getElementById('mw-content-text').firstChild);


Line 21: Line 21:


// Get all paragraphs in the content area
// Get all paragraphs in the content area
var paragraphs = document.querySelectorAll ('table.wikitable.sortable.mw-datatable.jquery-tablesorter tr:not(:first-child)');
var rows = document.querySelectorAll('table.wikitable.sortable.mw-datatable.jquery-tablesorter tr:not(:first-child)');


// Loop through each paragraph
// Loop through each row
rows.forEach(function(row) {
for (var i = 0; i < paragraphs.length; i++) {
var paragraph = paragraphs[i];
var rowCells = row.cells;
var paragraphText = paragraph.textContent.toLowerCase();
var rowShouldBeVisible = false;


// Check if the paragraph contains the search text
// Loop through each cell in the row
if (paragraphText.includes(searchText)) {
Array.from(rowCells).forEach(function(cell) {
paragraph.style.display = 'block'; // Show the paragraph
var cellText = cell.textContent.toLowerCase();
// Check if any cell contains the search text
if (cellText.includes(searchText)) {
rowShouldBeVisible = true;
}
});

// Set the display property of the row and its cells
if (rowShouldBeVisible) {
row.style.display = 'table-row';
Array.from(rowCells).forEach(function(cell) {
cell.style.display = 'table-cell';
});
} else {
} else {
paragraph.style.display = 'none'; // Hide the paragraph
row.style.display = 'none';
}
}
}
});
});
});
}
}

Revision as of 19:05, 10 May 2024

if ( mw.config.get( 'wgPageName' ) === 'Main_Page' ) {
    document.getElementsByTagName('br')[0].remove();
}

if ( mw.config.get( 'wgPageName' ) === 'Main_Page' ) {
    document.getElementsByTagName('h1')[0].remove();
}

// Function to initialize the text parsing and filtering functionality
function initializeTextParser() {
    // Create a search input field
    var searchInput = document.createElement('input');
    searchInput.setAttribute('type', 'text');
    searchInput.setAttribute('id', 'mediawiki-text-parser-input');
    searchInput.setAttribute('placeholder', 'Type to search');
    document.getElementById('mw-content-text').insertBefore(searchInput, document.getElementById('mw-content-text').firstChild);

    // Attach an event listener to the search input field
    searchInput.addEventListener('input', function() {
        var searchText = this.value.toLowerCase();

        // Get all paragraphs in the content area
        var rows = document.querySelectorAll('table.wikitable.sortable.mw-datatable.jquery-tablesorter tr:not(:first-child)');

        // Loop through each row
        rows.forEach(function(row) {
            var rowCells = row.cells;
            var rowShouldBeVisible = false;

            // Loop through each cell in the row
            Array.from(rowCells).forEach(function(cell) {
                var cellText = cell.textContent.toLowerCase();
                // Check if any cell contains the search text
                if (cellText.includes(searchText)) {
                    rowShouldBeVisible = true;
                }
            });

            // Set the display property of the row and its cells
            if (rowShouldBeVisible) {
                row.style.display = 'table-row';
                Array.from(rowCells).forEach(function(cell) {
                    cell.style.display = 'table-cell';
                });
            } else {
                row.style.display = 'none';
            }
        });
    });
}

// Call the initializeTextParser function when the DOM is ready
function initializeTextParserOnLoad() {
    if (document.readyState === "complete" || (document.readyState !== "loading" && !document.documentElement.doScroll)) {
        // The DOM has already loaded, so call initializeTextParser immediately
        initializeTextParser();
    } else {
        // Wait for the DOMContentLoaded event to call initializeTextParser
        document.addEventListener('DOMContentLoaded', initializeTextParser);
    }
}

// Call the initializeTextParserOnLoad function when the entire page is loaded
window.onload = initializeTextParserOnLoad;