From 97de22591e444d4d1b01d69856e63b7337a1073a Mon Sep 17 00:00:00 2001 From: Marija Gerasimovska Date: Wed, 9 Jul 2025 11:50:32 +0200 Subject: [PATCH 1/5] On activated tab listener --- chrome/manifest.json | 3 ++- chrome/src/background.ts | 52 +++++++++++++++++++++++++++++++++++++++ firefox/src/background.ts | 7 +++++- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/chrome/manifest.json b/chrome/manifest.json index 282a3c6..d405811 100644 --- a/chrome/manifest.json +++ b/chrome/manifest.json @@ -17,7 +17,8 @@ "cookies", "webNavigation", "declarativeNetRequest", - "webRequest" + "webRequest", + "tabs" ], "host_permissions": [ "https://*.toddle.site/*", diff --git a/chrome/src/background.ts b/chrome/src/background.ts index 8a529b1..0db2001 100644 --- a/chrome/src/background.ts +++ b/chrome/src/background.ts @@ -61,6 +61,50 @@ chrome.webNavigation.onBeforeNavigate.addListener( }, ) +chrome.tabs.onActivated.addListener(async (activeInfo) => { + const tab = await chrome.tabs.query({ + active: true, + lastFocusedWindow: true, + }) + + const urlString = tab[0].url + + if (!urlString) { + return + } + + const url = new URL(urlString) + + if ( + url.host.endsWith('toddle.dev') === false && + url.host.endsWith('nordcraft.com') === false + ) { + return false + } + + // Get the cookies for the .nordcraft.site domain + const frames = await chrome.webNavigation.getAllFrames({ + tabId: activeInfo.tabId, + }) + + const domain = frames + ?.map((frame) => { + const url = new URL(frame.url) + if (url.host?.endsWith('.toddle.site')) { + return url.host + } + }) + .filter((v) => v)[0] + if (!domain) { + return + } + const cookies = await chrome.cookies.getAll({ domain }) + + if (cookies.length > 0) { + await updateSessionRules({ domainCookies: cookies, RULE_ID }) + } +}) + chrome.webRequest.onHeadersReceived.addListener( (info) => { // check the parent frame so we only override cookies if we are on nordcraft.com @@ -71,6 +115,11 @@ chrome.webRequest.onHeadersReceived.addListener( if (!isNordcraft) { return undefined } + + if (!info.initiator) { + return undefined + } + if (info.responseHeaders) { const setCookieHeaders = info.responseHeaders .filter( @@ -88,6 +137,9 @@ chrome.webRequest.onHeadersReceived.addListener( setCookieHeaders, requestUrl: info.url, setCookie: async (cookie, domain) => { + if (!domain?.endsWith('.toddle.site')) { + return + } await chrome.cookies.set(cookie) const parsedUrl = new URL(cookie.url) diff --git a/firefox/src/background.ts b/firefox/src/background.ts index c485f38..498e70f 100644 --- a/firefox/src/background.ts +++ b/firefox/src/background.ts @@ -107,7 +107,12 @@ browser.webRequest.onHeadersReceived.addListener( setCookies({ setCookieHeaders, requestUrl: info.url, - setCookie: (cookie) => browser.cookies.set(cookie), + setCookie: (cookie, domain) => { + if (!domain?.endsWith('.toddle.site')) { + return + } + browser.cookies.set(cookie) + }, removeCookie: (cookie) => browser.cookies.remove(cookie), notifyUser, }) From 154863be3d631f89201a5b690955653de0e47d44 Mon Sep 17 00:00:00 2001 From: Marija Gerasimovska Date: Wed, 9 Jul 2025 11:51:00 +0200 Subject: [PATCH 2/5] Update version --- chrome/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chrome/manifest.json b/chrome/manifest.json index d405811..bd29ac4 100644 --- a/chrome/manifest.json +++ b/chrome/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 3, "name": "Nordcraft", "description": "Browser extension for the Nordcraft editor.", - "version": "2.3.6", + "version": "2.3.7", "icons": { "32": "icons/nordcraft-32.png", "64": "icons/nordcraft-64.png", From 303c626bb97929c6cfd501c41f6bebe63554ca9f Mon Sep 17 00:00:00 2001 From: Marija Gerasimovska Date: Wed, 9 Jul 2025 14:50:56 +0200 Subject: [PATCH 3/5] Fixes --- chrome/manifest.json | 1 - chrome/src/background.ts | 58 +++++++++++++-------------------------- chrome/src/helpers.ts | 43 ++++++++++++++++++++++++++--- firefox/manifest.json | 1 - firefox/src/background.ts | 10 +++---- 5 files changed, 63 insertions(+), 50 deletions(-) diff --git a/chrome/manifest.json b/chrome/manifest.json index bd29ac4..945eba4 100644 --- a/chrome/manifest.json +++ b/chrome/manifest.json @@ -27,7 +27,6 @@ "content_scripts": [ { "matches": [ - "https://*.toddle.dev/*", "https://*.toddle.site/*", "https://editor.nordcraft.com/*", "https://*.nordcraft.site/*" diff --git a/chrome/src/background.ts b/chrome/src/background.ts index 0db2001..2ee23ef 100644 --- a/chrome/src/background.ts +++ b/chrome/src/background.ts @@ -1,7 +1,11 @@ // The .js extension is necessary for Chrome to pickup the import correctly import { setCookies } from '../../shared/setCookies.js' import type { RequireFields } from '../../shared/setCookies.js' -import { nordcraftIsParentFrame, updateSessionRules } from './helpers.js' +import { + getCookiesAndUpdateSessionRules, + nordcraftIsParentFrame, + updateSessionRules, +} from './helpers.js' console.log('Nordcraft extension loaded') @@ -23,35 +27,10 @@ chrome.webNavigation.onBeforeNavigate.addListener( return } - // Get the cookies for the .nordcraft.site domain - const url = new URL(event.url) - const domain = url.host - const domainCookies = await chrome.cookies.getAll({ - domain, + await getCookiesAndUpdateSessionRules({ + url: event.url, + RULE_ID, }) - const tab = chrome.tabs.query({ - active: true, - lastFocusedWindow: true, - }) - - const requestedUrl = url.origin - - // Don't return the value for the http cookies and include the requested url - const cookies = domainCookies.map((c) => - c.httpOnly - ? { ...c, url: requestedUrl, value: undefined } - : { ...c, url: requestedUrl }, - ) - - tab.then(([t]) => { - if (t && t.id) { - chrome.tabs.sendMessage(t.id, cookies) - } - }) - - if (domainCookies.length > 0) { - await updateSessionRules({ domainCookies, RULE_ID }) - } }, { url: [ @@ -76,8 +55,8 @@ chrome.tabs.onActivated.addListener(async (activeInfo) => { const url = new URL(urlString) if ( - url.host.endsWith('toddle.dev') === false && - url.host.endsWith('nordcraft.com') === false + url.host.endsWith('nordcraft.com') === false && + url.host.endsWith('-toddle.toddle.site') ) { return false } @@ -87,22 +66,20 @@ chrome.tabs.onActivated.addListener(async (activeInfo) => { tabId: activeInfo.tabId, }) - const domain = frames + const frameUrl = frames ?.map((frame) => { const url = new URL(frame.url) if (url.host?.endsWith('.toddle.site')) { - return url.host + return frame.url } }) .filter((v) => v)[0] - if (!domain) { + + if (!frameUrl) { return } - const cookies = await chrome.cookies.getAll({ domain }) - if (cookies.length > 0) { - await updateSessionRules({ domainCookies: cookies, RULE_ID }) - } + await getCookiesAndUpdateSessionRules({ url: frameUrl, RULE_ID }) }) chrome.webRequest.onHeadersReceived.addListener( @@ -137,7 +114,10 @@ chrome.webRequest.onHeadersReceived.addListener( setCookieHeaders, requestUrl: info.url, setCookie: async (cookie, domain) => { - if (!domain?.endsWith('.toddle.site')) { + if ( + !domain?.endsWith('.toddle.site') && + !domain?.endsWith('.nordcraft.site') + ) { return } await chrome.cookies.set(cookie) diff --git a/chrome/src/helpers.ts b/chrome/src/helpers.ts index a5d969d..5696b66 100644 --- a/chrome/src/helpers.ts +++ b/chrome/src/helpers.ts @@ -52,11 +52,46 @@ export async function nordcraftIsParentFrame({ } const parentUrl = new URL(parentFrame.url) - if ( - parentUrl.host.endsWith('toddle.dev') === false && - parentUrl.host.endsWith('nordcraft.com') === false - ) { + if (parentUrl.host.endsWith('nordcraft.com') === false) { return false } return true } + +export async function getCookiesAndUpdateSessionRules({ + url, + RULE_ID, +}: { + url: string + RULE_ID: number +}) { + const parsedUrl = new URL(url) + const domain = parsedUrl.host + + // Get the cookies for the .nordcraft.site domain + const domainCookies = await chrome.cookies.getAll({ domain }) + + // Don't return the value for the http cookies and include the requested url + const requestedUrl = parsedUrl.origin + const cookies = domainCookies.map((c) => + c.httpOnly + ? { ...c, url: requestedUrl, value: undefined } + : { ...c, url: requestedUrl }, + ) + + const tab = chrome.tabs.query({ + active: true, + lastFocusedWindow: true, + }) + + tab.then(([t]) => { + if (t && t.id) { + chrome.tabs.sendMessage(t.id, cookies) + } + }) + + if (cookies.length > 0) { + await updateSessionRules({ domainCookies, RULE_ID }) + } + return cookies +} diff --git a/firefox/manifest.json b/firefox/manifest.json index 9cf80eb..1a94b2e 100644 --- a/firefox/manifest.json +++ b/firefox/manifest.json @@ -26,7 +26,6 @@ "content_scripts": [ { "matches": [ - "https://*.toddle.dev/*", "https://*.toddle.site/*", "https://editor.nordcraft.com/*", "https://*.nordcraft.site/*" diff --git a/firefox/src/background.ts b/firefox/src/background.ts index 498e70f..b46e07f 100644 --- a/firefox/src/background.ts +++ b/firefox/src/background.ts @@ -108,7 +108,10 @@ browser.webRequest.onHeadersReceived.addListener( setCookieHeaders, requestUrl: info.url, setCookie: (cookie, domain) => { - if (!domain?.endsWith('.toddle.site')) { + if ( + !domain?.endsWith('.toddle.site') && + !domain?.endsWith('.nordcraft.site') + ) { return } browser.cookies.set(cookie) @@ -151,10 +154,7 @@ async function nordcraftIsParentFrame({ const parentUrl = new URL(parentFrame.url) - if ( - parentUrl.host.endsWith('toddle.dev') === false && - parentUrl.host.endsWith('nordcraft.com') === false - ) { + if (parentUrl.host.endsWith('nordcraft.com') === false) { return false } return true From e956cdd22356a1b06ec18de1594d512696f67ab8 Mon Sep 17 00:00:00 2001 From: Marija Gerasimovska Date: Fri, 11 Jul 2025 10:20:14 +0200 Subject: [PATCH 4/5] small fixes --- chrome/src/background.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chrome/src/background.ts b/chrome/src/background.ts index 2ee23ef..fdd5e44 100644 --- a/chrome/src/background.ts +++ b/chrome/src/background.ts @@ -56,12 +56,12 @@ chrome.tabs.onActivated.addListener(async (activeInfo) => { if ( url.host.endsWith('nordcraft.com') === false && - url.host.endsWith('-toddle.toddle.site') + url.host.endsWith('-toddle.toddle.site') === false ) { return false } - // Get the cookies for the .nordcraft.site domain + // Get the cookies for the .toddle.site domain const frames = await chrome.webNavigation.getAllFrames({ tabId: activeInfo.tabId, }) From de7f198f5e6e99081305ee26320e5790916a07da Mon Sep 17 00:00:00 2001 From: Marija Gerasimovska Date: Fri, 11 Jul 2025 15:58:28 +0200 Subject: [PATCH 5/5] get the right domain cookies --- firefox/manifest.json | 2 +- firefox/src/background.ts | 4 ++-- shared/setCookies.ts | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/firefox/manifest.json b/firefox/manifest.json index 1a94b2e..4ad0f97 100644 --- a/firefox/manifest.json +++ b/firefox/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "Nordcraft", - "version": "2.3.4", + "version": "2.3.5", "description": "Browser extension for the Nordcraft editor.", "background": { "scripts": [ diff --git a/firefox/src/background.ts b/firefox/src/background.ts index b46e07f..c42fe98 100644 --- a/firefox/src/background.ts +++ b/firefox/src/background.ts @@ -14,11 +14,11 @@ setup() /** * Used to send notifications to the Nordcraft editor about which cookies are set */ -const notifyUser = async (requestedUrl: string) => { +const notifyUser = async (requestedUrl: string, domain?: string) => { try { const url = new URL(requestedUrl) const domainCookies = await browser.cookies.getAll({ - domain: url.host, + domain: domain ?? url.host, }) const cookies = domainCookies.map((c) => c.httpOnly diff --git a/shared/setCookies.ts b/shared/setCookies.ts index 6ef6915..aea3291 100644 --- a/shared/setCookies.ts +++ b/shared/setCookies.ts @@ -3,7 +3,7 @@ export interface SetCookiesArguments { setCookieHeaders: string[] setCookie: (cookie: ParsedCookie & { url: string }, domain?: string) => void removeCookie: (cookie: { name: string; url: string }, domain?: string) => void - notifyUser: (requestedUrl: string) => void + notifyUser: (requestedUrl: string, domain: string) => void } export function setCookies({ @@ -65,7 +65,7 @@ export function setCookies({ ) } try { - notifyUser(url) + notifyUser(url, requestedUrl.host) } catch {} }, )