Shoutbox

help me fix my script please - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: help me fix my script please (/showthread.php?tid=63215)

help me fix my script please by AberNStein on 07-14-2006 at 12:14 AM

ok so i'm working on a new script that involves scraping a webpage. below is the scraping bit, along with a saveToFile function, so i can see what stuff returns.

the problem:
my xmlhttprequest is returning nothing.
the script:

code:
function OnEvent_Initialize(MessengerStart)
{
getData();
}

var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

function getData()
{
xmlhttp.open("GET", "http://shoutbox.menthix.net/forumdisplay.php?fid=39", true);
xmlhttp.onreadystatechange = stateChanged;
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
  {
  if (xmlhttp.status==200)
    {
    saveToFile(xmlhttp.responseText);
    }
  else
    {
    saveToFile(xmlhttp.status);
    }
  }
}

function saveToFile(data1)
{
var fso, f1, thePath;
fso = new ActiveXObject("Scripting.FileSystemObject");
thePath = "C:\\file.txt";
f1 = fso.CreateTextFile(thePath, true);
f1.Write (data1);
f1.Close();
}


function OnEvent_Uninitialize(MessengerExit)
{
}


the script is kinda cut and pasted together from various sources, but it looks like it should work. it's returning a readystate of 4, but an http status of zero.

help me please.
RE: help me fix my script please by luisillo on 07-14-2006 at 12:27 AM

when you call your function, woudln't it be something about like that:

quote:
Originally posted by AberNStein
xmlhttp.onreadystatechange = stateChanged();
??

Otherwhise you are not calling the function, just telling it's name, and if I'm not wrong in the debug window it should say something like "Error inline xx not defined"
I hope this fixes your problem;)


Sorry, that is even worse, but your script DOES get the website's code, what's wrong whit your script? (sorry if this is a silly question)
RE: help me fix my script please by AberNStein on 07-14-2006 at 01:07 AM

for me, it doesn't.
are you saying that when you run that, it outputs the site's code into the file?
because for me it just outputs "0"

edit: wtf. it works now. i had a different irl in my actual script, but for some reason it works with the msghelp one. woah.


RE: help me fix my script please by BstrdSmkr on 07-14-2006 at 01:45 AM

i'm working on a similar project, and on the server i'm working with, it requires some headers to be set.  (request.setHeader("Referer", "chrome://myscript") in my case)  so check to see if that's the case with the server your working with.  if you don't have direct access to the server, the FireFox extension "Live HTTP headers" is a godsend. i'll see if i can find the address to the extention in a min, but its on the official extention site.   hope this helps! :)


RE: help me fix my script please by AberNStein on 07-14-2006 at 01:57 AM

thanks a bunch.
i've gotten quite far now with the script actually.
i'll post another version once i'm really stumped again though.

edit: here we go.
so the purpose of this script is to scrape the scripts forum and toast if there are any new requests. right now it scrapes the forum, and toasts any link with [request] (case insensitive) in it, and then it writes them to a text file. the goal is to have it check that text file before it toasts and then not toast if the thread is already there. i'll add the timer later.
p.s. there's a runtime error on line one or something, i'd rather supress it than figure it out. oh yeah and it toasts and logs twice for each one.
p.p.s. i write ugly code. don't laugh.

code:
function OnEvent_Initialize(MessengerStart)
{
getData();
}

function logging(datum)
{
var fso;
var ForAppending = 8;
var TheFile = MsgPlus.ScriptFilesPath + "\log.txt";
fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.OpenTextFile(TheFile, ForAppending, true);
a.WriteLine(datum);
a.Close();
}

var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

function getData()
{
xmlhttp.open("GET", "http://shoutbox.menthix.net/forumdisplay.php?fid=39", true);
xmlhttp.onreadystatechange = stateChanged;
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
  {
  if (xmlhttp.status==200)
    {
    parseIt(xmlhttp.responseText);
    }
  else
    {
    }
  }
}

function parseIt(htmls)
{
var foo = "";
var oDoc = new ActiveXObject("htmlfile");
oDoc.open();
oDoc.write(htmls);
oDoc.close();
var links = oDoc.getElementsByTagName("a");
for (i=0; i<links.length; i++)
{
foo = links[i].innerHTML.toLowerCase()
if (foo.indexOf("[request]") > -1)
{
MsgPlus.DisplayToast("msghelp.net [request]",links[i].innerHTML,'','OpenSite');
logging(links[i].innerHTML);
}
}
}

function OpenSite(){
new ActiveXObject('WScript.Shell').run('http://shoutbox.menthix.net/forumdisplay.php?fid=39');
}

function OnEvent_Uninitialize(MessengerExit)
{
}


RE: help me fix my script please by AberNStein on 07-14-2006 at 09:00 PM

bizzump fo shizzump


RE: help me fix my script please by Silentdragon on 07-14-2006 at 09:42 PM

Yeah..I got carried away, but you should be able to tell whats changed.

code:
var TheFile = MsgPlus.ScriptFilesPath + "\\log.txt";
var fso = new ActiveXObject("Scripting.FileSystemObject");
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

function OnEvent_Initialize(MessengerStart) {
    getData();
}
function checkDupe(datum) {
    if(fso.FileExists(TheFile)) {
        var a = fso.OpenTextFile(TheFile, 1);
        var text = a.ReadAll();
        text = text.split('\r\n');
        for(var i = 0;i<text.length;i++)
            if(text[i] == datum) {
                a.Close();
                return true;
            }
        a.Close();
        return false;
    }
}
function logging(datum) {
    if(fso.FileExists(TheFile)) {
            if(checkDupe(datum))
                return;
            a = fso.OpenTextFile(TheFile, 8);
    } else
        var a = fso.OpenTextFile(TheFile,2,true);
    a.WriteLine(datum);
    a.Close();
}

function getData() {
    xmlhttp.open("GET", "http://shoutbox.menthix.net/forumdisplay.php?fid=39", true);
    xmlhttp.onreadystatechange = stateChanged;
    xmlhttp.send(null);
}

function stateChanged() {
    if (xmlhttp.readyState==4)
        if (xmlhttp.status==200)
               parseIt(xmlhttp.responseText);
}

function parseIt(htmls)
{
    var foo = "";
    var oDoc = new ActiveXObject("htmlfile");
    oDoc.open();
    oDoc.write(htmls);
    oDoc.close();
    var links = oDoc.getElementsByTagName("a");
    for (var i=0; i<links.length; i++) {
        foo = links[i].innerHTML.toLowerCase()
        if (foo.indexOf("[request]") > -1 && !checkDupe(links[i].innerHTML)) {
            MsgPlus.DisplayToast("MsgHelp.net [Request]",links[i].innerHTML,'','OpenSite');
            logging(links[i].innerHTML);
        }
    }
}

function OpenSite() {
    new ActiveXObject('WScript.Shell').run('http://shoutbox.menthix.net/forumdisplay.php?fid=39');
}

function OnEvent_Uninitialize(MessengerExit) {
}

RE: help me fix my script please by AberNStein on 07-15-2006 at 01:26 AM

looks good!
thanks.

edit:

quote:
A Runtime Error has occurred.
Do you wish to Debug?

Line: 1
Error: Syntax Error

everything runs though, so if i can supress the error i'd be happy.