Skip to content

Commit

Permalink
Show all pictures at least once
Browse files Browse the repository at this point in the history
  • Loading branch information
olibu committed Mar 12, 2023
1 parent 20feab3 commit 07ed392
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
4 changes: 1 addition & 3 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
### New Features:
- New Option for sequential or sequential image order
- Iteration through more images
- Complete image list is updated only once a day
- Show each picture at least one time

### Bugfixes:
- none
22 changes: 15 additions & 7 deletions js/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ async function loadOptions() {
lastUpdate: -1,
interval: 60,
random: false,
lastPos: -1
lastPos: -1,
maxPos: 0
});
}

Expand All @@ -42,19 +43,21 @@ async function updateCache(forceUpdate = false, forceUrlUpdate = false) {
return;
}

// update the image list once a day
if (forceUrlUpdate || options.lastUrlUpdate === -1 || options.lastUrlUpdate + 1000 * 60 * 60 * 24 < new Date().getTime()) {
// update the URL list
console.log('updating URL cache');
try {
const images = await getImages();
options.imgUrl = images;
options.imgUrlPos = 0;
options.lastUrlUpdate = new Date().getTime();
options.maxPos = MAX_IMAGES;

chrome.storage.local.set({
imgUrl: options.imgUrl,
imgUrlPos: options.imgUrlPos,
imgUrl: images,
imgUrlPos: 0,
lastUrlUpdate: options.lastUrlUpdate,
maxPos: MAX_IMAGES,
});
} catch (e) {
console.log('Not possible to update URL cache', e);
Expand All @@ -64,9 +67,10 @@ async function updateCache(forceUpdate = false, forceUrlUpdate = false) {
console.log('updating image cache');

try {
// update the image
// update the image cache
// console.log('maxPos', options.maxPos);
let overflow = 0;
for (let pos=0; pos < MAX_IMAGES; pos++) {
for (let pos=0; pos <= options.maxPos; pos++) {
let imagePos = options.imgUrlPos + pos;
if (imagePos>=options.imgUrl.length) {
// start at the on top of the image url list
Expand All @@ -82,7 +86,7 @@ async function updateCache(forceUpdate = false, forceUrlUpdate = false) {
}
}

options.imgUrlPos += MAX_IMAGES;
options.imgUrlPos += options.maxPos;
if (overflow>0) {
options.imgUrlPos = overflow;
}
Expand All @@ -98,10 +102,14 @@ async function updateCache(forceUpdate = false, forceUrlUpdate = false) {
img: options.img,
imgUrlPos: options.imgUrlPos,
lastUpdate: new Date().getTime(),
maxPos: 0,
lastPos: -1,
});

// reset current position to start iteration at firt image
options.lastPos = -1;
options.maxPos = 0;

} catch (e) {
console.log('Not possible to update cache', e);
}
Expand Down
7 changes: 7 additions & 0 deletions js/newtab.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ async function setBackgroundImage() {
const backgroundField = document.getElementById('background');
const authorField = document.getElementById('author');
if (options.img.length > 0) {
// console.log('lastPos', options.lastPos);
options.lastPos++;
if (options.lastPos >= options.img.length) {
options.lastPos = 0;
}

// set the max pos to show each image once at least
if (options.lastPos >= options.maxPos) {
options.maxPos = options.lastPos + 1;
}

if (options.random) {
let pos = Math.floor(Math.random() * options.img.length);
// ensure that the background changes in case of reload
Expand All @@ -30,6 +36,7 @@ async function setBackgroundImage() {
authorField.href = 'https://500px.com' + options.img[options.lastPos].link;
chrome.storage.local.set({
lastPos: options.lastPos,
maxPos: options.maxPos,
});
} else {
console.log('using fallback image');
Expand Down

0 comments on commit 07ed392

Please sign in to comment.