From 65e70145cbf784691fc318d0d3a24b0f97f435cc Mon Sep 17 00:00:00 2001 From: Kevin Candelaria Date: Thu, 3 Aug 2017 17:06:14 +0800 Subject: [PATCH 1/2] Add customizable and randomized background through option --- js/background.js | 32 ++++++++++++++++++++++++-------- js/options.js | 4 +++- js/waitblock.js | 4 +++- options.html | 14 ++++++++++++++ 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/js/background.js b/js/background.js index 47b29a7..3327a5f 100644 --- a/js/background.js +++ b/js/background.js @@ -5,6 +5,7 @@ var unblockTimes = {}; // Default URL for the background image. Should always load. DEFAULT_IMAGE_URL = 'https://source.unsplash.com/collection/323879/1440x900/daily'; + // Cached URL or the last displayed image. var imageUrl = DEFAULT_IMAGE_URL; @@ -73,18 +74,33 @@ function blockingHtml() { return request.responseText.replace(/{{imageUrl}}/g, imageUrl); } +function hasCustomBackground() { + var userBackgrounds = window.options.background.split(','); + return userBackgrounds.length > 0; +} + +function randomUserBackground() { + var userBackgrounds = window.options.background.split(','); + var randomUserBackground = userBackgrounds[Math.floor(Math.random()*userBackgrounds.length)] + return randomUserBackground; +} + // The default image URL we are currently using, redirects to the actual URL // of the actual image. This is slow and destroys user experience. Therefore we // do cache the URL of the actual image in a global variable. function refreshImageUrl() { - var request = new XMLHttpRequest(); - request.onreadystatechange = function() { - if (request.status === 200) { - imageUrl = request.responseURL; - } - }; - request.open('GET', DEFAULT_IMAGE_URL) - request.send(); + if (hasCustomBackground()) { + imageUrl = randomUserBackground(); + } else { + var request = new XMLHttpRequest(); + request.onreadystatechange = function() { + if (request.readyState === XMLHttpRequest.DONE && request.status === 200) { + imageUrl = request.responseURL; + } + }; + request.open('GET', imageUrl) + request.send(); + } } // Fetch image URL on startup to speed up the first image load. diff --git a/js/options.js b/js/options.js index 814fe1d..501217e 100644 --- a/js/options.js +++ b/js/options.js @@ -3,7 +3,8 @@ function saveOptions() { waitTime: $('waitTime').value, blockTime: $('blockTime').value, blocklist: $('blocklist').value, - whitelist: $('whitelist').value + whitelist: $('whitelist').value, + background: $('background').value }; chrome.storage.sync.set(options, function() { @@ -18,6 +19,7 @@ function restoreOptions() { $('blockTime').value = items.blockTime; $('blocklist').value = items.blocklist; $('whitelist').value = items.whitelist; + $('background').value = items.background; // Setting value does not trigger click oninput event. $('waitTimeOutput').value = humanizeTime(items.waitTime); diff --git a/js/waitblock.js b/js/waitblock.js index cb79ddd..cf7c2cc 100644 --- a/js/waitblock.js +++ b/js/waitblock.js @@ -7,5 +7,7 @@ var defaultOptions = { // The list of domains to block. Covers subdomains as well. blocklist: 'facebook.com', // The list of subdomains to allow. - whitelist: 'l.facebook.com' + whitelist: 'l.facebook.com', + // The list of background URLs to randomize from. + background: '' }; diff --git a/options.html b/options.html index 43c071f..2db4b99 100644 --- a/options.html +++ b/options.html @@ -91,6 +91,20 @@ +
+
+
+ + Comma separated list of background URLs to randomize from (e.g. + https://i.imgur.com/zu5Nno5.jpg) + +
+ +
+ +
+
+
From d405fd5298a7666593317b780e982db61178bb64 Mon Sep 17 00:00:00 2001 From: Kevin Candelaria Date: Thu, 3 Aug 2017 17:49:18 +0800 Subject: [PATCH 2/2] Fix default image url not being used when no custom background was defined --- js/background.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/background.js b/js/background.js index 3327a5f..87a75d3 100644 --- a/js/background.js +++ b/js/background.js @@ -76,7 +76,7 @@ function blockingHtml() { function hasCustomBackground() { var userBackgrounds = window.options.background.split(','); - return userBackgrounds.length > 0; + return userBackgrounds.length > 0 && userBackgrounds[0] != ""; } function randomUserBackground() {