Skip to content

Commit

Permalink
Load Quotes in Background
Browse files Browse the repository at this point in the history
  • Loading branch information
beatles1 committed Jun 10, 2016
1 parent f79adf5 commit c3ce16b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
33 changes: 33 additions & 0 deletions Clean Tab/js/eventpage.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
chrome.runtime.onInstalled.addListener(function(details){
if(details.reason == "install"){
localStorage.setItem("quote", "Welcome to Clean Tab!");
}
});

function cacheUnsplash(w, h) {
if (window.navigator.onLine && window.XMLHttpRequest && w && h) {
var xmlhttp;
Expand All @@ -13,6 +19,33 @@ function cacheUnsplash(w, h) {
}
}

function loadQuote() {
var d = new Date();
var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var data = xmlhttp.responseText;

var quoteJSON = JSON.parse(data);
if (typeof quoteJSON["error"] === 'undefined') {

var quote = '"'+ quoteJSON["quoteText"] +'"';
if (quoteJSON["quoteAuthor"] && quoteJSON["quoteAuthor"] != "") {
quote += ' - '+ quoteJSON["quoteAuthor"];
}
localStorage.setItem("quote", quote);

d.setHours(23,0,0,0);
localStorage.setItem("quoteTime", d.getTime());
}
}
}
xmlhttp.open("GET","https://beatles1-forismatic-quotes-v1.p.mashape.com/?method=getQuote&format=json&lang=en", true);
xmlhttp.setRequestHeader("X-Mashape-Authorization", "K0tj7GVDTJmshC0R86WSEtc9oMNUp1KOCL6jsnYrlynLRg7NqW");
xmlhttp.send();
}

function encode64(inputStr)
{
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
Expand Down
35 changes: 6 additions & 29 deletions Clean Tab/js/newtab.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,38 +149,15 @@ window.onload = function() {
}

if (useQuote) {
displayQuote();
var d = new Date();
var quoteTime = localStorage.getItem("quoteTime");
if ((!quoteTime || quoteTime < d.getTime()) && window.navigator.onLine && window.XMLHttpRequest) {
var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var data = xmlhttp.responseText;

var quoteJSON = JSON.parse(data);
if (typeof quoteJSON["error"] === 'undefined') {

var quote = '"'+ quoteJSON["quoteText"] +'"';
if (quoteJSON["quoteAuthor"] && quoteJSON["quoteAuthor"] != "") {
quote += ' - '+ quoteJSON["quoteAuthor"];
}
localStorage.setItem("quote", quote);

displayQuote();

d.setHours(24,0,0,0);
localStorage.setItem("quoteTime", d.getTime());
} else {
displayQuote();
}
}
}
xmlhttp.open("GET","https://beatles1-forismatic-quotes-v1.p.mashape.com/?method=getQuote&format=json&lang=en", true);
xmlhttp.setRequestHeader("X-Mashape-Authorization", "K0tj7GVDTJmshC0R86WSEtc9oMNUp1KOCL6jsnYrlynLRg7NqW");
xmlhttp.send();
} else {
displayQuote();
chrome.runtime.getBackgroundPage(function(bp) {
if (bp) {
bp.loadQuote();
};
});
}
}
}
2 changes: 1 addition & 1 deletion Clean Tab/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "Clean Tab - New Tab Page",
"short_name": "Clean Tab",
"description": "New Tab page replacement with beautiful photography.",
"version": "0.41",
"version": "0.42",
"offline_enabled": true,

"chrome_url_overrides": {
Expand Down

0 comments on commit c3ce16b

Please sign in to comment.