From f54058e8f316cae201395807c2f27d8bba0b48e0 Mon Sep 17 00:00:00 2001 From: Travis Lawrence Date: Wed, 14 Aug 2024 23:18:29 -0700 Subject: [PATCH 1/5] think its working --- manifest.json | 2 +- public/scripts/application.js | 35 +++++++++++++++++++----------- public/scripts/background.js | 12 ++++++++-- public/scripts/options.js | 10 ++++----- public/scripts/utilities/helper.js | 8 +++++++ 5 files changed, 46 insertions(+), 21 deletions(-) diff --git a/manifest.json b/manifest.json index bf7b8fb..5e4e885 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { "name": "Mouse Odometer", - "version": "0.9.2", + "version": "0.9.5", "manifest_version": 3, "description": "Keep track of the distance your mouse has moved!", "web_accessible_resources": [ diff --git a/public/scripts/application.js b/public/scripts/application.js index 114ac63..16860b3 100644 --- a/public/scripts/application.js +++ b/public/scripts/application.js @@ -37,7 +37,7 @@ const getStorage = (cb) => { chrome.storage.sync.get( - ["currentDistance", "showOdometer", "currentDate"], + ["currentDistance", "showOdometer", "currentDate", "totalDistance"], cb ); }; @@ -45,6 +45,7 @@ class MouseOdometer { constructor() { this.currentDistance = 0; + this.totalDistance = 0; this.lastMove = { x: 0, y: 0 }; this.throttledUpdate = throttle(this.updateStorage, STORAGE_UPDATE_DELAY); getStorage(this.buildOdometerWrapper.bind(this)); @@ -54,6 +55,7 @@ buildOdometerWrapper(options) { this.currentDistance = options.currentDistance || this.currentDistance || 0; + this.totalDistance = options.totalDistance || this.totalDistance || 0; if (options.showOdometer) { this.odometerWrapper = document.createElement("div"); @@ -81,6 +83,7 @@ const dy = Math.abs(oldY - newY); const move = Math.sqrt(dx ** 2 + dy ** 2); this.currentDistance += move; + this.totalDistance += move; this.throttledUpdate(); this.renderDistance(); this.lastMove = { x: newX, y: newY }; @@ -104,19 +107,25 @@ // Sends distance to chrome.storage updateStorage() { chrome.runtime - .sendMessage({ latestDistance: this.currentDistance }, (response) => { - if (!response) { - return; + .sendMessage( + { + latestDistance: this.currentDistance, + totalDistance: this.totalDistance, + }, + (response) => { + if (!response) { + return; + } + + if (response?.isNewDay) { + this.currentDistance = 0; + } + const currentTier = response.currentTier; + this.odometerWrapper?.classList.add( + `odometer-text-color-${currentTier.background}` + ); } - - if (response?.isNewDay) { - this.currentDistance = 0; - } - const currentTier = response.currentTier; - this.odometerWrapper?.classList.add( - `odometer-text-color-${currentTier.background}` - ); - }) + ) ?.bind(this); } } diff --git a/public/scripts/background.js b/public/scripts/background.js index 76376e4..ae4b39b 100644 --- a/public/scripts/background.js +++ b/public/scripts/background.js @@ -18,11 +18,19 @@ chrome.runtime.onMessage.addListener((request, _, sendResponse) => { request.latestDistance > settings.currentDistance && !settings.isNewDay ? request.latestDistance : settings.currentDistance; + const newTotalDistance = + request.totalDistance > settings.totalDistance + ? request.totalDistance + : settings.totalDistance; const currentTier = findTier(newDistance); const iconPath = currentTier.path; chrome.action.setIcon({ path: { 128: iconPath } }); - setStorage({ ...settings, currentDistance: newDistance }); - sendResponse({ ...settings, currentTier }); + setStorage({ + ...settings, + currentDistance: newDistance, + totalDistance: newTotalDistance, + }); + sendResponse({ ...settings, currentTier, totalDistance: newTotalDistance }); }); return true; }); diff --git a/public/scripts/options.js b/public/scripts/options.js index 4ff638a..ed3709f 100644 --- a/public/scripts/options.js +++ b/public/scripts/options.js @@ -49,7 +49,8 @@ const pixelConversion = [ ]; export const updateDisplay = ({ options, date }) => { - const { currentDistance, previousDistances, maxDistance } = options; + const { currentDistance, previousDistances, maxDistance, totalDistance } = + options; const distance = date === "today" @@ -88,9 +89,8 @@ showOdometerCheckbox.addEventListener("change", (event) => { setStorage({ showOdometer: event.target.checked }); }); -const calcTotalDistance = (prevDistances, currentDistance) => { - const total = sumDistances(prevDistances); - totalDistanceCalculated = Math.round(total + currentDistance); +const calcTotalDistance = (totalDistance) => { + totalDistanceCalculated = totalDistance; toggleTotalDistanceConversions(); }; @@ -99,7 +99,7 @@ getStorage((options) => { buildHistory(options); } conversionIndex = options.conversionIndex || 0; - calcTotalDistance(options.previousDistances, options.currentDistance); + calcTotalDistance(options.totalDistance); const currentTier = findTier(options.currentDistance); updateIcon(currentTier.path); odometerContainer.classList.add(`background-${currentTier.background}`); diff --git a/public/scripts/utilities/helper.js b/public/scripts/utilities/helper.js index 902385e..6a9077b 100644 --- a/public/scripts/utilities/helper.js +++ b/public/scripts/utilities/helper.js @@ -6,6 +6,7 @@ export const SETTING_VALUES = [ "previousDistances", "conversionIndex", "maxDistance", + "totalDistance", ]; const WHITE = "white"; @@ -60,6 +61,7 @@ export const formatDate = (date) => { }; const DEFAULT_VALUES = { + totalDistance: 0, showOdometer: true, currentDistance: 0, currentDate: formatDate(new Date()), @@ -101,6 +103,11 @@ export const buildSettings = (options) => { options.previousDistances?.slice(`-${MAX_DAY_HISTORY}`) || DEFAULT_VALUES.previousDistances; + const totalDistance = + options.totalDistance > 0 + ? options.totalDistance + : sumDistances(previousDistances) + currentDistance || + DEFAULT_VALUES.totalDistance; const defaultMaxDist = { date, distance: currentDistance }; const previousMaxDist = findMaxDistance(previousDistances); @@ -135,6 +142,7 @@ export const buildSettings = (options) => { previousDistances, isNewDay, maxDistance, + totalDistance, }; }; From 1cddb53dcc8eaae5ee81b826b769c2bdb9b06118 Mon Sep 17 00:00:00 2001 From: Travis Lawrence Date: Wed, 14 Aug 2024 23:24:59 -0700 Subject: [PATCH 2/5] cleanup --- public/scripts/options.js | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/public/scripts/options.js b/public/scripts/options.js index ed3709f..4400652 100644 --- a/public/scripts/options.js +++ b/public/scripts/options.js @@ -5,7 +5,6 @@ import { APPLICATION_CLASSNAME, getFormattedDate, findAvgDistance, - sumDistances, } from "./utilities/helper.js"; import { updateIcon, buildHistory } from "./utilities/historyGraph.js"; @@ -73,14 +72,13 @@ export const updateDisplay = ({ options, date }) => { } }; -let totalDistanceCalculated = 0; let conversionIndex = 0; -const toggleTotalDistanceConversions = () => { +const toggleTotalDistanceConversions = (totalDistanceValue) => { const conversion = pixelConversion[conversionIndex % pixelConversion.length]; setStorage({ conversionIndex }); conversionIndex++; totalDistance.textContent = `${( - totalDistanceCalculated / conversion.pixels + totalDistanceValue / conversion.pixels ).toLocaleString()}${conversion.label}!`; }; @@ -89,17 +87,12 @@ showOdometerCheckbox.addEventListener("change", (event) => { setStorage({ showOdometer: event.target.checked }); }); -const calcTotalDistance = (totalDistance) => { - totalDistanceCalculated = totalDistance; - toggleTotalDistanceConversions(); -}; - getStorage((options) => { if (options.previousDistances) { buildHistory(options); } conversionIndex = options.conversionIndex || 0; - calcTotalDistance(options.totalDistance); + toggleTotalDistanceConversions(options.totalDistance); const currentTier = findTier(options.currentDistance); updateIcon(currentTier.path); odometerContainer.classList.add(`background-${currentTier.background}`); From 4af36ef4b3f647d69b216fad937ff0fbb64e8039 Mon Sep 17 00:00:00 2001 From: Travis Lawrence Date: Wed, 14 Aug 2024 23:28:20 -0700 Subject: [PATCH 3/5] should be a class in options --- public/scripts/options.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/public/scripts/options.js b/public/scripts/options.js index 4400652..460ecc8 100644 --- a/public/scripts/options.js +++ b/public/scripts/options.js @@ -72,13 +72,14 @@ export const updateDisplay = ({ options, date }) => { } }; +let totalDistanceCalculated = 0; let conversionIndex = 0; -const toggleTotalDistanceConversions = (totalDistanceValue) => { +const toggleTotalDistanceConversions = () => { const conversion = pixelConversion[conversionIndex % pixelConversion.length]; setStorage({ conversionIndex }); conversionIndex++; totalDistance.textContent = `${( - totalDistanceValue / conversion.pixels + totalDistanceCalculated / conversion.pixels ).toLocaleString()}${conversion.label}!`; }; @@ -92,7 +93,8 @@ getStorage((options) => { buildHistory(options); } conversionIndex = options.conversionIndex || 0; - toggleTotalDistanceConversions(options.totalDistance); + totalDistanceCalculated = options.totalDistance; + toggleTotalDistanceConversions(); const currentTier = findTier(options.currentDistance); updateIcon(currentTier.path); odometerContainer.classList.add(`background-${currentTier.background}`); From da10deacba2c3edb4ebbafcdde69468951716692 Mon Sep 17 00:00:00 2001 From: Travis Lawrence Date: Wed, 14 Aug 2024 23:32:29 -0700 Subject: [PATCH 4/5] round --- public/scripts/options.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/scripts/options.js b/public/scripts/options.js index 460ecc8..b28e628 100644 --- a/public/scripts/options.js +++ b/public/scripts/options.js @@ -93,7 +93,7 @@ getStorage((options) => { buildHistory(options); } conversionIndex = options.conversionIndex || 0; - totalDistanceCalculated = options.totalDistance; + totalDistanceCalculated = Math.round(options.totalDistance); toggleTotalDistanceConversions(); const currentTier = findTier(options.currentDistance); updateIcon(currentTier.path); From 2d3f49ef9830f76051e026ab59c29905d8e98155 Mon Sep 17 00:00:00 2001 From: Travis Lawrence Date: Wed, 14 Aug 2024 23:35:01 -0700 Subject: [PATCH 5/5] default to 0 --- public/scripts/options.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/public/scripts/options.js b/public/scripts/options.js index b28e628..3af8947 100644 --- a/public/scripts/options.js +++ b/public/scripts/options.js @@ -93,7 +93,9 @@ getStorage((options) => { buildHistory(options); } conversionIndex = options.conversionIndex || 0; - totalDistanceCalculated = Math.round(options.totalDistance); + totalDistanceCalculated = options.totalDistance + ? Math.round(options.totalDistance) + : 0; toggleTotalDistanceConversions(); const currentTier = findTier(options.currentDistance); updateIcon(currentTier.path);