Simple HTML read/write question

C

chris.schwalm

I would like to first state that I have searched through the archives
and found a lot of related material, but am still new enough to
javascript that I can't fit all the pieces together yet. So here is my
situation.

I would like to create a program that grabs the source of a displayed
webpage, sends it off to another [external] program, then receive the
slightly modified source back from that other program, and then reposts
the slightly modified source code back to the origonal page.

What I am hoping to get from this group is a few leads.

(1) It is possible to write the contents of a webpage as an output
stream?
(2) Is it possible to completely overwrite the source code of a webpage
with new source code?
(3) Could I put this kind of functionality in a bookmarkelt/favelet?

Could you shoot me a few idea's as to how this is done. Thanks!!
 
J

Julian Turner

I would like to first state that I have searched through the archives
and found a lot of related material, but am still new enough to
javascript that I can't fit all the pieces together yet. So here is my
situation.

I would like to create a program that grabs the source of a displayed
webpage, sends it off to another [external] program, then receive the
slightly modified source back from that other program, and then reposts
the slightly modified source code back to the origonal page.

Hi. Why do you want to do this? I would be helpful to understand the
objective.
What I am hoping to get from this group is a few leads.

(1) It is possible to write the contents of a webpage as an output
stream?

It is possible to read a remote web page into a string using the
responseText property of the HTTPRequest object.
(2) Is it possible to completely overwrite the source code of a webpage
with new source code?
[snip]

Do you mean overwrite it on a remote web server or on your local
machine?

If you mean on your local machine, then you can use
Scriping.FileSystemObject (IE) or XPCOM (Mozilla), to read and write
local files from Javascript/JScript.

If you mean overwite it on a remote web server which you do not
control, then obviously no: think of the security implications. I
assume you don't mean this.

If you mean overwrite it on a remote server you control, then you could
either:-

- Overwrite the file using FTP. Javascript does not have FTP
natively, so you would need some form of component to do this.
- Create a web page on that server using ASP, PHP etc, that can
receive and process a POST request from an HTTPRequest, and use file
save components on that server. If it is a UNIX server, remember to
set the appropriate file access permissions.

Julian
 
C

chris.schwalm

Yes, I would like to overwrite it on my local machine.

The basic concept is the following: I would like to take a webpage that
has some sort of encrypted data on it. Run my java script...and have it
replace the encrypted data with unencrypted data.

Thanks for your help
 
J

Julian Turner

Yes, I would like to overwrite it on my local machine.

The basic concept is the following: I would like to take a webpage that
has some sort of encrypted data on it. Run my java script...and have it
replace the encrypted data with unencrypted data.

Thanks for your help

Hi. In which case, it sounds like you just need to learn about file
handling components that ship with IE and Firefox.

I.e. I am assuming that you want to load a web page as a string from a
given directory on your machine, process it as a string, and then save
again on the local computer.

As noted, there are two sets of file handling options available:-

1. For Internet Explorer

Scripting.FileSystemObject - an ActiveX object. Search in particular
on the Microsoft MSDN web site.

2. For Firefox

There are load and save functions at the end of the following page:-

http://www.captain.at/programming/xul/

Julian
 
J

Julian Turner

VK said:
Does it really work for you w/o second privilege for
"UniversalFileRead"?
If so, what Firefox version are you using?

Sorry, not quite sure what you mean. I must confess to only having
Mozilla 1.6. Have not tested on Firefox yet. Does it not work on
Firefox? Have you checked security settings.

If I have understood you correctly, AFAIK you need to use

netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

in every function that is using a component. It is not like ActiveX,
create one time, use many times. With UniveralXPConnect you must
enable privileges in each function and nested function that uses it.

Regards

Julian
 
M

McKirahan

I would like to first state that I have searched through the archives
and found a lot of related material, but am still new enough to
javascript that I can't fit all the pieces together yet. So here is my
situation.

I would like to create a program that grabs the source of a displayed
webpage, sends it off to another [external] program, then receive the
slightly modified source back from that other program, and then reposts
the slightly modified source code back to the origonal page.

What I am hoping to get from this group is a few leads.

(1) It is possible to write the contents of a webpage as an output
stream?
(2) Is it possible to completely overwrite the source code of a webpage
with new source code?
(3) Could I put this kind of functionality in a bookmarkelt/favelet?

Could you shoot me a few idea's as to how this is done. Thanks!!

This is sort-of what you want, I think.

1) It grabs the source of a URL
2) It modifies part of it; (xTXT replaces sTXT)
3) It replaces the current page
(.innerHTML doesn't work in all browsers)
4) It doesn't resolve relative paths (e.g. images).

Watch for word-wrap.

<html>
<head>
<title>Googler.htm</title>
<script type="text/javascript">
function Googler() {
var sURL = "http://www.google.com/index.html";
var oXML = new ActiveXObject("Microsoft.XMLHTTP");
oXML.Open("GET",sURL,false);
oXML.send();
try {
var sXML = oXML.ResponseText;
var sTXT = "&copy;2005 Google"
var xTXT = This page has been modified by Googler!"
var iTXT = sXML.indexOf(sTXT
var sHTM = sXML.substr(0,iTXT) + xTXT +
sXML.substr(iTXT+sTXT.length);
document.body.innerHTML = sHTM;
} catch(e) {
alert(sURL + " not found!");
}
}
</script>
</head>
<body onload="Googler()">
</body>
</html>


This version adds a <base href=''> tag after the <body> tag to resolve
relative paths.

<html>
<head>
<title>Googler.htm</title>
<script type="text/javascript">
function Googler() {
var sURL = "http://www.google.com/index.html";
var xURL = sURL.substr(0,sURL.lastIndexOf("/")+1);
var oXML = new ActiveXObject("Microsoft.XMLHTTP");
oXML.Open("GET",sURL,false);
oXML.send();
try {
var sXML = oXML.ResponseText;
// Replace the value of sTXT with the value of xTXT.
var sTXT = "&copy;2005 Google";
var xTXT = "This page has been modified by Googler!";
var iTXT = sXML.indexOf(sTXT);
var sHTM = sXML.substr(0,iTXT) + xTXT +
sXML.substr(iTXT+sTXT.length);
// Insert <base href=''> after the <body> tag.
var sTAG = "<body";
var iTAG = sHTM.toLowerCase().indexOf(sTAG);
var jTAG = sHTM.substr(iTAG).indexOf(">") + iTAG;
var xTAG = "<base href='" + xURL + "'>";
sHTM = sHTM.substr(0,jTAG+1) + xTAG + sHTM.substr(jTAG+1);
// Replace the page's source code.
document.body.innerHTML = sHTM;
} catch(e) {
alert(sURL + " not found!");
}
}
</script>
</head>
<body onload="Googler()">
</body>
</html>

There are probably flaws with this approach which others will tell us.
 
V

VK

Julian said:
If I have understood you correctly, AFAIK you need to use
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

You're absolutely right. Mozilla is using Netscape 4.x Java security
model for XPConnect (despite it is not Java based anymore).

You *always* have to ask for UniversalXPConnect first:
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

It gives you privilege to use XPConnect per se (create class instances
etc.) but rather useless without a privilege to do something outside
the sandbox.

In the particular in order to access local file system you have to ask
for additional privilege: either UniversalFileRead or
UniversalFileWrite.

So the combined code will be:
....
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
netscape.security.PrivilegeManager.enablePrivilege("UniversalFileRead");
....
otherwise even with UniversalXPConnect enabled you will not be able to
access local files.

It works this way at least for Firefox under Windows 98 / XP - and it
should work this way. All this of course for default security settings.

Here my original post with the code sample:
<http://groups.google.com/group/comp..._frm/thread/7cd482713658987b/61b24d432da604b8>

Unfortunately Mozilla did not implement (yet?) Netscape's macro
targets.
In Netscape you had
Primitive targets (like "UniversalFileRead", "UniversalFileWrite")
Macro targets (like "UniversalFileAccess")

You may find interesting this forgotten lore which become recently once
again actual:
<http://web.archive.org/web/20010309...ocs/manuals/signedobj/capabilities/index.html>

P.S. And where are the promised code samples from you ? ;-)
 
T

Thomas 'PointedEars' Lahn

McKirahan said:
function Googler() {
var sURL = "http://www.google.com/index.html";
var oXML = new ActiveXObject("Microsoft.XMLHTTP");
oXML.Open("GET",sURL,false);
oXML.send();
try {

Since you cannot test for ActiveX objects, the
NewExpression has to be within the `try' block.

And I think it should be open(), not Open().
There are probably flaws with this approach which others will tell us.

[x] done


PointedEars
 
M

McKirahan

THANKS A TON for all your help. This was way more then I expected!!

Your very welcome.

Be aware, though, that the <base href=''> tag will not resolve relative
paths in the body tag (e.g. "background=") nor those in scripts or CSS.
 
J

Julian Turner

VK said:
P.S. And where are the promised code samples from you ? ;-)

If you look at the original thread you requested them on, you might
find them. I posted them yesterday (my time).

Julian
 
J

Julian Turner

VK said:
Got it!

Great thanks!

Good. I know the code nothing special, but let me know how you get on.
I use it just for my own private applications, but I have not tested it
in anger , and it is a bit of a hack, so I fully expect the code to
fall over or some unexpected twist to reveal itself.

Julian
 
V

VK

Julian said:
Good. I know the code nothing special, but let me know how you get on.
I use it just for my own private applications, but I have not tested it
in anger , and it is a bit of a hack, so I fully expect the code to
fall over or some unexpected twist to reveal itself.

Yes, I think that in the first release of jsFileManager I limit myself
with the original set:
1) Display full directory info with multiple file selection.
2) Read/Write/Create/Delete text files (.txt and .html)
3) Launch applications

This alone takes a lot to test for full IE/Gecko compatability. I guess
it is better to have lesser features but working right rather than more
features with some bombs in them :)

I definitely would like to implement later binary read/write and base64
codec. If binary I/O indeed can be implemented on IE only via VBScript,
I'll use
window.execScript(code,'VBScript') wrapper to keep one js-only library.
 
J

Julian Turner

VK wrote:

[snip]
I definitely would like to implement later binary read/write and base64
codec. If binary I/O indeed can be implemented on IE only via VBScript,
I'll use
window.execScript(code,'VBScript') wrapper to keep one js-only library.

I am looking at this as purely academic interest (being a hobbyist, not
a professional), to create an ASP web page to enable me to manage files
on my server remotely as an alternative to FTP.

I was not aware of that function. I will have a look at it. I have
been dynamically creating script tags.

I am also looking at what scriptable I/O streams Firefox has, and will
share the results.

Finally, I am exploring creating a Java applet to do base64 encoding,
as this is a slow process with Javascript for files >100k.

We are well off topic now, so I'll say nothing more.

Julian
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top