2

it's a sports site with lots of text, i will be sorting through the text for interests later.

the only code that i have come across is:

<html>
<body>
<script language="JScript">
<!--
function open()

{
    var result = string.Empty;
    using (var webClient = new System.Net.WebClient())

    result = webClient.DownloadString("http://some.url");



    var myObject, afile;
    myObject = new ActiveXObject("Scripting.FileSystemObject");
    afile = myObject.OpenTextFile("F:\\sports.txt", 8, true, 0);
    afile.write (result);
    afile.close();
}
-->
</script>
Open a text stream for the file sport.txt

<form name="myForm">
<input type="Button" value="Open File" onClick='open()'>
</form>
</body>
</html>

any help would be appreciated, i can write in other languages too if needed. please direct me!!

user2095984
  • 21
  • 1
  • 2
  • possible duplicate of [Save text file on client machine using javascript](http://stackoverflow.com/questions/9717214/save-text-file-on-client-machine-using-javascript) – Quentin Feb 21 '13 at 15:59
  • For security reasons, you're never going to get (reliable, cross-browser) access to the user's file system. Your best bet is to spawn a separate page with just the text and provide a download option. – Mike Robinson Feb 21 '13 at 16:00
  • @Quentin - no it's not – PitaJ Feb 21 '13 at 16:01
  • I don't think the OP is asking to save it without a 'save as...' dialog. – Mooseman Feb 21 '13 at 16:01
  • The example is a bit misleading, especially the line with a reference to the file system. If he wants to just render a text file, we'll need to know more about his backend. – Mike Robinson Feb 21 '13 at 16:05
  • @MikeRobinson I mostly disregarded the quoted code as the OP said it was "the only code that i have come across." Besides, it's JScript with an `ActiveXObject` in it. – Mooseman Feb 21 '13 at 16:14

2 Answers2

0

If you want to write your own utility script that grabs the content of a page and downloads it to a file, and you want to write it in JavaScript, you could use Node.

http://nodejs.org/

If you just need a command-line tool to do that, use wget.

Both these options run on many platforms.

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
0

The code you post do nothing, as it's not a valid JS code. And with so unclear question, the answer may not what you ask for.

I'm not sure what you really want to save, the entire page source or just visible text that the browser render. Also you not specify in which environment will run your script, is it in a web browser or WSH?

I'll post example code for both cases (page source/text). I'll do my best to write in JScript at least one of them. However, it's more easy to me to write in VBScript, and as you said that's not a problem, my second example code will be in VBS.

To get html source code (.JS):

var url = 'http://some.url'; // set your page url here
with (new ActiveXObject("Microsoft.XmlHttp")) {
    open('GET', url, false);
    send('');
    var data = responseText;
    with (new ActiveXObject("ADODB.Stream")) {
        Open();
        Type = 2; // adTypeText
        Charset = 'utf-8'; // specify correct encoding
        WriteText(data);
        SaveToFile("page.html", 2);
        Close();
    }
}

To get visible/render text (.VBS):

Dim url: url = "http://some.url" 'set your page url here'
With WScript.CreateObject("InternetExplorer.Application", "IE_")
    .Visible = False
    .Navigate url
    Do
        WScript.Sleep 100
    Loop While .ReadyState < 4 And .Busy
    Dim data: data = .Document.Body.innerText
    With CreateObject("ADODB.Stream")
        .Open
        .Type     = 2 'adTypeText'
        .Position = 0
        .Charset  = "utf-8"
        .WriteText data
        .SaveToFile "output.txt", 2
        .Close
    End With
    .Quit
End With
Panayot Karabakalov
  • 3,109
  • 3
  • 19
  • 28