diff --git a/chrome/manifest.json b/chrome/manifest.json index 282a3c6..945eba4 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", @@ -17,7 +17,8 @@ "cookies", "webNavigation", "declarativeNetRequest", - "webRequest" + "webRequest", + "tabs" ], "host_permissions": [ "https://*.toddle.site/*", @@ -26,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 8a529b1..fdd5e44 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: [ @@ -61,6 +40,48 @@ 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('nordcraft.com') === false && + url.host.endsWith('-toddle.toddle.site') === false + ) { + return false + } + + // Get the cookies for the .toddle.site domain + const frames = await chrome.webNavigation.getAllFrames({ + tabId: activeInfo.tabId, + }) + + const frameUrl = frames + ?.map((frame) => { + const url = new URL(frame.url) + if (url.host?.endsWith('.toddle.site')) { + return frame.url + } + }) + .filter((v) => v)[0] + + if (!frameUrl) { + return + } + + await getCookiesAndUpdateSessionRules({ url: frameUrl, RULE_ID }) +}) + chrome.webRequest.onHeadersReceived.addListener( (info) => { // check the parent frame so we only override cookies if we are on nordcraft.com @@ -71,6 +92,11 @@ chrome.webRequest.onHeadersReceived.addListener( if (!isNordcraft) { return undefined } + + if (!info.initiator) { + return undefined + } + if (info.responseHeaders) { const setCookieHeaders = info.responseHeaders .filter( @@ -88,6 +114,12 @@ chrome.webRequest.onHeadersReceived.addListener( setCookieHeaders, requestUrl: info.url, setCookie: async (cookie, domain) => { + if ( + !domain?.endsWith('.toddle.site') && + !domain?.endsWith('.nordcraft.site') + ) { + return + } await chrome.cookies.set(cookie) const parsedUrl = new URL(cookie.url) 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..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": [ @@ -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 c485f38..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 @@ -107,7 +107,15 @@ browser.webRequest.onHeadersReceived.addListener( setCookies({ setCookieHeaders, requestUrl: info.url, - setCookie: (cookie) => browser.cookies.set(cookie), + setCookie: (cookie, domain) => { + if ( + !domain?.endsWith('.toddle.site') && + !domain?.endsWith('.nordcraft.site') + ) { + return + } + browser.cookies.set(cookie) + }, removeCookie: (cookie) => browser.cookies.remove(cookie), notifyUser, }) @@ -146,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 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 {} }, )