From 0dede8d7fe4b636a8adff4c78e74a41e7767483f Mon Sep 17 00:00:00 2001 From: Drew McMillan Date: Mon, 16 Nov 2026 15:17:19 +0000 Subject: [PATCH 1/6] feat: add embed mode for iframing pages into Parallel Squared ?embed strips the page chrome and reports the content height to the parent window via postMessage so the host iframe can auto-size. --- functions/index.js | 8 ++-- functions/views/_layout.pug | 88 +++++++++++++++++++------------------ static/scripts/challenge.js | 4 +- static/scripts/embed.js | 42 ++++++++++++++++++ static/scripts/main.js | 5 ++- static/styles/iframe.less | 32 ++++++++++++++ 6 files changed, 132 insertions(+), 47 deletions(-) create mode 100644 static/scripts/embed.js diff --git a/functions/index.js b/functions/index.js index bf2d718d..d32f95d0 100644 --- a/functions/index.js +++ b/functions/index.js @@ -123,14 +123,16 @@ app.use((req, res, next) => { res.locals.firebaseClientConfig = firebaseClientConfig; res.locals.isBeta = isBeta; - if (req.user && req.user.showWelcomeMsg && !req.query.latest && !req.path.includes('/api')) { + if (req.user && req.user.showWelcomeMsg && !req.query.latest && !('embed' in req.query) &&!req.path.includes('/api')) { userDB.doc(req.user.uid) // async .update({showWelcomeMsg: false}) .catch(() => console.error('Failed to update welcome msg', req.user.uid)); } - if (req.query.iframe) { - res.locals.insideIframe = true; + if ('embed' in req.query) { + res.locals.embed = true; + res.locals.sidebarDisabled = true; + res.locals.privacyModalDisabled = true; } for (let l of LEVELS) { diff --git a/functions/views/_layout.pug b/functions/views/_layout.pug index 3d8d330f..f29f6c78 100644 --- a/functions/views/_layout.pug +++ b/functions/views/_layout.pug @@ -1,7 +1,9 @@ doctype html -html +html(class=embed ? 'embed' : undefined) head meta(charset="utf-8") + if embed + base(target="_top") meta(http-equiv="X-UA-Compatible" content="IE=edge") title Parallel by Simon Singh @@ -38,39 +40,40 @@ html body #vue - if isBeta + if isBeta && !embed .beta div You are on our Beta Testing Site! div The information & events on this page are for testing purposes and should not be followed. a(href="https://parallel.org.uk") Click here to return to the production site. - .headerWrapper - .header - if !sidebarDisabled - #menu(v-on:click="showSidebar=true") - svg(width=24 height=24) - rect(y=2 width=24 height=3 fill="white") - rect(y=10 width=24 height=3 fill="white") - rect(y=18 width=24 height=3 fill="white") - - if path && !noNavigation - div(class="homeButtonsLeft") - a#home(href="/" title="Home" class="homeButton homeButtonWithSvg") - include svgs/home.svg - | Home - - if user - div(class="homeButtonsRight") - a#home(href="/account" title="Home" class="homeButton") - | Settings - a#home(href="/" title="Home" class="homeButton" @click="user.logout") - | Sign out - - if noNavigation - div#logo(href="/" title="Home" class=(sidebarDisabled ? 'sidebarDisabled' : '')) - else - a#logo(href="/" title="Home" class=(sidebarDisabled ? 'sidebarDisabled' : '')) - - #shadow(v-on:click="showSidebar=false" v-bind:class="{visible: showSidebar}") + if !embed + .headerWrapper + .header + if !sidebarDisabled + #menu(v-on:click="showSidebar=true") + svg(width=24 height=24) + rect(y=2 width=24 height=3 fill="white") + rect(y=10 width=24 height=3 fill="white") + rect(y=18 width=24 height=3 fill="white") + + if path && !noNavigation + div(class="homeButtonsLeft") + a#home(href="/" title="Home" class="homeButton homeButtonWithSvg") + include svgs/home.svg + | Home + + if user + div(class="homeButtonsRight") + a#home(href="/account" title="Home" class="homeButton") + | Settings + a#home(href="/" title="Home" class="homeButton" @click="user.logout") + | Sign out + + if noNavigation + div#logo(href="/" title="Home" class=(sidebarDisabled ? 'sidebarDisabled' : '')) + else + a#logo(href="/" title="Home" class=(sidebarDisabled ? 'sidebarDisabled' : '')) + + #shadow(v-on:click="showSidebar=false" v-bind:class="{visible: showSidebar}") #container if !sidebarDisabled @@ -161,7 +164,7 @@ html //- label(for="terms-modal") We will email you when a new Parallelogram challenge is released, when the next Parallel Circles session is scheduled, and when there are special competitions, prizes and opportunities. By checking this box you consent to receiving these emails, in accordance with our #[a(href="/privacy-notice" target="_blank") Data Privacy policy]. //- button(type="submit") Continue - if user && !user.code && user.showWelcomeMsg + if user && !user.code && user.showWelcomeMsg && !embed .modal(v-if="showWelcomeMsg") .modal-background .modal-body @@ -169,17 +172,18 @@ html p.text-center We’ve updated your level to #[strong #{levelNamesWithAges[user.level]}]. You can always change it on your #[a(href="/account") account settings] page. The first Parallelogram is now available, but officially it is launched in early September. We will be releasing new Parallelograms every week, from mid-September. button(type="submit" v-on:click="showWelcomeMsg = false") Continue - footer - a(href="/about") About - | • - a(href="/contact") Contact - | • - a(href="/privacy-notice") Privacy Notice - br - | Created by - a(href="http://goodthinkingsociety.org" target="_blank") #[img(src="/images/good-thinking.png" width=10 height=16)] Good Thinking Society - | and - a(href="https://mathigon.org" target="_blank") #[img(src="/images/mathigon.png" width=16 height=16)] Mathigon + if !embed + footer + a(href="/about") About + | • + a(href="/contact") Contact + | • + a(href="/privacy-notice") Privacy Notice + br + | Created by + a(href="http://goodthinkingsociety.org" target="_blank") #[img(src="/images/good-thinking.png" width=10 height=16)] Good Thinking Society + | and + a(href="https://mathigon.org" target="_blank") #[img(src="/images/mathigon.png" width=16 height=16)] Mathigon if user && user.privacy && user.privacy.visible && !privacyModalDisabled iframe(src="/privacy-modal" id="privacy-iframe" style="height: 100%;width: 100%;position: absolute;z-index: 10000000000;top: 0;left: 0;border: none;") diff --git a/static/scripts/challenge.js b/static/scripts/challenge.js index 164ed8ec..6461a6d5 100644 --- a/static/scripts/challenge.js +++ b/static/scripts/challenge.js @@ -54,7 +54,9 @@ export default function() { testPasswordSubmit(e) { e.preventDefault(); - window.location.search = `?p=${challenge.testPassword}`; + const params = new URLSearchParams(window.location.search); + params.set('p', challenge.testPassword); + window.location.search = params.toString(); }, // Only used by teachers diff --git a/static/scripts/embed.js b/static/scripts/embed.js new file mode 100644 index 00000000..b38600d1 --- /dev/null +++ b/static/scripts/embed.js @@ -0,0 +1,42 @@ +// ============================================================================= +// Embed height reporting (used when ?embed is set and inside an iframe) +// ============================================================================= + + +const MESSAGE_TYPE = 'parallel:embed:height'; + +export default function initEmbed() { + const root = document.documentElement; + if (!root.classList.contains('embed') || window.parent === window) return; + + let lastHeight = 0; + let frame = 0; + + const post = () => { + frame = 0; + const height = Math.ceil(root.getBoundingClientRect().height); + // Body is hidden (height 0) while reloading after a submission. + if (!height || height === lastHeight) return; + lastHeight = height; + window.parent.postMessage({type: MESSAGE_TYPE, height}, '*'); + }; + + const schedule = () => { + if (!frame) frame = window.requestAnimationFrame(post); + }; + + if (window.ResizeObserver) { + const observer = new ResizeObserver(schedule); + observer.observe(root); + observer.observe(document.body); + } else { + window.setInterval(schedule, 500); + } + + window.addEventListener('resize', schedule); + window.addEventListener('load', schedule); + document.addEventListener('load', schedule, true); + if (document.fonts && document.fonts.ready) document.fonts.ready.then(schedule); + + schedule(); +} diff --git a/static/scripts/main.js b/static/scripts/main.js index 9533bbdb..6385bb4e 100755 --- a/static/scripts/main.js +++ b/static/scripts/main.js @@ -7,6 +7,7 @@ import '../../node_modules/babel-polyfill/dist/polyfill'; import getUser from './user'; import getChallenge from './challenge'; +import initEmbed from './embed'; import '../../node_modules/@johmun/vue-tags-input/dist/vue-tags-input'; function removeStudent(id, index, level) { @@ -128,7 +129,7 @@ document.addEventListener('DOMContentLoaded', function() { }); if (window.self !== window.top) { - document.body.className = 'insideIframe' + document.body.classList.add('insideIframe'); } window.app = new Vue({ @@ -137,4 +138,6 @@ document.addEventListener('DOMContentLoaded', function() { data: {showSidebar: false, showWelcomeMsg: true, user, c: challenge, removeStudent, sortTable} }); + + initEmbed(); }); diff --git a/static/styles/iframe.less b/static/styles/iframe.less index 4e0c91d7..97f78646 100644 --- a/static/styles/iframe.less +++ b/static/styles/iframe.less @@ -30,3 +30,35 @@ max-width: 100%; } } + + +// ----------------------------------------------------------------------------- +// Embedded within the Parallel Squared site (?embed) + +html.embed { + background: transparent; + overflow: hidden; + + &, body { height: auto; min-height: 0; } + + #container { + display: block; + width: 100%; + max-width: 100%; + } + + article, article.sidebarDisabled { + background: transparent; + border-radius: 0; + box-shadow: none; + padding: 0; + width: 100%; + min-height: 0; + overflow: visible; + } + + header { + margin: 0 0 32px 0; + border-radius: 4px; + } +} From ad43e3e3a1bc6b53c9e08523c929d813bcc7142a Mon Sep 17 00:00:00 2001 From: Drew McMillan Date: Mon, 16 Nov 2026 15:23:28 +0000 Subject: [PATCH 2/6] fix: harden embed mode from review feedback Keep embed on static and error pages, bump asset versions, restrict framing to same origin, drop embed when not framed, and open external links in the top window without breaking in-page anchors. --- functions/index.js | 8 +++++--- functions/views/_layout.pug | 6 ++---- static/scripts/embed.js | 18 +++++++++++++++++- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/functions/index.js b/functions/index.js index d32f95d0..6350bbde 100644 --- a/functions/index.js +++ b/functions/index.js @@ -79,7 +79,8 @@ function scoreClass(score) { function error(res, code) { res.status(code); res.locals = { - sidebarDisabled: true + sidebarDisabled: true, + embed: res.locals.embed } return res.render('error', {code}); } @@ -133,6 +134,7 @@ app.use((req, res, next) => { res.locals.embed = true; res.locals.sidebarDisabled = true; res.locals.privacyModalDisabled = true; + res.set('Content-Security-Policy', "frame-ancestors 'self' http://localhost:*"); } for (let l of LEVELS) { @@ -471,8 +473,8 @@ const pagesWithoutPrivacyModal = ['privacy-notice'] for (let p of ['about', 'introduction', 'parents', 'teachers', 'privacy-notice', 'safeguarding', 'hints-tips', 'job-ad', 'developer-support', 'pmc', 'primary-parallel', 'masterclass', 'troubleshooting', 'tutorinfo', 'academy-primary-parent', 'academy-primary-teacher']) { const content = fs.readFileSync(path.join(__dirname, `build/${p}.html`)); app.get('/' + p, (_, res) => { - res.locals.sidebarDisabled = pagesWithoutSidebar.includes(p); - res.locals.privacyModalDisabled = pagesWithoutPrivacyModal.includes(p); + res.locals.sidebarDisabled = res.locals.embed || pagesWithoutSidebar.includes(p); + res.locals.privacyModalDisabled = res.locals.embed || pagesWithoutPrivacyModal.includes(p); res.render('_layout', {content}) }); } diff --git a/functions/views/_layout.pug b/functions/views/_layout.pug index f29f6c78..c8f2885a 100644 --- a/functions/views/_layout.pug +++ b/functions/views/_layout.pug @@ -2,8 +2,6 @@ doctype html html(class=embed ? 'embed' : undefined) head meta(charset="utf-8") - if embed - base(target="_top") meta(http-equiv="X-UA-Compatible" content="IE=edge") title Parallel by Simon Singh @@ -28,7 +26,7 @@ html(class=embed ? 'embed' : undefined) //- Stylesheets link(href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600,300,400italic" rel="stylesheet") - link(href="/parallel.css?v=34" rel="stylesheet") + link(href="/parallel.css?v=35" rel="stylesheet") if isProduction include scripts/newrelic.html @@ -192,7 +190,7 @@ html(class=embed ? 'embed' : undefined) script(defer src="https://www.gstatic.com/firebasejs/8.10.0/firebase-auth.js") script(defer src="https://www.gstatic.com/firebasejs/8.10.0/firebase-firestore.js") script(defer src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.9/vue.min.js") - script(defer src="/parallel.js?v=39") + script(defer src="/parallel.js?v=40") script. if (typeof window !== 'undefined' && window.newrelic && window.newrelic.setCustomAttribute) { diff --git a/static/scripts/embed.js b/static/scripts/embed.js index b38600d1..b0209a2f 100644 --- a/static/scripts/embed.js +++ b/static/scripts/embed.js @@ -7,7 +7,23 @@ const MESSAGE_TYPE = 'parallel:embed:height'; export default function initEmbed() { const root = document.documentElement; - if (!root.classList.contains('embed') || window.parent === window) return; + if (!root.classList.contains('embed')) return; + + // Embed mode hides the privacy modal, so only allow it inside a frame. + if (window.parent === window) { + const url = new URL(window.location.href); + if (!url.searchParams.has('embed')) return; + url.searchParams.delete('embed'); + return window.location.replace(url.toString()); + } + + // Links to other pages open in the top window; in-page anchors stay in the frame. + document.addEventListener('click', (e) => { + const link = e.target.closest && e.target.closest('a[href]'); + if (!link || link.target) return; + const samePage = link.href.split('#')[0] === window.location.href.split('#')[0]; + if (!samePage) link.target = '_top'; + }, true); let lastHeight = 0; let frame = 0; From b6fddf947378f728594785a5c56bae79cc6ec83e Mon Sep 17 00:00:00 2001 From: Drew McMillan Date: Mon, 16 Nov 2026 17:03:55 +0000 Subject: [PATCH 3/6] feat: show level and PG picker sidebar in embed mode Hide only the intro, circles and account buttons; PG links point at /pg/:pid, and on mobile the picker stacks below the content. --- functions/index.js | 3 +-- functions/views/_layout.pug | 4 ++-- static/styles/iframe.less | 31 +++++++++++++++++++++++++++++-- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/functions/index.js b/functions/index.js index 6350bbde..bee6fb0b 100644 --- a/functions/index.js +++ b/functions/index.js @@ -132,7 +132,6 @@ app.use((req, res, next) => { if ('embed' in req.query) { res.locals.embed = true; - res.locals.sidebarDisabled = true; res.locals.privacyModalDisabled = true; res.set('Content-Security-Policy', "frame-ancestors 'self' http://localhost:*"); } @@ -473,7 +472,7 @@ const pagesWithoutPrivacyModal = ['privacy-notice'] for (let p of ['about', 'introduction', 'parents', 'teachers', 'privacy-notice', 'safeguarding', 'hints-tips', 'job-ad', 'developer-support', 'pmc', 'primary-parallel', 'masterclass', 'troubleshooting', 'tutorinfo', 'academy-primary-parent', 'academy-primary-teacher']) { const content = fs.readFileSync(path.join(__dirname, `build/${p}.html`)); app.get('/' + p, (_, res) => { - res.locals.sidebarDisabled = res.locals.embed || pagesWithoutSidebar.includes(p); + res.locals.sidebarDisabled = pagesWithoutSidebar.includes(p); res.locals.privacyModalDisabled = res.locals.embed || pagesWithoutPrivacyModal.includes(p); res.render('_layout', {content}) }); diff --git a/functions/views/_layout.pug b/functions/views/_layout.pug index c8f2885a..0545684b 100644 --- a/functions/views/_layout.pug +++ b/functions/views/_layout.pug @@ -26,7 +26,7 @@ html(class=embed ? 'embed' : undefined) //- Stylesheets link(href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600,300,400italic" rel="stylesheet") - link(href="/parallel.css?v=35" rel="stylesheet") + link(href="/parallel.css?v=36" rel="stylesheet") if isProduction include scripts/newrelic.html @@ -139,7 +139,7 @@ html(class=embed ? 'embed' : undefined) for l in user ? user.sidebarLevels : levels nav(v-show=`user.level == '${l}'`) for p in pages[l] - a(href=("/" + p.url) class=('/' + p.url === path ? 'active' : '')) + a(href=(embed ? "/pg/" : "/") + p.url class=('/' + p.url === path ? 'active' : '')) .title-meta PG #{p.index} #[span.spaced •] #{p.date} .title= p.title if user && user.answers[p.url] && user.answers[p.url].submitted diff --git a/static/styles/iframe.less b/static/styles/iframe.less index 97f78646..54324327 100644 --- a/static/styles/iframe.less +++ b/static/styles/iframe.less @@ -42,23 +42,50 @@ html.embed { &, body { height: auto; min-height: 0; } #container { - display: block; width: 100%; max-width: 100%; } + .intros, + .sidebar-circles-item, + .sidebar-field { + display: none; + } + + .sidebar { margin: 0 24px 0 0; } + + .sidebar-wrap { + position: static; + max-height: none; + overflow: visible; + border-radius: 4px; + } + article, article.sidebarDisabled { background: transparent; border-radius: 0; box-shadow: none; padding: 0; - width: 100%; min-height: 0; overflow: visible; } + article.sidebarDisabled { width: 100%; } + + @media screen and (max-width: 720px) { + #container { display: flex; flex-direction: column; } + article, article.sidebarDisabled { width: 100%; order: -1; } + .sidebar { margin: 32px 0 0; width: 100%; } + .sidebar-wrap { transform: none; box-shadow: none; width: auto; } + } + header { margin: 0 0 32px 0; border-radius: 4px; } + + .status-banner { + margin: 0 0 32px; + border-radius: 4px; + } } From a6c570941b1ad7fc10fbc060b2523d90d43f886c Mon Sep 17 00:00:00 2001 From: Drew McMillan Date: Mon, 16 Nov 2026 17:09:41 +0000 Subject: [PATCH 4/6] feat: keep white article card in embed mode Article keeps its white background and padding so the content reads as a card on the host page's background. --- functions/views/_layout.pug | 2 +- static/styles/iframe.less | 20 ++------------------ 2 files changed, 3 insertions(+), 19 deletions(-) diff --git a/functions/views/_layout.pug b/functions/views/_layout.pug index 0545684b..70551f7b 100644 --- a/functions/views/_layout.pug +++ b/functions/views/_layout.pug @@ -26,7 +26,7 @@ html(class=embed ? 'embed' : undefined) //- Stylesheets link(href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600,300,400italic" rel="stylesheet") - link(href="/parallel.css?v=36" rel="stylesheet") + link(href="/parallel.css?v=37" rel="stylesheet") if isProduction include scripts/newrelic.html diff --git a/static/styles/iframe.less b/static/styles/iframe.less index 54324327..c7003cdb 100644 --- a/static/styles/iframe.less +++ b/static/styles/iframe.less @@ -62,30 +62,14 @@ html.embed { } article, article.sidebarDisabled { - background: transparent; - border-radius: 0; box-shadow: none; - padding: 0; min-height: 0; - overflow: visible; } - article.sidebarDisabled { width: 100%; } - @media screen and (max-width: 720px) { #container { display: flex; flex-direction: column; } - article, article.sidebarDisabled { width: 100%; order: -1; } - .sidebar { margin: 32px 0 0; width: 100%; } + article, article.sidebarDisabled { width: auto; order: -1; padding: 24px; border-radius: 4px; } + .sidebar { margin: 24px 0 0; width: 100%; } .sidebar-wrap { transform: none; box-shadow: none; width: auto; } } - - header { - margin: 0 0 32px 0; - border-radius: 4px; - } - - .status-banner { - margin: 0 0 32px; - border-radius: 4px; - } } From 4311968db3bb9f54d8b2d7e60520e0a8563f3127 Mon Sep 17 00:00:00 2001 From: Drew McMillan Date: Mon, 16 Nov 2026 17:22:27 +0000 Subject: [PATCH 5/6] feat: point embedded PG links at /secret/:pid --- functions/views/_layout.pug | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/views/_layout.pug b/functions/views/_layout.pug index 70551f7b..e23c3c8e 100644 --- a/functions/views/_layout.pug +++ b/functions/views/_layout.pug @@ -139,7 +139,7 @@ html(class=embed ? 'embed' : undefined) for l in user ? user.sidebarLevels : levels nav(v-show=`user.level == '${l}'`) for p in pages[l] - a(href=(embed ? "/pg/" : "/") + p.url class=('/' + p.url === path ? 'active' : '')) + a(href=(embed ? "/secret/" : "/") + p.url class=('/' + p.url === path ? 'active' : '')) .title-meta PG #{p.index} #[span.spaced •] #{p.date} .title= p.title if user && user.answers[p.url] && user.answers[p.url].submitted From dc2b541b18136399fce93e0b2d3bbccd5e5aa449 Mon Sep 17 00:00:00 2001 From: Drew McMillan Date: Mon, 16 Nov 2026 17:26:18 +0000 Subject: [PATCH 6/6] =?UTF-8?q?=F0=9F=94=A7=20Continue=20to=20implement=20?= =?UTF-8?q?and=20debug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- static/styles/forms.less | 2 +- static/styles/iframe.less | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/static/styles/forms.less b/static/styles/forms.less index 37bd201d..80a274a8 100644 --- a/static/styles/forms.less +++ b/static/styles/forms.less @@ -224,7 +224,7 @@ form.loading button[type="submit"] { .toggle { display: flex; flex-direction: row; - margin: 6px 0 4px;; + margin: 0px 0 6px 4px; justify-content: center; padding: 0 2px; } diff --git a/static/styles/iframe.less b/static/styles/iframe.less index c7003cdb..d6047fde 100644 --- a/static/styles/iframe.less +++ b/static/styles/iframe.less @@ -52,7 +52,7 @@ html.embed { display: none; } - .sidebar { margin: 0 24px 0 0; } + .sidebar { margin: 0 6px 0 0; } .sidebar-wrap { position: static;