DrStrangeLove
DrStrangeLove

Reputation: 11557

How do i solve these issues?

I wrote simplest extension as an exercise in JS coding. This extension checks if some user (of certain social network) is online, and then outputs his/her small image, name and online status in notification alert. It checks profile page every 2 minutes via (setTimeout), but when user becomes "online", i set setTimeout to 45 minutes.(to avoid online alerts every 2 minutes).

It works, but not exactly as i expected. I have 2 issues:

1)When certain user is online and i change user id (via options page) to check another one, it doesnt happen because it waits 45 or less minutes. i tried the following code (in options.html), but it doesnt help.

2)When i change users, image output doesnt work correctly!! It outputs image of previous user!!

How do i fix these problems?? Thanks!

options.html

<script>

  onload = function() {
  if (localStorage.id){
    document.getElementById("identifier").value = localStorage.id;
  }

  else {
    var el = document.createElement("div");
    el.innerHTML = "Enter ID!!";
    document.getElementsByTagName("body")[0].appendChild(el);

  }

  };

  function onch(){

localStorage.id = document.getElementById("identifier").value;
var bg = chrome.extension.getBackgroundPage();
if(bg.id1){
clearTimeout(bg.id1);
bg.getdata();
}



  }
</script>
<body>
<h1>

</h1>
<form id="options">
<h2>Settings</h2>


    <label><input type='text' id ='identifier' value='' onchange="onch()"> Enter ID </label>
</form>
</body>
</html>

backg.html

<script type="text/javascript">
var domurl = "http://www.xxxxxxxxxxxxxx.xxx/id";


var txt;
var id1;
var id2;
var imgarres = [];
var imgarr = [];
var imgels = [];


function getdata() {
    if (id1){clearTimeout(id1);}
    if (id2){clearTimeout(id2);}

    var url = getUrl();

    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,url);     
        var nam = el.getElementsByTagName("title")[0].innerHTML;
            if (r != -1) {
            var notification = webkitNotifications.createNotification(n,  nam, 'online!!' );
            notification.show();
            var id1 = setTimeout(getdata, 60000*45);


            }
           else {

            var id2 = setTimeout(getdata, 60000*2);
           }


    }}

    xhr.send();

}


function imgprocess(text,url){
 imgels = text.getElementsByTagName("IMG");
for (var i=0;i< imgels.length;i++){
 if (imgels[i].src.indexOf(parse(url)) != -1){
    imgarr.push(imgels[i]);


 }


}
for (var p=0; p< imgarr.length; p++){
 if (imgarr[p].parentNode.nodeName=="A"){
  imgarres.push(imgarr[p]);


}
} 
var z = imgarres[0].src;

return z; 
}

function getUrl(){
if (localStorage.id){
var ur = domurl + localStorage.id;
return ur;  
}
else {
var notif = webkitNotifications.createNotification(null, 'blah,blah,blah', 'Enter ID in options!!' );
notif.show();   
getdata();
}
}


function init() {

 getdata();


}
</script>
</head>
<body onload="init();">

</body>
</html>

Upvotes: 0

Views: 142

Answers (1)

serg
serg

Reputation: 111265

In options instead of clearTimeout(bg.id1); try bg.clearTimeout(bg.id1);

For image problem looks like you never clean imgarres array, only adding elements to it and then taking the first one.

PS. You code is very hard to read, maybe if you made it well formatted and didn't use cryptic variable names you would be able to find bugs easier.

UPDATE

I think I know what the problem is. When you are setting the timeout you are using local scope variable because of var keyword, so your id1 is visible only inside this function and global id1 is still undefined. So instead of:

var id1 = setTimeout(getdata, 60000*45);

try:

id1 = setTimeout(getdata, 60000*45);

Because of this if(bg.id1){} inside options is never executed.

(bg.clearTimeout(bg.id1); should work after that, but it is not needed as you are clearing the timeout inside getdata() anyway)

Upvotes: 1

Related Questions