Need a little help on my first script
|
|
I'm trying to make a script for my own use to wrangle the quotes on one of the forums I browse into being collapsible. The board is PHPBB 2.x and I use the Conundrum theme on it.
var quotes = document.evaluate( '//div[@class="inside_quote"]', document.body, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
for ( var i = 0; i < quotes.snapshotLength; i++ )
{
var quoteCell = quotes.snapshotItem( i );
var wroteCell = quoteCell.parentNode;
quoteCell.style.display = 'none';
var a = document.createElement( 'a' );
wroteCell.innerHTML += ' ';
wroteCell.insertBefore( a, wroteCell.firstChild.nextSibling );
a.href = '#'
a.innerHTML = '...';
a.addEventListener( 'click', function(e) {
e.preventDefault();
var Cell = this.nextSibling;
Cell.style.display = ( Cell.style.display == 'none' ? '' : 'none' );
}, false );
}
Above is what I have so far, but it only makes the top-level quotes collapsible (none of the cascaded ones) I'd suppose this is because the tags are cascaded also... meaning <div class="quote">user1 wrote: <div class="inside_quote"> <div class="quote">user2 wrote: <div class="inside_quote">message1</div> </div> message2</div> </div> Is an example of the syntax the theme uses. I'm guessing that evaluate won't return results that are contained in results it already found, and that for each quote, I need to recursively call this script i got here, substituting the current quoteCell (the body of the quote) in for document, but I can't seem to get that to work =/ Any help would be greatly appreciated. edited for code tag unborking. |
|
|
Have you look if you receive any javascript error? Try to log the Cell {GM_log(Cell)} to see if you have got the right element, to be sure to get the first div you can use also the following code: var Cell=this.parentNode.getElementsByTagName("DIV")[0]; |
|
|
I know I am getting the correct element because I'm getting all of the top-level quotes... just not any of the ones contained with-in those... within the expamle above, It adds the '...' to expand and collapse next to "user1 wrote" but not next to "user2 wrote"... |
|
|
I am not very happy with the wroteCell.innerHTML += ' ', first I don't know if the string concatenation operator += works with the innerHTML property, second because with this logic you add a space at the end of the innerHTML and I don't see the utility of it, third because doing it you force the DOM to covert a part of it to a string to only add a space at the end of it and reconvert it back to DOM format, forth because when reassign the innerHTML you replace all the previous elements with new ones and so you loose all the events that you have attached on the old ones. |
|
|
@Aquilax, string concatenation operator += does work with the innerHTML |
|
|
Also if it works it's the worst way to add a space, I use the innerHTML property only if I have to add a lot of html to a new created html element that is faster to write it into a string than using the createElement and appendChild methods.
|
|
|
i have some question regarding the same problem |
|
|
Back to the original question, I believe Aquilax answered it. when reassign the innerHTML you replace all the previous elements with new onesThe evaluate would presumably return all of the inside_quote's, but because of what Aquilax said, when you come to play with the nested ones they will have gone and been replaced, so it won't seem to work.
|
|
|
Thanks guys!
|
