MediaWiki:Common.js: Difference between revisions
From Emmy The Robot Fandom Wiki
Content added Content deleted
No edit summary |
No edit summary Tag: Manual revert |
||
(17 intermediate revisions by the same user not shown) | |||
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 |
searchInput.setAttribute('placeholder', 'Type to search'); |
||
// Set the width of the search input field as a percentage |
|||
searchInput.style.width = '100%'; // You can adjust the percentage here |
|||
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); |
||
// Get the table |
|||
var table = document.querySelector('table.wikitable.sortable.mw-datatable.jquery-tablesorter'); |
|||
// Attach an event listener to the search input field |
// Attach an event listener to the search input field |
||
Line 20: | Line 27: | ||
var searchText = this.value.toLowerCase(); |
var searchText = this.value.toLowerCase(); |
||
// Get all |
// Get all rows in the table |
||
var |
var rows = table.querySelectorAll('tr'); |
||
// Loop through each |
// Loop through each row |
||
rows.forEach(function(row, index) { |
|||
if (index === 0) { |
|||
// Show the first row (headers) always |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} else { |
} else { |
||
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(); |
|||
⚫ | |||
⚫ | |||
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'; |
|||
}); |
|||
⚫ | |||
row.style.display = 'none'; |
|||
} |
|||
} |
} |
||
} |
}); |
||
}); |
}); |
||
} |
} |
||
Line 40: | Line 64: | ||
// Call the initializeTextParser function when the DOM is ready |
// Call the initializeTextParser function when the DOM is ready |
||
function initializeTextParserOnLoad() { |
function initializeTextParserOnLoad() { |
||
var pathname = window.location.pathname; |
|||
if (document.readyState === "complete" || (document.readyState !== "loading" && !document.documentElement.doScroll)) { |
|||
var pageNames = ['List_of_fan_Nandroids', 'List_of_fan_stories']; |
|||
// The DOM has already loaded, so call initializeTextParser immediately |
|||
if (pageNames.includes(pathname.substring(pathname.lastIndexOf('/') + 1))) { |
|||
initializeTextParser(); |
initializeTextParser(); |
||
⚫ | |||
// Wait for the DOMContentLoaded event to call initializeTextParser |
|||
document.addEventListener('DOMContentLoaded', initializeTextParser); |
|||
} |
} |
||
} |
} |
Latest revision as of 07:52, 18 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');
// Set the width of the search input field as a percentage
searchInput.style.width = '100%'; // You can adjust the percentage here
document.getElementById('mw-content-text').insertBefore(searchInput, document.getElementById('mw-content-text').firstChild);
// Get the table
var table = document.querySelector('table.wikitable.sortable.mw-datatable.jquery-tablesorter');
// Attach an event listener to the search input field
searchInput.addEventListener('input', function() {
var searchText = this.value.toLowerCase();
// Get all rows in the table
var rows = table.querySelectorAll('tr');
// Loop through each row
rows.forEach(function(row, index) {
if (index === 0) {
// Show the first row (headers) always
row.style.display = 'table-row';
} else {
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() {
var pathname = window.location.pathname;
var pageNames = ['List_of_fan_Nandroids', 'List_of_fan_stories'];
if (pageNames.includes(pathname.substring(pathname.lastIndexOf('/') + 1))) {
initializeTextParser();
}
}
// Call the initializeTextParserOnLoad function when the entire page is loaded
window.onload = initializeTextParserOnLoad;