5

In a userscript i am getting [object XrayWrapper [object HTMLSpanElement]] instead of html span tag.

How can i get the html tag like <span>--</span> from this object?

Oliver Salzburg
  • 86,445
  • 63
  • 260
  • 306
UserPink
  • 51
  • 1
  • 2
  • Please provide more details on what you're doing and/or trying to do. – Oliver Salzburg May 24 '12 at 11:44
  • I am printing the dynamically created tags by creating a hook to document.createElement method in userscript. The output is presented in web console using console.log(). This gives `` in chrome but in firefox the output is shown in `[object XrayWrapper [object HTMLSpanElement]]` object format – UserPink May 25 '12 at 04:02
  • 1
    Please [edit] your question to supply that info. – slhck May 25 '12 at 08:24

2 Answers2

4

The only difference is in the toString() method, which determines what happens when you try to turn an object into a primitive. You didn't state what you want to do, but if you're trying to turn a DOM object into HTML textual representation, then the outerHTML property should provide you with what you need.

var element = document.createElement("span");
element.appendChild(document.createTextNode("text"));
alert(element.outerHTML); // returns "<span>text</span>"
Witiko
  • 263
  • 4
  • 22
  • Can this be done without using document.createElement as i am including this code in the hook created for document.createElement this is becoming a never ending loop. can any one help – UserPink May 29 '12 at 04:09
  • In the above solution where should i add the code which shows [object XrayWrapper [object HTMLSpanElement]] – UserPink May 29 '12 at 04:15
  • This was just an example. Simply refer to `object.outerHTML` instead of `object` in your code. – Witiko May 29 '12 at 09:41
  • object.outerHTML retrieves the tag but how to obtain the innerHTML of the object without creating a new Element. i tried using object.innerHTML but it is showing undefined – UserPink May 29 '12 at 10:49
  • I see. It would appear that the `XrayWrapper` used by Greasemonkey to wrap around `DOM Elements` is quite an imperfect wrapper, since it doesn't really support the `innerHTML` getter/setter. I wrote you a simplistic `innerHTML` implementation, call it like this: `html(element)`. It ignores everything but elements, attributes and text nodes, but it should serve its purpose as long as the `XrayWrapper` supports the other `DOM attributes` I use in the function. http://pastebin.com/9LYPU67r – Witiko May 29 '12 at 16:03
0

In the desired html element, just access the innerText property. ex: element.innerText

Toto
  • 17,001
  • 56
  • 30
  • 41
Wesley
  • 1