Move sticky note path checking to context

This commit is contained in:
Idrees Hassan
2025-11-13 17:46:03 -05:00
parent 40169dd474
commit d7dca478d6
10 changed files with 293 additions and 243 deletions

View File

@@ -426,12 +426,12 @@ Promise.all([
drawStickyNotes(stickyNotes, save, deleteStickyNote);
let lastUrl = (window.location.href ?? "").split("?")[0];
let lastPath = getContext().getPath().split("?")[0];
setInterval(() => {
const currentUrl = (window.location.href ?? "").split("?")[0];
if (currentUrl !== lastUrl) {
log("URL changed, updating sticky notes");
lastUrl = currentUrl;
const currentPath = getContext().getPath().split("?")[0];
if (currentPath !== lastPath) {
log("Path changed, updating sticky notes");
lastPath = currentPath;
drawStickyNotes(stickyNotes, save, deleteStickyNote);
}
}, URL_CHECK_INTERVAL);

View File

@@ -1,4 +1,3 @@
import { debug, log, error } from "./shared.js";
const SAVE_KEY = "birbSaveData";
@@ -42,6 +41,40 @@ export class Context {
resetSaveData() {
throw new Error("Method not implemented");
}
/**
* @returns {string} The current path of the active page in this context
*/
getPath() {
// Default to website URL
return window.location.href;
}
/**
* Checks if a path is applicable given the context
* @param {string} path Can be a site URL or another context-specific path
* @returns {boolean} Whether the path matches the current context state
*/
isPathApplicable(path) {
// Default to website URL matching
const currentUrl = window.location.href;
const stickyNoteWebsite = path.split("?")[0];
const currentWebsite = currentUrl.split("?")[0];
if (stickyNoteWebsite !== currentWebsite) {
return false;
}
const pathParams = parseUrlParams(path);
const currentParams = parseUrlParams(currentUrl);
if (window.location.hostname === "www.youtube.com") {
if (currentParams.v !== undefined && currentParams.v !== pathParams.v) {
return false;
}
}
return true;
}
}
export class LocalContext extends Context {
@@ -223,4 +256,19 @@ export function getContext() {
}
error("No applicable context found, defaulting to LocalContext");
return new LocalContext();
}
/**
* Parse URL parameters into a key-value map
* @param {string} url
* @returns {Record<string, string>}
*/
function parseUrlParams(url) {
const queryString = url.split("?")[1];
if (!queryString) return {};
return queryString.split("&").reduce((params, param) => {
const [key, value] = param.split("=");
return { ...params, [key]: value };
}, {});
}

View File

@@ -3,6 +3,7 @@ import {
makeDraggable,
makeClosable
} from './shared.js';
import { getContext } from './context.js';
/**
* @typedef {Object} SavedStickyNote
@@ -30,46 +31,6 @@ export class StickyNote {
}
}
/**
* Parse URL parameters into a key-value map
* @param {string} url
* @returns {Record<string, string>}
*/
export function parseUrlParams(url) {
const queryString = url.split("?")[1];
if (!queryString) return {};
return queryString.split("&").reduce((params, param) => {
const [key, value] = param.split("=");
return { ...params, [key]: value };
}, {});
}
/**
* @param {StickyNote} stickyNote
* @returns {boolean} Whether the given sticky note is applicable to the current site/page
*/
export function isStickyNoteApplicable(stickyNote) {
const stickyNoteUrl = stickyNote.site;
const currentUrl = window.location.href;
const stickyNoteWebsite = stickyNoteUrl.split("?")[0];
const currentWebsite = currentUrl.split("?")[0];
if (stickyNoteWebsite !== currentWebsite) {
return false;
}
const stickyNoteParams = parseUrlParams(stickyNoteUrl);
const currentParams = parseUrlParams(currentUrl);
if (window.location.hostname === "www.youtube.com") {
if (currentParams.v !== undefined && currentParams.v !== stickyNoteParams.v) {
return false;
}
}
return true;
}
/**
* @param {StickyNote} stickyNote
* @param {() => void} onSave
@@ -152,8 +113,9 @@ export function drawStickyNotes(stickyNotes, onSave, onDelete) {
const existingNotes = document.querySelectorAll(".birb-sticky-note");
existingNotes.forEach(note => note.remove());
// Render all sticky notes
const context = getContext();
for (let stickyNote of stickyNotes) {
if (isStickyNoteApplicable(stickyNote)) {
if (context.isPathApplicable(stickyNote.site)) {
renderStickyNote(stickyNote, onSave, () => onDelete(stickyNote));
}
}
@@ -166,7 +128,7 @@ export function drawStickyNotes(stickyNotes, onSave, onDelete) {
*/
export function createNewStickyNote(stickyNotes, onSave, onDelete) {
const id = Date.now().toString();
const site = window.location.href;
const site = getContext().getPath();
const stickyNote = new StickyNote(id, site, "");
const element = renderStickyNote(stickyNote, onSave, () => onDelete(stickyNote));
element.style.left = `${window.innerWidth / 2 - element.offsetWidth / 2}px`;