Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -17,7 +17,8 @@
"cookies",
"webNavigation",
"declarativeNetRequest",
"webRequest"
"webRequest",
"tabs"
],
"host_permissions": [
"https://*.toddle.site/*",
Expand All @@ -26,7 +27,6 @@
"content_scripts": [
{
"matches": [
"https://*.toddle.dev/*",
"https://*.toddle.site/*",
"https://editor.nordcraft.com/*",
"https://*.nordcraft.site/*"
Expand Down
90 changes: 61 additions & 29 deletions chrome/src/background.ts
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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: [
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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)
Expand Down
43 changes: 39 additions & 4 deletions chrome/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 1 addition & 2 deletions firefox/manifest.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -26,7 +26,6 @@
"content_scripts": [
{
"matches": [
"https://*.toddle.dev/*",
"https://*.toddle.site/*",
"https://editor.nordcraft.com/*",
"https://*.nordcraft.site/*"
Expand Down
19 changes: 12 additions & 7 deletions firefox/src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
})
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions shared/setCookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -65,7 +65,7 @@ export function setCookies({
)
}
try {
notifyUser(url)
notifyUser(url, requestedUrl.host)
} catch {}
},
)
Expand Down