1 // Returns the current content document
2 function webdeveloper_getContentDocument()
4 return webdeveloper_getSelectedBrowser().contentDocument
;
7 // Returns the current content window
8 function webdeveloper_getContentWindow()
10 return webdeveloper_getSelectedBrowser().contentWindow
;
13 // Returns the document body element
14 function webdeveloper_getDocumentBodyElement(contentDocument
)
16 // If there is a body element
17 if(contentDocument
.body
)
19 return contentDocument
.body
;
23 var bodyElementList
= contentDocument
.getElementsByTagName("body");
25 // If there is a body element
26 if(bodyElementList
.length
> 0)
28 return bodyElementList
[0];
32 return contentDocument
.documentElement
;
35 // Returns the document head element
36 function webdeveloper_getDocumentHeadElement(contentDocument
)
38 var headElementList
= contentDocument
.getElementsByTagName("head");
40 // If there is a head element
41 if(headElementList
.length
> 0)
43 return headElementList
[0];
46 return contentDocument
.documentElement
;
49 // Gets all the documents from the current page
50 function webdeveloper_getDocuments(frame
)
52 var documents
= new Array();
54 // If the frame is set
57 var frames
= frame
.frames
;
58 var framesLength
= frames
.length
;
60 // If the frame document exists
63 documents
.push(frame
.document
);
66 // Loop through the frames
67 for(var i
= 0; i
< framesLength
; i
++)
69 documents
= documents
.concat(webdeveloper_getDocuments(frames
[i
]));
76 // Get the ancestors of the element
77 function webdeveloper_getElementAncestors(element
)
79 var ancestors
= webdeveloper_getElementAncestorsInternal(element
);
81 // Reverse the list and remove the last element which is the original element
88 // Recursively gets the ancestors of an element
89 function webdeveloper_getElementAncestorsInternal(element
)
91 var ancestors
= new Array();
93 // If the element is set
96 var parentElement
= element
.parentNode
;
98 // If the element has a tag name
101 ancestors
.push(element
);
104 // If there is a parent element
107 ancestors
= ancestors
.concat(webdeveloper_getElementAncestorsInternal(parentElement
));
114 // Get the children of the element
115 function webdeveloper_getElementChildren(element
)
117 var children
= new Array();
119 // If the element is set
123 var childNodes
= element
.childNodes
;
124 var childLength
= childNodes
.length
;
126 // Loop through the children
127 for(var i
= 0; i
< childLength
; i
++)
129 child
= childNodes
[i
];
131 // If the child and tag name are set
132 if(child
&& child
.tagName
)
134 children
.push(child
);
142 // Get the position of the element
143 function webdeveloper_getElementPosition(element
, xPosition
)
147 // If the element is set
150 var elementOffsetParent
= element
.offsetParent
;
152 // If the element has an offset parent
153 if(elementOffsetParent
)
155 // While there is an offset parent
156 while((elementOffsetParent
= element
.offsetParent
) != null)
158 // If getting the x position
161 position
+= element
.offsetLeft
;
165 position
+= element
.offsetTop
;
168 element
= elementOffsetParent
;
173 // If getting the x position
176 position
= element
.offsetLeft
;
180 position
= element
.offsetTop
;
188 // Get the x position of the element
189 function webdeveloper_getElementPositionX(element
)
191 return webdeveloper_getElementPosition(element
, true);
194 // Get the y position of the element
195 function webdeveloper_getElementPositionY(element
)
197 return webdeveloper_getElementPosition(element
, false);
200 // Returns the text from an element
201 function webdeveloper_getElementText(element
)
203 var elementText
= "";
205 // If the element is set
208 var childNode
= null;
209 var childNodeList
= element
.childNodes
;
210 var childNodeLength
= childNodeList
.length
;
211 var childNodeType
= null;
213 // Loop through the child nodes
214 for(var i
= 0; i
< childNodeLength
; i
++)
216 childNode
= childNodeList
[i
];
217 childNodeType
= childNode
.nodeType
;
219 // If the child node type is an element
220 if(childNodeType
== Node
.ELEMENT_NODE
)
222 elementText
+= webdeveloper_getElementText(childNode
);
224 else if(childNodeType
== Node
.TEXT_NODE
)
226 elementText
+= childNode
.nodeValue
+ " ";
234 // Returns the list of the images for the specified document
235 function webdeveloper_getImagesForDocument(contentDocument
, includeBackgroundImages
, includeIcons
)
237 var images
= new Array();
239 // If the content document is set
242 var backgroundImage
= null;
243 var computedStyle
= null;
244 var cssURI
= CSSPrimitiveValue
.CSS_URI
;
245 var documentURL
= contentDocument
.documentURI
;
248 var imageInterface
= Components
.interfaces
.nsIDOMHTMLImageElement
;
249 var inputInterface
= Components
.interfaces
.nsIDOMHTMLInputElement
;
250 var linkInterface
= Components
.interfaces
.nsIDOMHTMLLinkElement
;
251 var treeWalker
= contentDocument
.createTreeWalker(contentDocument
, NodeFilter
.SHOW_ELEMENT
, null, false);
252 var url
= Components
.classes
["@mozilla.org/network/standard-url;1"].createInstance(Components
.interfaces
.nsIURL
);
254 // While the tree walker has more nodes
255 while((element
= treeWalker
.nextNode()) != null)
257 // If this is an image element
258 if(element
instanceof imageInterface
)
260 images
.push(element
);
262 else if(element
instanceof inputInterface
&& element
.src
&& element
.type
&& element
.type
.toLowerCase() == "image")
265 url
.spec
= documentURL
;
266 image
.src
= url
.resolve(element
.src
);
268 // If this is not a chrome image
269 if(image
.src
.indexOf("chrome://") != 0)
274 else if(includeIcons
&& element
instanceof linkInterface
&& element
.href
&& element
.href
.indexOf("chrome://") != 0 && element
.rel
&& element
.rel
.indexOf("icon") != -1)
277 url
.spec
= documentURL
;
278 image
.src
= url
.resolve(element
.href
);
282 else if(includeBackgroundImages
)
284 computedStyle
= element
.ownerDocument
.defaultView
.getComputedStyle(element
, null);
286 // If the computed style is set
289 backgroundImage
= computedStyle
.getPropertyCSSValue("background-image");
291 // If this element has a background image and it is a URI
292 if(backgroundImage
&& backgroundImage
.primitiveType
== cssURI
)
295 image
.src
= backgroundImage
.getStringValue();
297 // If this is not a chrome image
298 if(image
.src
.indexOf("chrome://") != 0)
311 // Returns the list of the objects for the specified document
312 function webdeveloper_getObjectsForDocument(contentDocument
)
314 var objects
= new Array();
316 // If the content document is set
319 var documentObjects
= contentDocument
.embeds
;
320 var documentObjectsLength
= documentObjects
.length
;
322 // Loop through the document objects
323 for(var i
= 0; i
< documentObjectsLength
; i
++)
325 objects
.push(documentObjects
[i
]);
332 // Returns the selected browser
333 function webdeveloper_getSelectedBrowser()
335 return window
.top
.getBrowser().selectedBrowser
;
338 // Returns the list of the scripts for the specified document
339 function webdeveloper_getScriptsForDocument(contentDocument
, includeInline
)
341 var scripts
= new Array();
343 // If the content document is set
346 var documentScript
= null;
347 var documentScripts
= contentDocument
.getElementsByTagName("script");
348 var documentScriptsLength
= documentScripts
.length
;
349 var documentURL
= contentDocument
.documentURI
;
351 // Loop through the document scripts
352 for(var i
= 0; i
< documentScriptsLength
; i
++)
354 documentScript
= documentScripts
[i
];
356 // If including inline scripts or this is not inline
357 if(includeInline
|| documentScript
.src
!= documentURL
)
359 scripts
.push(documentScript
);
367 // Inserts the given child after the element
368 function webdeveloper_insertAfter(child
, after
)
370 // If the child and after are set
373 var nextSibling
= after
.nextSibling
;
374 var parent
= after
.parentNode
;
376 // If the element has a next sibling
379 parent
.insertBefore(child
, nextSibling
);
383 parent
.appendChild(child
);
388 // Inserts the given child as the first child of the element
389 function webdeveloper_insertAsFirstChild(element
, child
)
391 // If the element and child are set
394 // If the element has child nodes
395 if(element
.hasChildNodes())
397 element
.insertBefore(child
, element
.firstChild
);
401 element
.appendChild(child
);
406 // Returns true if the ancestor element is an ancestor of the element
407 function webdeveloper_isAncestor(element
, ancestorElement
)
409 // If the element and ancestor element are set
410 if(element
&& ancestorElement
)
412 var parentElement
= null;
414 // Loop through the parent elements
415 while((parentElement
= element
.parentNode
) != null)
417 // If the parent element is the ancestor element
418 if(parentElement
== ancestorElement
)
424 element
= parentElement
;
432 // Returns true if the page has frames
433 function webdeveloper_pageHasFrames()
435 // If the content document has a frame element
436 if(webdeveloper_getContentDocument().getElementsByTagName("frame").length
> 0)
444 // Removes all child elements from an element
445 function webdeveloper_removeAllChildElements(element
)
447 // If the element is set
450 var childElements
= element
.childNodes
;
452 // Loop through the child elements
453 for(var i
= 0; i
< childElements
.length
; i
++)
455 element
.removeChild(childElements
[i
]);
458 childElements
= element
.childNodes
;
460 // Loop through the child elements
461 while(childElements
.length
> 0)
463 element
.removeChild(childElements
[0]);
468 // Removes all elements matching the XPath
469 function webdeveloper_removeAllElementsByXPath(contentDocument
, xPath
)
471 var elementList
= webdeveloper_evaluateXPath(contentDocument
, xPath
);
472 var elementsLength
= elementList
.length
;
474 // Loop through all the elements
475 for(var i
= 0; i
< elementsLength
; i
++)
477 webdeveloper_removeElement(elementList
[i
]);
482 // Removes an element
483 function webdeveloper_removeElement(element
)
485 // If the element and it's parent node are set
486 if(element
&& element
.parentNode
)
488 element
.parentNode
.removeChild(element
);