I have this code:
<script type="text/javascript">
var url = "http://www.xxxxx.xxx/xxxxxxxxx";
var txt;
var id1;
var id2;
var imgarres = [];
var imgarr = [];
var imgels = [];
function getdata() {
if (id1){clearTimeout(id1);}
if (id2){clearTimeout(id2);}
var xhr = new XMLHttpRequest();
xhr.open('GET',url, true);
xhr.setRequestHeader('Cache-Control', 'no-cache');
xhr.setRequestHeader('Pragma', 'no-cache');
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
txt = xhr.responseText;
var r = txt.indexOf('<b class="fl_r">Online</b>');
var el = document.createElement("div");
el.innerHTML = txt;
var n = imgprocess(el);
var nam = el.getElementsByTagName("title")[0].innerHTML;
if (r != -1) {
var notification = webkitNotifications.createNotification('plus.gif', nam, 'online!!' );
notification.show();
var id1 = setTimeout(getdata, 60000);
} else {
var notification = webkitNotifications.createNotification(n, nam, 'offline!!' );
notification.show();
var id2 = setTimeout(getdata, 600000);
}
}
}
xhr.send();
}
function imgprocess(text) {
imgels = text.getElementsByTagName("IMG");
for (var i=0;i< imgels.length;i++) {
if (imgels[i].src.indexOf(parse(url)) != -1) {
imgarr = imgels[i];
}
}
for (var p=0; p< imgarr.length; p++) {
if (imgarr[p].parentNode.nodeName=="A") {
imgarres = imgarr[p];
}
}
var z = imgarres[0].src;
return z;
}
function init() {
getdata();
}
</script>
</head>
<body onload="init();">
When I execute this code, error says “src cannot be read of undefined” about var z = imgarres[0].src; When I remove src from that line, extension works without errors, but
imgprocess routine doesn’t return expected value! The expected value is imgurl, which is in src that I removed. It seems that the second for loop (for (var p=0; p< imgarr.length; p++){) doesn’t run at all, but the first one is OK. How do I fix this?
P.S.: I tried passing callback like this: xhr.onreadystatechange = function(imgprocess) {
but it doesnt work. It says “uncaught typeerror” object is not a function.
it looks like you’re overwriting an array with a single element in the above code.
Added edit:
If the if condition is true,
imgels[i]is an element (with ansrc) but instead of adding imgels[i] to theimgarrarray, you are changingimgarrto point to a single element.Then in the second for loop, you are treating it as an array.
In fact, this is also a mistake in the second loop. Is
imgarressupposed to be an array or not? If it is thenimgarres = imgarr[p];is wrong (it points to an element after you do that). If it isn’t thenvar z = imgarres[0].src;is wrong (if it is an element you don’t need the[0]).Added edit:
does not add the element to the array!
does.
Added edit: just try this instead. Who knows, it might work…