Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / Javascript

Windows 7 Sidebar Gadget - CodeProject Reputation Watcher

Rate me:
Please Sign up or sign in to vote.
4.94/5 (49 votes)
22 Mar 2011CPOL8 min read 136.7K   5.1K   80  
Windows 7 Gadget that scrapes your reputation points and graph, and tracks changes between updates

/*************************
    Local Storage Data
**************************/
// -------------------------------------------------
// Retrieve the memberID
// -------------------------------------------------
function getMemberID() {
    var memberID = System.Gadget.Settings.readString("MemberID");

    if (!memberID)
    { memberID = "0"; }
    return memberID.toString();
}

// -------------------------------------------------
// Retrieve the stored Reputation Data
// -------------------------------------------------
function getPrevRepDetail() {
    var prevRep = new Array();
    prevRep[0] = System.Gadget.Settings.readString("repTotal");
    prevRep[1] = System.Gadget.Settings.readString("repAuthor");
    prevRep[2] = System.Gadget.Settings.readString("repAuthority");
    prevRep[3] = System.Gadget.Settings.readString("repDebator");
    prevRep[4] = System.Gadget.Settings.readString("repEditor");
    prevRep[5] = System.Gadget.Settings.readString("repEnquirer");
    prevRep[6] = System.Gadget.Settings.readString("repOrganiser");
    prevRep[7] = System.Gadget.Settings.readString("repParticipant");
    prevRep[8] = System.Gadget.Settings.readString("repDate");
    return prevRep;
}

// -------------------------------------------------
// Save the current Reputation to local storage
// -------------------------------------------------
function setNewRepDetail(repDetail) {
    var newRep = repDetail;
    System.Gadget.Settings.writeString("repTotal", newRep[0]);
    System.Gadget.Settings.writeString("repAuthor", newRep[1]);
    System.Gadget.Settings.writeString("repAuthority", newRep[2]);
    System.Gadget.Settings.writeString("repDebator", newRep[3]);
    System.Gadget.Settings.writeString("repEditor", newRep[4]);
    System.Gadget.Settings.writeString("repEnquirer", newRep[5]);
    System.Gadget.Settings.writeString("repOrganiser", newRep[6]);
    System.Gadget.Settings.writeString("repParticipant", newRep[7]);
    System.Gadget.Settings.writeString("repDate", newRep[8]);
}

/*************************
   CP Content Addresses
**************************/
// -------------------------------------------------
// Get the address of the Reputation Graph on CP
// -------------------------------------------------
function getCPRepGraph() {
    var repAddress = "http://www.codeproject.com/script/Reputation/ReputationGraph.aspx?mid=" + getMemberID();
    return repAddress;
}

// -------------------------------------------------
// Get the address of the member profile page
// -------------------------------------------------
function getCPMemberProfile() {
    var profileAddress = "http://www.codeproject.com/script/Membership/View.aspx?mid=" + getMemberID();
    return profileAddress;
}

// -------------------------------------------------
// Navigate to Members Profile on CodeProject
// -------------------------------------------------
function navProfile() {
    window.open(getCPMemberProfile());
}

// -------------------------------------------------
// Navigate to CodeProject
// -------------------------------------------------
function navCodeProject() {
    window.open("http://www.codeproject.com/");
}

/*************************
     CP Profile Data
**************************/
// -------------------------------------------------
// Used to calculate the changes between previous load and current load and set td content
// -------------------------------------------------
  function calcRepChange(oldRep, newRep) 
  {
      //Hold the diff
      var change = 0;

      for (x = 0; x < 8; x++) 
      {
          //Deal with the thousand sep e.g. 7,500  = 7500
          var newVal = newRep[x];newVal = removeThousandSep(newVal); 
          var oldVal = oldRep[x];oldVal = removeThousandSep(oldVal); 

          //change = parseInt(newRep[x]) - parseInt(oldRep[x]);
          change = parseInt(newVal) - parseInt(oldVal);

          if (change > 0)
          { setChangeHTML(x, "UP", change); }
          else {
              if (change < 0) {
                  setChangeHTML(x, "DOWN", change * -1);
              }
              else {
                  setChangeHTML(x, "NONE", 0);
              }
          }     
      }
  }

  // -------------------------------------------------
  // Write the html for the rep point change and valve
  // -------------------------------------------------
    function setChangeHTML(category, changeType, changeVal ) {

        changeVal = insertThousandSep(changeVal);

        var html ="";
        //if (changeType == "UP") { html = "<img src='images/up_green.png' alt='Up'>&nbsp;" + changeVal.toString(); }
        //if (changeType == "DOWN") {html = "<img src='images/down_red.png' alt='Down'>&nbsp;" + changeVal.toString();}
        //if (changeType == "NONE") { html = "<img src='images/no_change.png' alt='No Change'>&nbsp;" + changeVal.toString(); }
        if (changeType == "UP") { html = "+" + changeVal.toString(); }
        if (changeType == "DOWN") { html = "-" + changeVal.toString(); }
        if (changeType == "NONE") { html = "."; }

        var tag = "#repChange" + x.toString();

        $(tag).html(html);
    }

/*************************
 String Number Formatting
**************************/
    // -------------------------------------------------
    // Remove thousand seps e.g. 7,500 = 7500
    // -------------------------------------------------
    function removeThousandSep(input) {
        input = String(input);
        return input.replace(/,/g, '');
    }

    // -------------------------------------------------
    // Add thousand seps e.g. 7000 = 7,000
    // -------------------------------------------------
    function insertThousandSep(input) {
        input = String(input);
        var RegEX = /^(.*\s)?([-+\u00A3\u20AC]?\d+)(\d{3}\b)/
        return input == (input = input.replace(RegEX, "$1$2,$3")) ? input : insertThousandSep(input)
    }

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Scotland Scotland
I have been working in the Oil & Gas Industry for over 30 years now.

Core Discipline is Instrumentation and Control Systems.

Completed Bsc Honours Degree (B29 in Computing) with the Open University in 2012.

Currently, Offshore Installation Manager in the Al Shaheen oil field, which is located off the coast of Qatar. Prior to this, 25 years of North Sea Oil & Gas experience.

Comments and Discussions