Simple HTML read/write question

Discussion in 'Javascript' started by chris.schwalm@gmail.com, Nov 17, 2005.

  1. Guest

    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!!
    , Nov 17, 2005
    #1
    1. Advertising

  2. wrote:

    > 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
    Julian Turner, Nov 17, 2005
    #2
    1. Advertising

  3. Guest

    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
    , Nov 17, 2005
    #3
  4. wrote:

    > 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
    Julian Turner, Nov 17, 2005
    #4
  5. VK Guest

    VK, Nov 17, 2005
    #5
  6. VK wrote:

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

    >
    > 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
    Julian Turner, Nov 17, 2005
    #6
  7. McKirahan Guest

    <> wrote in message
    news:...
    > 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.
    McKirahan, Nov 17, 2005
    #7
  8. VK Guest

    Julian Turner wrote:
    > 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.lang.javascript/browse_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/20010309004241/developer.netscape.com/docs/manuals/signedobj/capabilities/index.html>

    P.S. And where are the promised code samples from you ? ;-)
    VK, Nov 17, 2005
    #8
  9. McKirahan wrote:

    > 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
    Thomas 'PointedEars' Lahn, Nov 17, 2005
    #9
  10. Guest

    THANKS A TON for all your help. This was way more then I expected!!
    , Nov 17, 2005
    #10
  11. McKirahan Guest

    <> wrote in message
    news:...
    > 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.
    McKirahan, Nov 18, 2005
    #11
  12. VK wrote:

    > 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
    Julian Turner, Nov 18, 2005
    #12
  13. VK Guest

    Got it!

    Great thanks!
    VK, Nov 18, 2005
    #13
  14. VK wrote:

    > 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
    Julian Turner, Nov 18, 2005
    #14
  15. VK Guest

    Julian Turner wrote:
    > 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.
    VK, Nov 18, 2005
    #15
  16. 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
    Julian Turner, Nov 18, 2005
    #16
  17. Sorry, one final thing (as original thread is closed), take a look at
    this for a complete library:-

    <URL:http://www.henk-reints.nl/cgi-bin/getsrc?http://henk-reints.nl/jscript/js/hr$binstring.js>

    Julian
    Julian Turner, Nov 18, 2005
    #17
  18. VK Guest

    Julian Turner wrote:
    > Sorry, one final thing (as original thread is closed), take a look at
    > this for a complete library:-
    >
    > <URL:http://www.henk-reints.nl/cgi-bin/getsrc?http://henk-reints.nl/jscript/js/hr$binstring.js>


    Julian Turner, you're my hero!
    :))


    <TOPIC IS CLOSED>
    VK, Nov 18, 2005
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Isaac
    Replies:
    0
    Views:
    2,459
    Isaac
    Jul 10, 2003
  2. ÀÏÓà
    Replies:
    0
    Views:
    363
    ÀÏÓà
    May 4, 2005
  3. Trans
    Replies:
    3
    Views:
    168
    Trans
    Dec 21, 2005
  4. sahm
    Replies:
    4
    Views:
    679
  5. Tim Chase
    Replies:
    0
    Views:
    88
    Tim Chase
    Dec 16, 2013
Loading...

Share This Page