![]() ![]() |
Hi, I'm having a lot of problems getting started on greasemonkey
How to remove rows from a table containing a certain text
How do I identify the rows that include the text, but also have other text? The example is based on this
This example looks for "td" elements, where the text is
images = document.evaluate('//tr/td[text()="keyword"]',document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null) I'd like to change it to a list of elements where "keyword" just has to be a part of the "td" element content. |
![]() ![]() |
Since I don't know XPath, I'm going to mention another way to do it, which would be to loop through the table and look at the textContent of each row. For example:
Please note that indexOf is case sensitive. But I'm sure XPath has wildcard character or something to let you search within the content. |
![]() ![]() |
Thanks for the hint. So far this is what I got through testing: // ==UserScript==
alert('Script working!'); var targetCells = document.evaluate (
for (var J = targetCells.snapshotLength - 1; J >= 0; --J)
This script now pops up alerts whenever a matching table row is found, so for identifying the rows this approach works. The code to remove the found table row from the other example does not work yet. |
![]() ![]() |
Or: var rows = document.querySelectorAll("tr");
for (var i=rows.length-1; i>=0; i--){
if (rows[i].textContent.indexOf("keyword")>-1) {
//rows[i].style.backgroundColor = "#0ff";
rows[i].parentNode.removeChild(rows[i]);
}
}
|
![]() ![]() |
This works when the page loads or I press F5 to reload - thanks! It doesn't work when I use the pages own navigation, but this page
Thanks for the help. |
![]() ![]() |
This one should select all |
![]() ![]() |
Thanks. This approach works even better (the nested table problem was something that gave me a problem with the previous approach.) // ==UserScript==
GM_log('Script working!'); var targetCells = document.evaluate("//td[contains(., 'keyword')]/parent::tr[not(descendant::table)]", document, null, 7, null); for (var J = targetCells.snapshotLength - 1; J >= 0; --J)
|

