rpgs_player
rpgs_player

Reputation: 5449

Getting the content of <script> element

I've tried to use innerHTML but it returns null. Here's my code:

var sumScripts = gBrowser.contentDocument.getElementsByTagName("script");
  for (i = 0; i < sumScripts.length; i++) { 
    var content = sumScripts[i].innerHTML;
    alert(content);

So I tried to get all the scripts on a webpage and print them, but it prints null each time. Does anyone know what's wrong? This is XUL JavaScript by the way.

EDIT : Silly me. I fixed the iterator typo (changing 0 to i), thanks. It works now.

Upvotes: 0

Views: 63

Answers (1)

Noitidart
Noitidart

Reputation: 37238

In FF addon priveleaged scope you have a ton available to you.

You can access Document.scripts. Read about it here on MDN:

https://developer.mozilla.org/en-US/docs/Web/API/Document

But this is also available to non-priveagled. Just do window.document.scripts and itterate.

var scripts = gBrowser.contentWindow.document.scripts;
for (var i=0; i<scripts.length; i++) {
  console.log(i, scripts[i].src, scripts[i].innerHTML)
}

keep in mind, some script elements have set src=blah so they wont have innerHTML. You would have to get that by either live fetch (xmlhttprequest, etc) or cached value.

Upvotes: 1

Related Questions