From 395143966326267df50c3be77ba2b08559288fde Mon Sep 17 00:00:00 2001 From: Callum Brown Date: Wed, 5 Apr 2023 19:40:04 +0100 Subject: [PATCH] v0.3: require journal entry before undamming --- content/content.js | 23 ++++++++++++++++- manifest.json | 2 +- popup/popup.js | 63 +++++++++++++++++++--------------------------- todo | 4 +-- 4 files changed, 50 insertions(+), 42 deletions(-) diff --git a/content/content.js b/content/content.js index c9003db..8b5e793 100644 --- a/content/content.js +++ b/content/content.js @@ -16,6 +16,21 @@ const save = (text) => { }; +const undam = (hostname) => { + chrome.storage.sync.get( + { dammed: [] }, + ).then((items) => { + let index = items.dammed.indexOf(hostname); + if (index !== -1) { + items.dammed.splice(index, 1); + chrome.storage.sync.set( + { dammed: items.dammed } + ); + } + }); +}; + + const dam = () => { let dam = document.createElement("div"); dam.id = "beavers-dam"; @@ -42,6 +57,12 @@ const dam = () => { if (textarea.value.length > 0) { save(textarea.value); dam.remove(); + // Check if this host has been set to be undammed in popup.js + chrome.storage.local.get(["undam"]).then((items) => { + if (items.undam === location.hostname) { + undam(location.hostname); + } + }).then(() => chrome.storage.local.set({ undam: null })); } else { window.alert("Please write in your journal."); } @@ -51,7 +72,7 @@ const dam = () => { chrome.storage.sync.get( - { dammed: new Set() }, + { dammed: [] }, ).then((items) => { if (items.dammed.includes(location.hostname)) { dam(); diff --git a/manifest.json b/manifest.json index 5ceed21..efc8544 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 3, "name": "Beaver's Dam", "description": "Base Level Extension", - "version": "0.2.1", + "version": "0.3", "permissions": ["storage", "tabs"], "action": { "default_popup": "popup/popup.html" diff --git a/popup/popup.js b/popup/popup.js index d92bdf5..0eec172 100644 --- a/popup/popup.js +++ b/popup/popup.js @@ -1,31 +1,7 @@ -const textarea = document.getElementById("dammed"); - - -const openJournal = () => { - window.open(chrome.runtime.getURL("journal/journal.html")); -}; - - -const toggleDammed = (hostname, tabId) => { - chrome.storage.sync.get( - { dammed: [] }, - ).then((items) => { - let index = items.dammed.indexOf(hostname); - if (index === -1) { - items.dammed.push(hostname); - } else { - items.dammed.splice(index, 1); - } - chrome.storage.sync.set( - { dammed: items.dammed } - ).then(() => { - chrome.tabs.reload(tabId).then(window.close); - }); - }); -}; - - -document.getElementById("open-journal").addEventListener("click", openJournal); +document.getElementById("open-journal").addEventListener( + "click", + () => window.open(chrome.runtime.getURL("journal/journal.html")), +); document.getElementById("clear").addEventListener( "click", @@ -37,20 +13,33 @@ document.getElementById("clear").addEventListener( chrome.tabs.query({ active: true}).then(([tab]) => { let currentURL = new URL(tab.url); - let host = currentURL.hostname; + let hostname = currentURL.hostname; let toggleDammedButton= document.getElementById("toggle-dammed"); - toggleDammedButton.addEventListener( - "click", - () => toggleDammed(host, tab.id), - ); - // Set correct initial button text chrome.storage.sync.get( { dammed: [] }, ).then((items) => { - if (items.dammed.includes(host)) { - toggleDammedButton.textContent = `Undam ${host}`; + let toggleDammed; + if (items.dammed.includes(hostname)) { + toggleDammedButton.textContent = `Undam ${hostname}`; + toggleDammed = () => { + // Set hostname to be undammed in content.js + chrome.storage.local.set({ undam: hostname }).then(() => { + chrome.tabs.reload(tab.id).then(window.close); + }); + }; } else { - toggleDammedButton.textContent = `Dam ${host}`; + toggleDammedButton.textContent = `Dam ${hostname}`; + toggleDammed = () => { + // Dam hostname + items.dammed.push(hostname); + console.log(items.dammed); + chrome.storage.sync.set( + { dammed: items.dammed } + ).then(() => { + chrome.tabs.reload(tab.id).then(window.close); + }); + }; } + toggleDammedButton.addEventListener("click", toggleDammed); }); }); diff --git a/todo b/todo index 40c1789..8de09c9 100644 --- a/todo +++ b/todo @@ -2,9 +2,7 @@ programmatically modify matching pages in background.js? Min word count -Require reason for removal - -Custom prompt +Custom prompt (different prompt for undamming?) Custom style (colours, font) -- 2.45.2