Last active
March 8, 2025 17:41
-
-
Save chapmanjacobd/a6df242a6b902eb032d8b76c46b7c45b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Table Click Filter | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Click a table cell, filter the column based on input. | |
// @author You | |
// @match *://*/* | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
function filterColumn(table, columnIndex, filterText) { | |
if (!table || !table.rows || table.rows.length === 0) { | |
return; // Handle empty or invalid table | |
} | |
for (let i = 1; i < table.rows.length; i++) { // Start from 1 to skip header | |
const row = table.rows[i]; | |
if (row.cells.length > columnIndex) { | |
const cellText = row.cells[columnIndex].innerText; | |
if (!(filterText && cellText.indexOf(filterText) === -1)) { | |
row.style.display = 'none'; | |
} | |
} | |
} | |
} | |
function handleCellClick(event) { | |
const target = event.target; | |
if (target.tagName === 'TD') { | |
const cellText = target.innerText; | |
const columnIndex = target.cellIndex; | |
const table = target.closest('table'); | |
if (!table) { | |
return; // Not in a table | |
} | |
const userInput = prompt("Filter by:", cellText); | |
if (userInput === null) { | |
return; // Cancel was pressed | |
} | |
const filterValue = userInput.trim() === "" ? cellText : userInput.trim(); | |
filterColumn(table, columnIndex, filterValue); | |
} | |
} | |
document.addEventListener('click', handleCellClick); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment