From c3ce16b2de1a3faad41c97e9e4bc6384e9738528 Mon Sep 17 00:00:00 2001 From: Edward Bowden Date: Fri, 10 Jun 2016 13:29:03 +0100 Subject: [PATCH] Load Quotes in Background --- Clean Tab/js/eventpage.js | 33 +++++++++++++++++++++++++++++++++ Clean Tab/js/newtab.js | 35 ++++++----------------------------- Clean Tab/manifest.json | 2 +- 3 files changed, 40 insertions(+), 30 deletions(-) diff --git a/Clean Tab/js/eventpage.js b/Clean Tab/js/eventpage.js index 317e90d..7a28931 100644 --- a/Clean Tab/js/eventpage.js +++ b/Clean Tab/js/eventpage.js @@ -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; @@ -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+/="; diff --git a/Clean Tab/js/newtab.js b/Clean Tab/js/newtab.js index a6989c9..8449749 100644 --- a/Clean Tab/js/newtab.js +++ b/Clean Tab/js/newtab.js @@ -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(); + }; + }); } } } \ No newline at end of file diff --git a/Clean Tab/manifest.json b/Clean Tab/manifest.json index e1406ef..8448b58 100644 --- a/Clean Tab/manifest.json +++ b/Clean Tab/manifest.json @@ -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": {