alert(line) alerts 'ac' and typeof(line) is 'string', but charAt is not a function

Posted by Delirium tremens on Stack Overflow See other posts from Stack Overflow or by Delirium tremens
Published on 2010-03-06T16:51:13Z Indexed on 2010/03/08 0:12 UTC
Read the original article Hit count: 973

Filed under:
|

alert(line) alerts 'ac' typeof(line) is 'string'

When I run line.charAt(0), charAt is not a function.
When line is 'http://www.google.com/', it works,

I think it's the UTF-8 encoding of the file that I opened...

How to make charAt work with UTF-8?

UPDATED:

http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/src/effective_tld_names.dat?raw=1 is in my extension's chrome folder as effective_tld_names.dat

To run the code:

authority = 'orkut.com.br';
lines = sc_geteffectivetldnames();  
lines = sc_preparetouse(lines);
domainname = sc_extractdomainname(authority, lines);

The code:

function sc_geteffectivetldnames () {
    var MY_ID = "[email protected]";
    var em = Components.classes["@mozilla.org/extensions/manager;1"].
             getService(Components.interfaces.nsIExtensionManager);
    var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID, "chrome/effective_tld_names.dat");
    var istream = Components.classes["@mozilla.org/network/file-input-stream;1"].
                            createInstance(Components.interfaces.nsIFileInputStream);
    istream.init(file, 0x01, 0444, 0);
    istream.QueryInterface(Components.interfaces.nsILineInputStream);
    var line = {}, lines = [], hasmore;
    do {
      hasmore = istream.readLine(line);
      lines.push(line.value); 
    } while(hasmore);
    istream.close();
    return lines;
}

function sc_preparetouse(lines) {
    lines = sc_notcomment(lines);
    lines = sc_notempty(lines);
    return lines;
}
function sc_notcomment(lines) {
    var line;
    var commentre;
    var matchedcomment;
    var replacedlines;
    replacedlines = new Array();
    var i = 0;
    while (i < lines.length) {
        line = lines[i];
        commentre = new RegExp("^//", 'i');
        matchedcomment = line.match(commentre);
        if(matchedcomment) {
            lines.splice(i, 1);
        } else {
            i++;
        }
    }
    return lines;
}
function sc_notempty(lines) {
    var line;
    var emptyre;
    var matchedempty;
    var replacedlines;
    replacedlines = new Array();
    var i = 0;
    while (i < lines.length) {
        line = lines[i];
        emptyre = new RegExp("^$", 'i');
        matchedempty = line.match(emptyre);
        if(matchedempty) {
            lines.splice(i, 1);
        } else {
            i++;
        }
    }
    return lines;
}
function sc_extractdomainname(authority, lines) {
    for (var i = 0; i < lines.length; i++) {
        line = lines[i];
        alert(line);
        alert(typeof(line));
        if (line.chatAt(0) == '*') {
            alert('test1');
            continue;
        }
        if (line.chatAt(0) == '!') {
            alert('test2');
            line.chatAt(0) = '';
        }
        alert('test3');
        checkline = sc_checknotasteriskline(authority, line);
        if (checkline) {
            domainname = checkline;
        }
    }
    if (!domainname) {
        for (var i = 0; i < lines.length; i++) {
            line = lines[i];
            alert(line);
            if (line.chatAt(0) != '*') {
                continue;
                alert('test4');
            }
            if (line.chatAt(0) == '!') {
                line.chatAt(0) = '';
                alert('test5');
            }
            alert('test6');
            checkline = sc_checkasteriskline(authority, line);
            if (checkline) {
                domainname = checkline;
            }
        }
    }
    return domainname;
}

It alerts 'ac', then 'string', then nothing.

© Stack Overflow or respective owner

alert(line) alerts 'ac' and typeof(line) is 'string', but charAt is not a function

Posted by Delirium tremens on Stack Overflow See other posts from Stack Overflow or by Delirium tremens
Published on 2010-03-06T16:51:13Z Indexed on 2010/03/08 13:36 UTC
Read the original article Hit count: 973

Filed under:
|

alert(line) alerts 'ac' typeof(line) is 'string'

When I run line.charAt(0), charAt is not a function.
When line is 'http://www.google.com/', it works,

I think it's the UTF-8 encoding of the file that I opened...

How to make charAt work with UTF-8?

UPDATED:

http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/src/effective_tld_names.dat?raw=1 is in my extension's chrome folder as effective_tld_names.dat

To run the code:

authority = 'orkut.com.br';
lines = sc_geteffectivetldnames();  
lines = sc_preparetouse(lines);
domainname = sc_extractdomainname(authority, lines);

The code:

function sc_geteffectivetldnames () {
    var MY_ID = "[email protected]";
    var em = Components.classes["@mozilla.org/extensions/manager;1"].
             getService(Components.interfaces.nsIExtensionManager);
    var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID, "chrome/effective_tld_names.dat");
    var istream = Components.classes["@mozilla.org/network/file-input-stream;1"].
                            createInstance(Components.interfaces.nsIFileInputStream);
    istream.init(file, 0x01, 0444, 0);
    istream.QueryInterface(Components.interfaces.nsILineInputStream);
    var line = {}, lines = [], hasmore;
    do {
      hasmore = istream.readLine(line);
      lines.push(line.value); 
    } while(hasmore);
    istream.close();
    return lines;
}

function sc_preparetouse(lines) {
    lines = sc_notcomment(lines);
    lines = sc_notempty(lines);
    return lines;
}
function sc_notcomment(lines) {
    var line;
    var commentre;
    var matchedcomment;
    var replacedlines;
    replacedlines = new Array();
    var i = 0;
    while (i < lines.length) {
        line = lines[i];
        commentre = new RegExp("^//", 'i');
        matchedcomment = line.match(commentre);
        if(matchedcomment) {
            lines.splice(i, 1);
        } else {
            i++;
        }
    }
    return lines;
}
function sc_notempty(lines) {
    var line;
    var emptyre;
    var matchedempty;
    var replacedlines;
    replacedlines = new Array();
    var i = 0;
    while (i < lines.length) {
        line = lines[i];
        emptyre = new RegExp("^$", 'i');
        matchedempty = line.match(emptyre);
        if(matchedempty) {
            lines.splice(i, 1);
        } else {
            i++;
        }
    }
    return lines;
}
function sc_extractdomainname(authority, lines) {
    for (var i = 0; i < lines.length; i++) {
        line = lines[i];
        alert(line);
        alert(typeof(line));
        if (line.chatAt(0) == '*') {
            alert('test1');
            continue;
        }
        if (line.chatAt(0) == '!') {
            alert('test2');
            line.chatAt(0) = '';
        }
        alert('test3');
        checkline = sc_checknotasteriskline(authority, line);
        if (checkline) {
            domainname = checkline;
        }
    }
    if (!domainname) {
        for (var i = 0; i < lines.length; i++) {
            line = lines[i];
            alert(line);
            if (line.chatAt(0) != '*') {
                alert('test4');
                continue;
            }
            if (line.chatAt(0) == '!') {
                alert('test5');
                line.chatAt(0) = '';
            }
            alert('test6');
            checkline = sc_checkasteriskline(authority, line);
            if (checkline) {
                domainname = checkline;
            }
        }
    }
    return domainname;
}

It alerts 'ac', then 'string', then nothing.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about firefox