Skip to content

Commit

Permalink
Download Unsplash Images on EventPage
Browse files Browse the repository at this point in the history
  • Loading branch information
beatles1 committed Jun 8, 2016
1 parent 706bb9d commit f79adf5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 53 deletions.
55 changes: 55 additions & 0 deletions Clean Tab/js/eventpage.js
Original file line number Diff line number Diff line change
@@ -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<inputStr.length)
{
//all three "& 0xff" added below are there to fix a known bug
//with bytes returned by xhr.responseText
var byte1 = inputStr.charCodeAt(i++) & 0xff;
var byte2 = inputStr.charCodeAt(i++) & 0xff;
var byte3 = inputStr.charCodeAt(i++) & 0xff;

var enc1 = byte1 >> 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;
}
58 changes: 6 additions & 52 deletions Clean Tab/js/newtab.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)];

Expand All @@ -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));
Expand Down Expand Up @@ -188,45 +183,4 @@ window.onload = function() {
displayQuote();
}
}
}

function encode64(inputStr)
{
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var outputStr = "";
var i = 0;

while (i<inputStr.length)
{
//all three "& 0xff" added below are there to fix a known bug
//with bytes returned by xhr.responseText
var byte1 = inputStr.charCodeAt(i++) & 0xff;
var byte2 = inputStr.charCodeAt(i++) & 0xff;
var byte3 = inputStr.charCodeAt(i++) & 0xff;

var enc1 = byte1 >> 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;
}
8 changes: 7 additions & 1 deletion Clean Tab/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit f79adf5

Please sign in to comment.