Created
June 12, 2012 09:10
-
-
Save liquidz/2916372 to your computer and use it in GitHub Desktop.
Notify me OAuth2.0 draft updates every morning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Initializing variables | |
var dayBaseTime = 5; | |
var oauthDraftUrl = 'http://tools.ietf.org/wg/oauth/draft-ietf-oauth-v2/'; | |
// End of variables initializing | |
var isNull = function(x){ | |
return (x === null || x === undefined); | |
}; | |
var trim = function(str){ | |
return str.replace(/(^\s*|\s*$)/, ''); | |
}; | |
var getToday = function(date){ | |
var today = new Date() | |
, year = today.getFullYear() | |
, month = today.getMonth() + 1 | |
, day = today.getDate() | |
, hour = today.getHours() | |
; | |
if(hour < dayBaseTime){ --day; } | |
return('' + year + month + day); | |
}; | |
var notify = function(title, body, fn){ | |
var n = device.notifications.createNotification(title); | |
if(! isNull(body)){ n.content = body; } | |
if(! isNull(fn)){ n.on('click', fn); } | |
n.show(); | |
}; | |
console.log("Started script: Notify me OAuth2.0 draft updates every morning."); | |
device.screen.on('unlock', function(){ | |
var lastUnlocked = device.localStorage.getItem('lastUnlocked'); | |
var lastDraft = device.localStorage.getItem('lastDraft'); | |
var today = getToday(); | |
if(isNull(lastUnlocked) || lastUnlocked !== today){ | |
device.ajax({ | |
url: oauthDraftUrl | |
, type: 'GET' | |
}, function(body, status, response){ | |
var lines = body.split(/[\r\n]+/); | |
var i = 0, len = lines.length; | |
var mode = 0; | |
for(i = 0; i < len; ++i){ | |
if(mode === 0){ | |
if(lines[i].indexOf('Working documents:') !== -1){ | |
++mode; | |
} | |
} else if(mode === 1){ | |
if(lines[i].indexOf('<td valign="top" class="date">') !== -1){ | |
break; | |
} | |
} | |
} | |
var latestUpdate = trim(lines[i].replace(/<.+?>/g, '')); | |
console.log("oauth draft compare: [" + lastDraft + "] [" + latestUpdate + "]"); | |
if(isNull(lastDraft) || latestUpdate !== lastDraft){ | |
notify("NEW OAUTH2.0 DRAFT EXISTS!!!", "Latest version: " + latestUpdate, function(){ | |
device.browser.launch(oauthDraftUrl); | |
}); | |
device.localStorage.setItem('lastDraft', latestUpdate); | |
} | |
}, function(status, response){ | |
console.error('error', 'error: ' + status); | |
}); | |
// set last unlocked date | |
device.localStorage.setItem('lastUnlocked', today); | |
} | |
}); | |
console.log("Completed script: Notify me OAuth2.0 draft updates every morning."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment