~callum/beavers-dam

3d63ab6b768e6d56059ddf84d5e6ad56bf3f2ee5 — Callum Brown 1 year, 2 months ago 0cea9e8 v0.2
v0.2: dam or undam with toggle button
5 files changed, 40 insertions(+), 33 deletions(-)

M content/content.js
M manifest.json
M popup/popup.html
M popup/popup.js
M todo
M content/content.js => content/content.js +3 -3
@@ 51,9 51,9 @@ const dam = () => {


chrome.storage.sync.get(
	{ dammed: [] },
).then((options) => {
	if (options.dammed.includes(location.hostname)) {
	{ dammed: new Set() },
).then((items) => {
	if (items.dammed.includes(location.hostname)) {
		dam();
	}
});

M manifest.json => manifest.json +2 -2
@@ 2,8 2,8 @@
	"manifest_version": 3,
	"name": "Beaver's Dam",
	"description": "Base Level Extension",
	"version": "0.1",
	"permissions": ["storage"],
	"version": "0.2",
	"permissions": ["storage", "tabs"],
	"action": {
		"default_popup": "popup/popup.html"
	},

M popup/popup.html => popup/popup.html +1 -5
@@ 7,11 7,7 @@
		<h1>Beaver's Dam</h1>
		<button id="open-journal">Open Journal</button>
		<br>
		<label for="dammed">Dammed sites:</label>
		<br>
		<textarea id="dammed"></textarea>
		<div id="status"></div>
		<button id="save">Save</button>
		<button id="toggle-dammed"></button>
		<br>
		<button id="clear">Clear storage</button>
	</body>

M popup/popup.js => popup/popup.js +34 -20
@@ 1,5 1,3 @@
// Modified from https://developer.chrome.com/docs/extensions/mv3/options/

const textarea = document.getElementById("dammed");




@@ 8,31 6,30 @@ const openJournal = () => {
};


const saveSettings = () => {
	chrome.storage.sync.set(
		{ dammed: textarea.value.split("\n") }
	).then(() => {
		const saveStatus = document.getElementById("status");
		saveStatus.textContent = "Saved.";
		setTimeout(() => {
			saveStatus.textContent = "";
		}, 750);
	});
};


const restoreSettings = () => {
const toggleDammed = (hostname) => {
	chrome.storage.sync.get(
		{ dammed: [] },
	).then((settings) => {
		textarea.value = settings.dammed.join("\n");
	).then((items) => {
		let newText;
		let index = items.dammed.indexOf(hostname);
		if (index === -1) {
			items.dammed.push(hostname);
			newText = `Undam ${hostname}`;
		} else {
			items.dammed.splice(index, 1);
			newText = `Dam ${hostname}`;
		}
		chrome.storage.sync.set(
			{ dammed: items.dammed }
		).then(() => {
			// TODO: reload page instead of changing text
			document.getElementById("toggle-dammed").textContent = newText;
		});
	});
};


document.getElementById("open-journal").addEventListener("click", openJournal);
document.addEventListener("DOMContentLoaded", restoreSettings);
document.getElementById("save").addEventListener("click", saveSettings);

document.getElementById("clear").addEventListener(
	"click",


@@ 41,3 38,20 @@ document.getElementById("clear").addEventListener(
		chrome.storage.local.clear();
	},
);

chrome.tabs.query({ active: true}).then(([tab]) => {
	let currentURL = new URL(tab.url);
	let host = currentURL.hostname;
	let toggleDammedButton= document.getElementById("toggle-dammed");
	toggleDammedButton.addEventListener("click", () => toggleDammed(host));
	// Set correct initial button text
	chrome.storage.sync.get(
		{ dammed: [] },
	).then((items) => {
		if (items.dammed.includes(host)) {
			toggleDammedButton.textContent = `Undam ${host}`;
		} else {
			toggleDammedButton.textContent = `Dam ${host}`;
		}
	});
});

M todo => todo +0 -3
@@ 4,13 4,10 @@ save and view journal

Min word count

Add / Remove current site in popup

Require reason for removal

Cusom prompt

Custom style (colours, font)


Different prompt, word count per domain