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 13915fb..1236897 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}`; @@ -23,12 +26,20 @@ 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; } + +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 cd051e1..770535b 100644 --- a/src/main.js +++ b/src/main.js @@ -1,16 +1,16 @@ import M from "materialize-css"; import "materialize-css/dist/css/materialize.min.css"; import "./index.css"; -import { convertChipsData, convertToChipsData, to24hours } from "./helpers"; -import "./checkbox"; +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, @@ -24,9 +24,7 @@ let state = { subtitleTimerId: 0, }; - function handleJoinInfo(request, sender, sendResponse) { - console.log(request); if (!request.join) { document.getElementById("join").disabled = true; state.canJoin = false; @@ -36,7 +34,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", ""); @@ -62,25 +60,23 @@ function changeResetToSubmit() { toggleUI(); } function catchError(e) { - console.log(e); + console.error(e); } -function success(recievedState) { - console.log("Submit success, storing times"); +function SubmitSuccess(recievedState) { updateState(recievedState); - console.log(state); - saveJoinTimeToStorage(state.joinTime); + getPageURL().then((url) => { + storeSettings(url, state.joinTime, state.leaveThreshold, state.alertWords); + }); 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 }) @@ -89,25 +85,23 @@ function listenForSubmit() { }); } function onResetClick() { + browser.storage.local.remove("joinTime"); + const joinTimeButton = document.querySelector(".timepicker"); + joinTimeButton.innerHTML = "Join Time"; clearAllTimeouts(); changeResetToSubmit(); } function onSubmitClick() { - console.log("onSubmit click"); - state.alertWords = getAlertWords(); state.leaveThreshold = parseInt( document.getElementById("leave_threshold").value, 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); }); } @@ -119,10 +113,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], { @@ -138,7 +136,6 @@ function setupTimepickers({ joinTime: joinTimeStored }) { "