~callum/beavers-dam

395143966326267df50c3be77ba2b08559288fde — Callum Brown 1 year, 2 months ago 6ad667e v0.3
v0.3: require journal entry before undamming
4 files changed, 50 insertions(+), 42 deletions(-)

M content/content.js
M manifest.json
M popup/popup.js
M todo
M content/content.js => content/content.js +22 -1
@@ 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();

M manifest.json => manifest.json +1 -1
@@ 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"

M popup/popup.js => popup/popup.js +26 -37
@@ 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);
	});
});

M todo => todo +1 -3
@@ 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)