Skip to content

Commit

Permalink
Only load from ajax if not stored
Browse files Browse the repository at this point in the history
  • Loading branch information
ripexz committed Aug 25, 2014
1 parent 7767125 commit 612cf05
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
*.min.js
*.zip
node_modules
build
17 changes: 10 additions & 7 deletions src/badge.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
function updateBadge(data) {
var matchObj = JSON.parse(data.target.responseText);
var str_num = matchObj.matches.length + '';
chrome.browserAction.setBadgeText({text:str_num});
function saveData(data) {
var matchObj = JSON.parse(data.target.responseText),
countStr = matchObj.matches.length + '';
chrome.browserAction.setBadgeText({text: countStr});

matchObj.valid = true;
chrome.storage.local.set({'dota2matches': matchObj});
}
function getMatchCount() {
function getMatchData() {
var req = new XMLHttpRequest();
req.open("GET", 'http://eyeur.com/dota2/get_json.php', true);
req.onload = updateBadge.bind(this);
req.onload = saveData.bind(this);
req.send(null);
}

Expand All @@ -22,7 +25,7 @@ function addAlarm() {
}
chrome.alarms.onAlarm.addListener(function(alarm) {
if (alarm.name == "dota2betalert") {
getMatchCount();
getMatchData();
}
});
chrome.runtime.onStartup.addListener(function() {
Expand Down
28 changes: 23 additions & 5 deletions src/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,34 @@ var d2betalert = {
reqUrl: 'http://eyeur.com/dota2/get_json.php',

getData: function() {
chrome.storage.local.get('dota2matches', function(matchObj){
if (matchObj.valid) {

}
else {
this.getDataViaAjax();
}
});
},

getDataViaAjax: function() {
var req = new XMLHttpRequest();
req.open("GET", this.reqUrl, true);
req.onload = this.listMatches.bind(this);
req.onload = this.renderMatches.bind(this);
req.send(null);
},

listMatches: function(data) {
var matchObj = JSON.parse(data.target.responseText);
if ( matchObj.matches && matchObj.matches.length > 0 ) {
for ( var i = 0; i < matchObj.matches.length; i++ ) {
renderMatches: function(data) {
var matchObj;
if (data.valid) {
matchObj = data;
}
else {
matchObj = JSON.parse(data.target.responseText);
}

if (matchObj.matches && matchObj.matches.length > 0) {
for (var i = 0; i < matchObj.matches.length; i++) {
var p = document.createElement('p');
p.id = matchObj.matches[i].id + '';
p.innerHTML = '<span class="t1">'+matchObj.matches[i].team1+'</span><span class="vs">vs</span><span class="t2">'+matchObj.matches[i].team2+'</span>';
Expand Down

0 comments on commit 612cf05

Please sign in to comment.