From 9de32eca5472aad6a5976d35aa44e384183c7fe2 Mon Sep 17 00:00:00 2001 From: Dinesh Date: Sat, 26 Sep 2020 13:26:59 +0530 Subject: [PATCH 1/4] Fix the worthy bug to24hours too. We didn't notice this while fixing the previous one. There's some duplication here but I guess it's OK for now. --- src/helpers.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 13915fb..a69d4c7 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -5,8 +5,7 @@ export function getDateObject(timeObjectFromPicker) { if (hours !== 12) { hours += timeObjectFromPicker.amOrPm === "PM" ? 12 : 0; - } - if (timeObjectFromPicker.amOrPm === "AM" && hours === 12) hours = 0; + } else if (timeObjectFromPicker.amOrPm === "AM") hours = 0; date.setHours(hours, minutes, 0); return date; @@ -14,7 +13,11 @@ export function getDateObject(timeObjectFromPicker) { export function to24hours(time) { let str24hr = "", - hours = time.hours + (time.amOrPm === "PM" ? 12 : 0); + { hours } = time; + + if (hours !== 12) hours += time.amOrPm === "PM" ? 12 : 0; + else if (time.amOrPm === "AM") hours = 0; + if (hours < 10) hours = `0${hours}`; const minutes = time.minutes < 10 ? `0${time.minutes}` : time.minutes; str24hr += `${hours}:${minutes}`; From 7f2ec894e5428b9e1ed9a658580fc74e7bb9f7ca Mon Sep 17 00:00:00 2001 From: Dinesh Date: Sat, 26 Sep 2020 13:29:13 +0530 Subject: [PATCH 2/4] Show join time only if it's sometime in the future. Resets for the timepicker too. Also deletes time obj from storage when leaving so that yestrday's time object that might be higher than today's current won't be displayed. --- src/main.js | 18 +++++++++++++++--- src/page.js | 1 + src/storage.js | 5 ++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main.js b/src/main.js index cd051e1..2d0f99c 100644 --- a/src/main.js +++ b/src/main.js @@ -1,7 +1,12 @@ import M from "materialize-css"; import "materialize-css/dist/css/materialize.min.css"; import "./index.css"; -import { convertChipsData, convertToChipsData, to24hours } from "./helpers"; +import { + convertChipsData, + convertToChipsData, + getDateObject, + to24hours, +} from "./helpers"; import "./checkbox"; import MChips from "./chips"; @@ -89,6 +94,9 @@ function listenForSubmit() { }); } function onResetClick() { + browser.storage.local.remove("joinTime"); + const joinTimeButton = document.querySelector(".timepicker"); + joinTimeButton.innerHTML = "Join Time"; clearAllTimeouts(); changeResetToSubmit(); } @@ -119,10 +127,14 @@ let joinInstance; function setupTimepickers({ joinTime: joinTimeStored }) { let joinTimeStr; - if (joinTimeStored) { + + // When this evaluate to true it mostly means join time button is blocked. + // When unblocked by clicking reset we delete the time object from storage. + // This check is just to avoid inconsistencies in odd cases like reloading + // the extension while letting the time object stay in storage. + if (joinTimeStored && getDateObject(joinTimeStored) > new Date()) { state.joinTime = joinTimeStored; joinTimeStr = to24hours(joinTimeStored); - console.log(joinTimeStr, joinTimeStored); } const elems = document.querySelectorAll(".timepicker"); joinInstance = M.Timepicker.init(elems[0], { diff --git a/src/page.js b/src/page.js index a06bd98..62c2e7c 100644 --- a/src/page.js +++ b/src/page.js @@ -109,6 +109,7 @@ export function leaveWhenPeopleLessThan(state) { console.log("leaving now. people count:", peopleCountNow); clearInterval(state.leaveId); leaveCall(state); + browser.storage.local.remove("joinTime"); } } diff --git a/src/storage.js b/src/storage.js index 545b38a..f30f65c 100644 --- a/src/storage.js +++ b/src/storage.js @@ -1,3 +1,5 @@ +import { getDateObject } from "./helpers"; + function storeSucess() { console.log("Succesfully stored to local storage."); } @@ -37,7 +39,8 @@ export async function setUpSettingsFromStorage(state) { console.log("join", joinTime); if (Object.keys(joinTime).length !== 0 && joinTime.joinTime) { console.log("in object"); - setJoinTime(joinTime); + if (getDateObject(joinTime.joinTime) > new Date()) setJoinTime(joinTime); + else await browser.storage.local.remove("joinTime"); } const { leaveThreshold } = await browser.storage.local.get( "leaveThreshold" From 349b45a284b5137bd3152ad973a245f996d510e2 Mon Sep 17 00:00:00 2001 From: Dinesh Date: Sat, 26 Sep 2020 13:58:36 +0530 Subject: [PATCH 3/4] Remove debugging logs. Modified lint rules to allow console.error(). Also fixed few lint warnings/errors and remvoed an unncessary blank file. --- .eslintrc.json | 2 +- src/checkbox.js | 0 src/content.js | 15 +++------------ src/helpers.js | 4 ++-- src/main.js | 20 ++++---------------- src/page.js | 5 ----- src/storage.js | 8 ++------ 7 files changed, 12 insertions(+), 42 deletions(-) delete mode 100644 src/checkbox.js diff --git a/.eslintrc.json b/.eslintrc.json index 471e4ac..6f85040 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -18,7 +18,7 @@ "prettier/prettier": ["warn"], "one-var": ["off"], "no-unused-vars": "warn", - "no-console": "warn", + "no-console": ["warn", { "allow": ["warn", "error"] }], "no-restricted-syntax": "warn", "func-names":"off", "no-param-reassign": "off", diff --git a/src/checkbox.js b/src/checkbox.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/content.js b/src/content.js index 20eac2a..d731780 100644 --- a/src/content.js +++ b/src/content.js @@ -11,8 +11,6 @@ function setUpTimeouts(state) { const { joinTime } = state, joinTimeDateObj = getDateObject(joinTime); - console.log("setting up timeouts", { join: joinTimeDateObj }); - if (joinTime) { new Promise((resolve, reject) => { state.joinTimerId = setTimeout(() => { @@ -33,7 +31,7 @@ function setUpTimeouts(state) { state.joinTimerId = 0; }) .catch((e) => { - console.log(e); + console.error(e); state.joinTimerOn = false; state.joinTimerId = 0; }); @@ -50,8 +48,8 @@ function sendJoinInfo(e) { join: !!joinBtn, }); sending.then( - (respone) => { - console.log("sent join info"); + (response) => { + console.log("sent join info. response:", response); }, (err) => console.error(err) ); @@ -65,8 +63,6 @@ function sendJoinInfo(e) { } window.hasRun = true; - console.log("contentscript"); - const state = { joinTime: 10, leaveThreshold: 0, @@ -110,14 +106,11 @@ function sendJoinInfo(e) { state.leaveInitId = 0; state.leaveTimerOn = false; storeTimeoutIds(0); - console.log("All timeouts cleared"); } browser.runtime.onMessage.addListener((recievedObj, sender, sendResponse) => { const { message, state: recievedState } = recievedObj; - console.log("hi"); updateState(recievedState); - console.log("message recieved in content:", message); switch (message) { case "submit": onSubmit(); @@ -129,12 +122,10 @@ function sendJoinInfo(e) { break; case "updateWords": startSubtitleTimers(state); - console.log(state) break; default: console.log("message did not match: ", message); } - console.log("content", state); return Promise.resolve(state); }); })(); diff --git a/src/helpers.js b/src/helpers.js index a69d4c7..745fff6 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -26,12 +26,12 @@ export function to24hours(time) { export function convertToChipsData(dataArray) { const chipsData = []; - dataArray.forEach(word => chipsData.push({tag: word})); + dataArray.forEach((word) => chipsData.push({ tag: word })); return chipsData; } export function convertChipsData(chipsDataArray) { const data = []; - chipsDataArray.forEach(dataObj => data.push(dataObj.tag)); + chipsDataArray.forEach((dataObj) => data.push(dataObj.tag)); return data; } diff --git a/src/main.js b/src/main.js index 2d0f99c..e58e4ca 100644 --- a/src/main.js +++ b/src/main.js @@ -7,7 +7,6 @@ import { getDateObject, to24hours, } from "./helpers"; -import "./checkbox"; import MChips from "./chips"; import { @@ -29,9 +28,7 @@ let state = { subtitleTimerId: 0, }; - function handleJoinInfo(request, sender, sendResponse) { - console.log(request); if (!request.join) { document.getElementById("join").disabled = true; state.canJoin = false; @@ -41,7 +38,7 @@ function handleJoinInfo(request, sender, sendResponse) { function toggleUI() { const leaveInput = document.getElementById("leave_threshold"), joinButton = document.getElementById("join"); - if (state.submitReset == "reset") { + if (state.submitReset === "reset") { leaveInput.setAttribute("readonly", ""); leaveInput.setAttribute("class", "dark_border"); joinButton.setAttribute("disabled", ""); @@ -67,25 +64,21 @@ function changeResetToSubmit() { toggleUI(); } function catchError(e) { - console.log(e); + console.error(e); } function success(recievedState) { - console.log("Submit success, storing times"); updateState(recievedState); - console.log(state); saveJoinTimeToStorage(state.joinTime); changeSubmitToReset(); } function listenForSubmit() { - console.log("listen"); function onSubmitResetClick() { - if (state.submitReset == "submit") onSubmitClick(); - if (state.submitReset == "reset") onResetClick(); + if (state.submitReset === "submit") onSubmitClick(); + if (state.submitReset === "reset") onResetClick(); } function clearAllTimeouts() { - console.log("initiating clear timouts in main"); browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => { browser.tabs .sendMessage(tabs[0].id, { message: "clearAllTimeouts", state }) @@ -101,8 +94,6 @@ function listenForSubmit() { changeResetToSubmit(); } function onSubmitClick() { - console.log("onSubmit click"); - state.alertWords = getAlertWords(); state.leaveThreshold = parseInt( document.getElementById("leave_threshold").value, @@ -150,7 +141,6 @@ function setupTimepickers({ joinTime: joinTimeStored }) { "
Join:
"; const span = document.getElementById("joinSpan"); span.innerHTML = this.time + this.amOrPm; - console.log("onClose", state.joinTime); } }, }); @@ -200,7 +190,6 @@ function updateState(recievedState) { }) .catch((e) => console.error(e)); }); - console.log("main state updated:", state); } setUpSettingsFromStorage(state).then((joinTime) => { @@ -226,4 +215,3 @@ browser.tabs .catch((e) => console.error(`Error occured: ${e}`)); browser.runtime.onMessage.addListener(handleJoinInfo); -//setUpListnerForInput(); diff --git a/src/page.js b/src/page.js index 62c2e7c..aafa806 100644 --- a/src/page.js +++ b/src/page.js @@ -19,7 +19,6 @@ export function leaveCall(state) { const leaveButton = getLeaveButton(); if (leaveButton !== undefined) { leaveButton.click(); - console.log("left meeting"); } else { console.error("leave button not found"); } @@ -28,9 +27,7 @@ export function leaveCall(state) { export function joinCall() { const joinButton = getJoinButton(); - console.log(joinButton); if (joinButton !== undefined) { - console.log("joined meeting."); joinButton.click(); return 1; } @@ -106,7 +103,6 @@ export function leaveWhenPeopleLessThan(state) { function leave() { const peopleCountNow = getPeopleCount(); if (count > peopleCountNow) { - console.log("leaving now. people count:", peopleCountNow); clearInterval(state.leaveId); leaveCall(state); browser.storage.local.remove("joinTime"); @@ -121,5 +117,4 @@ export function leaveWhenPeopleLessThan(state) { } } state.leaveInitId = setInterval(runInit, 1000); - // change setTimeout to setTimer ? } diff --git a/src/storage.js b/src/storage.js index f30f65c..85310aa 100644 --- a/src/storage.js +++ b/src/storage.js @@ -5,7 +5,7 @@ function storeSucess() { } function storeFailure(e) { - console.log("Failed to store with error:", e); + console.error("Failed to store with error:", e); } export function saveJoinTimeToStorage(joinTime) { @@ -13,7 +13,7 @@ export function saveJoinTimeToStorage(joinTime) { } function getTimeFailure(e) { - console.log("Failed to get stored time. error:", e); + console.error("Failed to get stored time. error:", e); } function setJoinTime(time) { @@ -36,9 +36,7 @@ export async function setUpSettingsFromStorage(state) { let joinTime; try { joinTime = await browser.storage.local.get("joinTime"); - console.log("join", joinTime); if (Object.keys(joinTime).length !== 0 && joinTime.joinTime) { - console.log("in object"); if (getDateObject(joinTime.joinTime) > new Date()) setJoinTime(joinTime); else await browser.storage.local.remove("joinTime"); } @@ -63,14 +61,12 @@ function clearJoinTimeOut(object) { } export function cancelPreviousTimeouts() { - console.log("Clearing previous timeouts."); browser.storage.local .get("joinTimerId") .then(clearJoinTimeOut, getTimeFailure); } export function storeTimeoutIds(joinTimerId) { - console.log("storing timeout ids"); browser.storage.local.set({ joinTimerId }).then(storeSucess, storeFailure); } From e668f397d858c9ef3fab356b9a20a7c56a860244 Mon Sep 17 00:00:00 2001 From: Dinesh Date: Sat, 26 Sep 2020 15:27:55 +0530 Subject: [PATCH 4/4] multitab wip --- src/helpers.js | 8 ++++++++ src/main.js | 25 ++++++++++--------------- src/storage.js | 38 ++++++++++++++++++++------------------ 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 745fff6..1236897 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -35,3 +35,11 @@ export function convertChipsData(chipsDataArray) { chipsDataArray.forEach((dataObj) => data.push(dataObj.tag)); return data; } + +export function getPageURL() { + return browser.tabs + .query({ currentWindow: true, active: true }) + .then((tabs) => { + return tabs[0].url; + }); +} diff --git a/src/main.js b/src/main.js index e58e4ca..770535b 100644 --- a/src/main.js +++ b/src/main.js @@ -5,16 +5,12 @@ import { convertChipsData, convertToChipsData, getDateObject, + getPageURL, to24hours, } from "./helpers"; import MChips from "./chips"; -import { - saveJoinTimeToStorage, - setUpSettingsFromStorage, - storeAlertWords, - storeLeaveThreshold, -} from "./storage"; +import { setUpSettingsFromStorage, storeSettings } from "./storage"; let state = { joinTime: 0, @@ -67,9 +63,11 @@ function catchError(e) { console.error(e); } -function success(recievedState) { +function SubmitSuccess(recievedState) { updateState(recievedState); - saveJoinTimeToStorage(state.joinTime); + getPageURL().then((url) => { + storeSettings(url, state.joinTime, state.leaveThreshold, state.alertWords); + }); changeSubmitToReset(); } @@ -100,13 +98,10 @@ function listenForSubmit() { 10 ); - storeAlertWords(state.alertWords); - storeLeaveThreshold(state.leaveThreshold); - browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => { browser.tabs .sendMessage(tabs[0].id, { message: "submit", state }) - .then(success) + .then(SubmitSuccess) .catch(catchError); }); } @@ -148,7 +143,7 @@ function setupTimepickers({ joinTime: joinTimeStored }) { } function setupChips(words) { - function onChipsModified(){ + function onChipsModified() { state.alertWords = getAlertWords(); storeAlertWords(state.alertWords); browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => { @@ -164,10 +159,10 @@ function setupChips(words) { data: words, placeholder: "Enter alert words to get notified!", secondaryPlaceholder: "Alert word", - onChipAdd(){ + onChipAdd() { onChipsModified(); }, - onChipDelete(){ + onChipDelete() { onChipsModified(); }, }); diff --git a/src/storage.js b/src/storage.js index 85310aa..144b159 100644 --- a/src/storage.js +++ b/src/storage.js @@ -1,4 +1,4 @@ -import { getDateObject } from "./helpers"; +import { getDateObject, getPageURL } from "./helpers"; function storeSucess() { console.log("Succesfully stored to local storage."); @@ -33,22 +33,26 @@ function setLeaveThreshold(threshold) { } export async function setUpSettingsFromStorage(state) { - let joinTime; + let joinTime, leaveThreshold, alertWords; try { - joinTime = await browser.storage.local.get("joinTime"); + const url = await getPageURL(); + const storedSettings = await browser.storage.local.get(url)[url]; + + joinTime = storedSettings.joinTime; + leaveThreshold = storedSettings.leaveThreshold; + alertWords = storedSettings.alertWords; + if (Object.keys(joinTime).length !== 0 && joinTime.joinTime) { if (getDateObject(joinTime.joinTime) > new Date()) setJoinTime(joinTime); - else await browser.storage.local.remove("joinTime"); + else { + delete storedSettings.joinTime; + const toStore = {}; + toStore[url] = storedSettings; + await browser.storage.local.set(toStore); + } } - const { leaveThreshold } = await browser.storage.local.get( - "leaveThreshold" - ); if (leaveThreshold) setLeaveThreshold(leaveThreshold); - - const { alertWords } = await browser.storage.local.get("alertWords"); - if (alertWords) { - state.alertWords = alertWords; - } + if (alertWords) state.alertWords = alertWords; } catch (e) { getTimeFailure(e); } @@ -70,10 +74,8 @@ export function storeTimeoutIds(joinTimerId) { browser.storage.local.set({ joinTimerId }).then(storeSucess, storeFailure); } -export function storeLeaveThreshold(leaveThreshold) { - browser.storage.local.set({ leaveThreshold }).then(storeSucess, storeFailure); -} - -export function storeAlertWords(alertWords) { - browser.storage.local.set({ alertWords }).then(storeSucess, storeFailure); +export function storeSettings(url, joinTime, leaveThreshold, alertWords) { + const toStore = {}; + toStore[url] = { joinTime, leaveThreshold, alertWords }; + browser.storage.local.set(toStore).then(storeSucess, storeFailure); }