Beginner's problem: Remove rows from table based on text.

in Script development
Subscribe to Beginner's problem: Remove rows from table based on text. 7 posts, 3 voices



DaJo User
FirefoxWindows

Hi,

I'm having a lot of problems getting started on greasemonkey
and hope that someone can help give me a link to help me solve
this problem.

How to remove rows from a table containing a certain text
like "keyword"

How do I identify the rows that include the text, but also have other text?

The example is based on this
http://userscripts.org/topics/2343?page=1#posts...

This example looks for "td" elements, where the text is
"keyword"

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.

 
Jefferson Scher Scriptwright
FirefoxWindows

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:

if (tableRow.textContent.indexOf("keyword")>-1) {//do stuff}

Please note that indexOf is case sensitive.

But I'm sure XPath has wildcard character or something to let you search within the content.

 
DaJo User
FirefoxWindows

Thanks for the hint.

So far this is what I got through testing:

// ==UserScript==
// @name Test 1
// @namespace Helpdesk
// @include *
// ==/UserScript==

alert('Script working!');

var targetCells = document.evaluate (
"//table/tbody/tr[2]/td[1]",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null
);

for (var J = targetCells.snapshotLength - 1; J >= 0; --J)
{
var thisCell = targetCells.snapshotItem (J);

if (thisCell.textContent.indexOf("keyword")>-1)
{
alert(thisCell.textContent);

foo = thisCell.parentNode.parentNode
foo.parentNode.removeChild(foo.nextSibling)
foo.parentNode.removeChild(foo)
}

}

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.

 
Jefferson Scher Scriptwright
FirefoxWindows

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]);
  }
}

 
DaJo User
FirefoxWindows

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
has a lot of complicated javascript code so for the time being having to
press F5 to remove the lines works for me.

Thanks for the help.

 
AmpliDude Scriptwright
FirefoxWindows

document.evaluate("//td[contains(., 'keyword')]/parent::tr[not(descendant::table)]", document, null, 7, null);

This one should select all tr elements that are parents of td elements which contain 'keyword'. Also it won't select tr elements that have nested table elements inside.

 
DaJo User
FirefoxWindows

Thanks. This approach works even better (the nested table problem was something that gave me a problem with the previous approach.)

// ==UserScript==
// @name Test 1
// @namespace Helpdesk
// @include https://helpdesk.*/HelpDesk.jsp*
// ==/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)
{
var thisCell = targetCells.snapshotItem (J);

GM_log(thisCell.textContent);

foo = thisCell.parentNode

// foo.style.backgroundColor = "#808080";

foo.parentNode.removeChild(foo.nextSibling)
foo.parentNode.removeChild(foo)


}