Skip to content

Commit

Permalink
Retrieve random image id from background page.
Browse files Browse the repository at this point in the history
  • Loading branch information
mscavnicky committed Mar 6, 2016
1 parent fe114dd commit 8ea600e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
18 changes: 17 additions & 1 deletion background.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ function parsedBlocklist(blocklist) {
});
}

function randomImageId() {
var dayNumber = new Date().getTime() / (1000 * 60 * 60 * 24);

// This is a fallback in case image list was not loaded yet.
if (_.isEmpty(imageList)) {
return 0;
} else {
// To prevent change of image when imageList grows, we se the amount
// of maximum images on Unsplash to 950. There 974 of them at the moment
// of writing.
var randomIndex = (99991 * Math.floor(dayNumber)) % 950;
return imageList[randomIndex].id;
}
}

loadImageList();

// Load the options from storage.
Expand All @@ -74,6 +89,7 @@ chrome.storage.onChanged.addListener(function(changes) {
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
sendResponse({
'blocked': blocked,
'unblock': unblock
'unblock': unblock,
'randomImageId': randomImageId
}[message.subject](message));
});
9 changes: 5 additions & 4 deletions content.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ function block() {
request.send();

if (request.status === 200) {
var dayNumber = new Date().getTime() / (1000 * 60 * 60 * 24);
var randomId = (99991 * Math.floor(dayNumber)) % 900;
var html = request.responseText.replace(/{{imageId}}/g, randomId);
document.all[0].innerHTML = html;
var message = { subject: "randomImageId" };
chrome.runtime.sendMessage(message, function(imageId) {
var html = request.responseText.replace(/{{imageId}}/g, imageId);
document.all[0].innerHTML = html;
});
}

// Start the countdown
Expand Down

0 comments on commit 8ea600e

Please sign in to comment.