Greasemonkey, XPath, Setting Element Properties in a Loop
Posted: Tue Jun 30, 2009 11:08 am
I am working on a Greasemonkey script to act as a temporary fix for the Expand/Contract links on this forum. For details on this issue, read this post.
The script is able to find all of the codeholder divs and iterate through them with a while loop. However, if I try to set any of the div properties such as style.maxHeight or innerHTML, the loop breaks after the property is set for the first div. My attempts at setting the div properties have been commented out.
I cannot find the max-height property of the codeholder div in document.styeSheets[0].cssRules because it is an inline style, so I assume I must access it through the element. Does anyone have any ideas about how to do this?
Two code blocks are required to debug this, so here is a second dummy code block.
Edit: This post was recovered from search engine cache.
The script is able to find all of the codeholder divs and iterate through them with a while loop. However, if I try to set any of the div properties such as style.maxHeight or innerHTML, the loop breaks after the property is set for the first div. My attempts at setting the div properties have been commented out.
Code: Select all
// ==UserScript==
// @name Debugging DevNetwork.net Expand Code Link Fix
// @namespace https://snydev.com/greasemonkey/
// @description Fixes a bug on devnetwork.net forum related to the Expand/Contract links
// @include http://forums.devnetwork.net/viewtopic.php*
// ==/UserScript==
var codeHolders = document.evaluate("//div[@class='codeholder']", document, null, XPathResult.ANY_TYPE, null);
var ch = codeHolders.iterateNext();
var i = 0;
while (ch) {
i++;
//ch.style.maxHeight = '600px';
//ch.style.cssText = 'max-height: 600px';
//ch.style.setProperty('max-height', '600px' , 'important');
//ch.innerHTML = '';
window.alert('Count: ' + i + "\n" +
'ch.style.cssText: ' + ch.style.cssText + "\n" +
'ch.style.maxHeight: ' + ch.style.maxHeight);
ch = codeHolders.iterateNext();
}Two code blocks are required to debug this, so here is a second dummy code block.
Code: Select all
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z