<ch***********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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 = "©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 = "©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.