SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Firefox Toolbar/Browser Interaction & Javascript

    I'm working on a toolbar add-on for firefox. It has a series of buttons which are updated each time the browser loads a different web site. For example, if you go to cnn.com, the 'Rating' button on the toolbar will change from a blank image to an image of 1 to 5 stars, depending on the site's rating in the db, and its oncommand behavior is set to pop up an alert box with the number of people who rated the site.

    I'm having three problems with the toolbar:

    First, the function only automatically fires once when the browser is first opened. It should fire on every subsequent page change, but it does not. The correct information is still successfully retrieved from the db when I click a button that manually calls the function, but I can't figure out how to get it to fire on its own each time. This probably isn't working because unlike a webpage with an onload or similar attribute, the toolbar is only loaded when the browser starts up, so the addEventListener is only ever used once per browser session. Anyone with knowledge of scripting add-ons for Firefox have any ideas?

    The second problem is that this one initial automated call seems to be firing before the page is loaded and so it returns the default 'no info found' data, even for sites which are in the db. Maybe addEventListener isn't the way to go? How would I hold the firing of the main function (showInfo) until the page has completely loaded? This doesn't seem specific to toolbar scripting...

    The third problem is that the oncommand alert popup shows the same info as in the initial call no matter what page I'm on or how many other pages I've gone to since I opened the browser. Using the button to manually call the function changes the button images correctly, and retrieves the right info for the popups, but they still show the info from the initial call when the buttons are actually clicked. This has been a problem even before I tried to get the script to run automatically.

    Code:
    window.addEventListener("load",showInfo,false);
    var xmlHttp;
    
    function showInfo() {
     var currenturl = "";
     var responseArray = new Array();
     var responseTexts = "";
     xmlHttp=GetXmlHttpObject();
     if (xmlHttp==null) {
      alert ("Browser does not support HTTP Request");
      return;
     }
     currenturl = window._content.location.href;
     var url="http://something/blah/website/getinfo.php";
     url=url+"?site="+currenturl;
     url=url+"&sid="+Math.random();
     xmlHttp.onreadystatechange=stateChanged;
     xmlHttp.open("GET",url,true);
     xmlHttp.send(null);
    }
    
    function stateChanged() {
     if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
      responseTexts = xmlHttp.responseText;
      responseArray = responseTexts.split(",");
      // for debugging:
      alert(responseTexts);
      if (responseArray[1]=='1') {
       document.getElementById('Button3').style.listStyleImage = "url('chrome://toolbar/skin/rating.png');";
       document.getElementById('Button3').setAttribute('oncommand','alert("Number of ratings: '+responseArray[5]+'.");');
      } else {
       document.getElementById('Button3').style.listStyleImage = "url('chrome://toolbar/skin/notrated.png');";
       document.getElementById('Button3').setAttribute('oncommand','alert("This site has not been rated.");');
      }
    
     }
    }
    
    function GetXmlHttpObject() {
     ...stuff...
    }
    Anyway, any help on any of these problems would be very much appreciated.

  2. #2
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I got a solution for the first two problems... they were indeed related, I just needed to be using 'pageshow' rather than 'load' in the first line of my javascript. it is apparently a very under-documented event, according to people in the mozilla extdev irc forum.

    the other problem, however... still no clues...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •