Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6161803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:32:25+00:00 2026-05-23T21:32:25+00:00

I wrote simplest extension as an exercise in JS coding. This extension checks if

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:32:25+00:00Added an answer on May 23, 2026 at 9:32 pm

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a windows service using VB that read some legacy data from Visual
I'm trying to do some processing on whether the user enters a (1) file
I wrote the simplest WYSIWYG in js/jquery: http://jsfiddle.net/XnSWF/ Works perfectly (eg. set to write
What is the simplest way to allow one user write access and everyone else
im writing a sample SDL program, and I just wrote the simplest program, but
I'm binding the Background property of a Canvas to some SelectedColorItem object like this:
In an interview for some company, I was asked this question. What design patterns
How can I write this in the simplest way using LINQ? SELECT MAX(Game_id) AS
I'm trying to learn Hibernate and I wrote the simplest Person Entity and I
I create a txt file that have 3 element and i wrote this code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.