Get contents of included JS file from within JS?

M

Matt Kruse

Let me break down the issue:

Ultimate Goal: Building a custom debugger window which shows a
function trace in and out of functions as they are called, which can
be executed via IE's Context Menu and used on any page (no source
changes needed, and all code can be IE-specific)

Mid-term Goal: To retrieve all the javascript source in the page, to
parse for function names, which can then be accessed in the window[]
namespace to retrieve their source.

Current Problem: I can't find a way to get access to the source of
external JS files, included using <SCRIPT SRC="">. If I look at the
innerHTML of the tag it is, of course, empty.

QUESTION: Is there any way to retrieve the source of the JS file,
other than using an IFRAME or something and loading the contents into
there to be read?

RELATED QUESTION: If not this, then is there any way (can be
IE6-specific) to access all the user-defined functions in a page? They
don't show up when you iterate the window[] namespace, yet they DO
exist there.

My debug windows is working, and my code currently goes in and renames
functions and adds debug message hooks. But it's useless if I can't
get access to the functions included in external files!

Thanks!

Matt Kruse
Javascript Toolbox: http://www.JavascriptToolbox.com/
 
K

kaeli

My debug windows is working, and my code currently goes in and renames
functions and adds debug message hooks. But it's useless if I can't
get access to the functions included in external files!

Doesn't Mozilla have an open source javascript debugger? Try looking at
their source. Maybe it will help.

Other than that, I'm not nearly good enough to answer this one. :)

--
 
M

Martin Honnen

Matt said:
Mid-term Goal: To retrieve all the javascript source in the page, to
parse for function names, which can then be accessed in the window[]
namespace to retrieve their source.

Current Problem: I can't find a way to get access to the source of
external JS files, included using <SCRIPT SRC="">. If I look at the
innerHTML of the tag it is, of course, empty.

QUESTION: Is there any way to retrieve the source of the JS file,
other than using an IFRAME or something and loading the contents into
there to be read?

IE on Win has Msxml.XMLHTTP to make HTTP requests, you could use that to
read the source of a file:

<html>
<head>
<title>document.scripts</title>
<script type="text/javascript" src="test20031223.js"></script>
<script type="text/javascript">
function getScriptFileContent (src) {
var httpRequest = new ActiveXObject('Msxml2.XMLHTTP');
httpRequest.open('GET', src, false);
// the false means synchronous loading but assuming the file
// is already cached it shouldn't do much harm
httpRequest.send(null);
return httpRequest.responseText;
}

function checkScripts () {
for (var i = 0; document.scripts && i < document.scripts.length; i++) {
var script = document.scripts;
if (script.text) {
alert(script.text);
}
else {
alert(getScriptFileContent(script.src));
}
}
}

window.onload = checkScripts;
</script>
</head>
<body>
<p>
document.scripts test
</p>
</body>
</html>

However be warned that the use of responseText is limited, if the .js
file is not UTF-8 encoded the and contains non-ASCII characters they
show up garbled in responseText
 
T

Thomas 'PointedEars' Lahn

Matt said:
Ultimate Goal: Building a custom debugger window which shows a
function trace in and out of functions as they are called, which can
be executed via IE's Context Menu and used on any page (no source
changes needed, and all code can be IE-specific)

There is a JScript debugger and Visual Studio (.NET) for the IE browser
component. (There is Venkman for Mozilla/5.0.)
Mid-term Goal: To retrieve all the javascript source in the page, to
parse for function names, which can then be accessed in the window[]
namespace to retrieve their source.

You could parse the whole document.documentElement.innerHTML but what
would that achieve?
Current Problem: I can't find a way to get access to the source of
external JS files, included using <SCRIPT SRC="">. If I look at the
innerHTML of the tag it is, of course, empty.

You can make it the src of a HTMLIFrameElement or HTMLObjectElement and
access its properties.
QUESTION: Is there any way to retrieve the source of the JS file,
other than using an IFRAME or something and loading the contents into
there to be read?

I know none.
RELATED QUESTION: If not this, then is there any way (can be
IE6-specific) to access all the user-defined functions in a page?

See above.
They don't show up when you iterate the window[] namespace,

That depends on the UA. They show up in Mozilla/5.0.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top