From f79adf582b2e1a24193d8db571e2ce49a663cee8 Mon Sep 17 00:00:00 2001 From: Edward Bowden Date: Wed, 8 Jun 2016 22:21:56 +0100 Subject: [PATCH] Download Unsplash Images on EventPage --- Clean Tab/js/eventpage.js | 55 +++++++++++++++++++++++++++++++++++++ Clean Tab/js/newtab.js | 58 ++++----------------------------------- Clean Tab/manifest.json | 8 +++++- 3 files changed, 68 insertions(+), 53 deletions(-) create mode 100644 Clean Tab/js/eventpage.js diff --git a/Clean Tab/js/eventpage.js b/Clean Tab/js/eventpage.js new file mode 100644 index 0000000..317e90d --- /dev/null +++ b/Clean Tab/js/eventpage.js @@ -0,0 +1,55 @@ +function cacheUnsplash(w, h) { + if (window.navigator.onLine && window.XMLHttpRequest && w && h) { + var xmlhttp; + xmlhttp=new XMLHttpRequest(); + xmlhttp.onreadystatechange = function() { + if (xmlhttp.readyState==4 && xmlhttp.status==200) { + localStorage.setItem("unsplashCached", "data:image/jpeg;base64," + encode64(xmlhttp.responseText)); + } + } + xmlhttp.open("GET","https://source.unsplash.com/random/" + w + "x" + h, true); + xmlhttp.overrideMimeType('text/plain; charset=x-user-defined'); + xmlhttp.send(); + } +} + +function encode64(inputStr) +{ + var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + var outputStr = ""; + var i = 0; + + while (i> 2; + var enc2 = ((byte1 & 3) << 4) | (byte2 >> 4); + + var enc3, enc4; + if (isNaN(byte2)) + { + enc3 = enc4 = 64; + } + else + { + enc3 = ((byte2 & 15) << 2) | (byte3 >> 6); + if (isNaN(byte3)) + { + enc4 = 64; + } + else + { + enc4 = byte3 & 63; + } + } + + outputStr += b64.charAt(enc1) + b64.charAt(enc2) + b64.charAt(enc3) + b64.charAt(enc4); + } + + return outputStr; +} \ No newline at end of file diff --git a/Clean Tab/js/newtab.js b/Clean Tab/js/newtab.js index 496993d..a6989c9 100644 --- a/Clean Tab/js/newtab.js +++ b/Clean Tab/js/newtab.js @@ -71,7 +71,7 @@ window.onload = function() { var bgTypes = []; if (useUnsplashBGs && window.navigator.onLine && window.XMLHttpRequest) {bgTypes.push("unsplash");} - if (useDefaultBGs) {bgTypes.push("default");} + if (useDefaultBGs || !window.navigator.onLine) {bgTypes.push("default");} if (extraBGs && extraBGs.length > 0 && window.navigator.onLine) {bgTypes.push("extra");} var bgType = bgTypes[Math.floor(Math.random() * bgTypes.length)]; @@ -82,16 +82,11 @@ window.onload = function() { loadBG("https://source.unsplash.com/random/" + window.innerWidth + "x" + window.innerHeight); } - var xmlhttp; - xmlhttp=new XMLHttpRequest(); - xmlhttp.onreadystatechange = function() { - if (xmlhttp.readyState==4 && xmlhttp.status==200) { - localStorage.setItem("unsplashCached", "data:image/jpeg;base64," + encode64(xmlhttp.responseText)); - } - } - xmlhttp.open("GET","https://source.unsplash.com/random/" + window.innerWidth + "x" + window.innerHeight, true); - xmlhttp.overrideMimeType('text/plain; charset=x-user-defined'); - xmlhttp.send(); + chrome.runtime.getBackgroundPage(function(bp) { + if (bp) { + bp.cacheUnsplash(window.innerWidth, window.innerHeight); + }; + }); } else if (bgType == "default") { // Load Local Image loadBG(Math.floor((Math.random() * localBGs) + 1)); @@ -188,45 +183,4 @@ window.onload = function() { displayQuote(); } } -} - -function encode64(inputStr) -{ - var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; - var outputStr = ""; - var i = 0; - - while (i> 2; - var enc2 = ((byte1 & 3) << 4) | (byte2 >> 4); - - var enc3, enc4; - if (isNaN(byte2)) - { - enc3 = enc4 = 64; - } - else - { - enc3 = ((byte2 & 15) << 2) | (byte3 >> 6); - if (isNaN(byte3)) - { - enc4 = 64; - } - else - { - enc4 = byte3 & 63; - } - } - - outputStr += b64.charAt(enc1) + b64.charAt(enc2) + b64.charAt(enc3) + b64.charAt(enc4); - } - - return outputStr; } \ No newline at end of file diff --git a/Clean Tab/manifest.json b/Clean Tab/manifest.json index 0bee96b..e1406ef 100644 --- a/Clean Tab/manifest.json +++ b/Clean Tab/manifest.json @@ -4,12 +4,18 @@ "name": "Clean Tab - New Tab Page", "short_name": "Clean Tab", "description": "New Tab page replacement with beautiful photography.", - "version": "0.40", + "version": "0.41", "offline_enabled": true, "chrome_url_overrides": { "newtab": "newtab.html" }, + + "background": { + "scripts": ["js/eventpage.js"], + "persistent": false + }, + "icons": { "48": "icon48.png", "128": "icon128.png"