From f9876c7ccf5647a6147429319090ba67e655d61f Mon Sep 17 00:00:00 2001 From: Miguel Montalvo Date: Sun, 20 Mar 2022 14:09:40 -0700 Subject: [PATCH 1/4] test: run javascript from urls --- tests/platform/iframe/iframe.spec.ts | 4 ++++ tests/platform/iframe/index.html | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/tests/platform/iframe/iframe.spec.ts b/tests/platform/iframe/iframe.spec.ts index 33faf3c9..18b5606a 100644 --- a/tests/platform/iframe/iframe.spec.ts +++ b/tests/platform/iframe/iframe.spec.ts @@ -135,4 +135,8 @@ test('iframe', async ({ page }) => { await page.waitForSelector('.testWindowFrames'); const testWindowFrames = page.locator('#testWindowFrames'); await expect(testWindowFrames).toHaveText('window-frames'); + + await page.waitForSelector('.testSetJavascriptSrc'); + const testSetJavascriptSrc = page.locator('#testSetJavascriptSrc'); + await expect(testSetJavascriptSrc).toHaveText('javascript:void(0);'); }); diff --git a/tests/platform/iframe/index.html b/tests/platform/iframe/index.html index 126cfc48..8d4cede2 100644 --- a/tests/platform/iframe/index.html +++ b/tests/platform/iframe/index.html @@ -854,6 +854,23 @@

Iframe

})(); + +
  • + set javascript: source + + +

  • From 52e37220588cbbba944d73a31c47adaa6c2728d6 Mon Sep 17 00:00:00 2001 From: Miguel Montalvo Date: Sun, 20 Mar 2022 14:27:30 -0700 Subject: [PATCH 2/4] feat: run javascript if the url contains the javascript: pseudo-protocol --- src/lib/web-worker/worker-iframe.ts | 10 +++++++++- src/lib/web-worker/worker-node.ts | 7 ++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/lib/web-worker/worker-iframe.ts b/src/lib/web-worker/worker-iframe.ts index a44e8f7c..8b9e5e11 100644 --- a/src/lib/web-worker/worker-iframe.ts +++ b/src/lib/web-worker/worker-iframe.ts @@ -35,10 +35,18 @@ export const patchHTMLIFrameElement = (WorkerHTMLIFrameElement: any, env: WebWor src: { get() { - let src = getIframeEnv(this).$location$.href; + let src = getter(this, ['src']); + if (src.startsWith('javascript:')) { + return src; + } + src = getIframeEnv(this).$location$.href; return src.startsWith('about:') ? '' : src; }, set(src: string) { + if (src.startsWith('javascript:')) { + setter(this, ['src'], src); + return; + } if (!src.startsWith('about:')) { let xhr = new XMLHttpRequest(); let xhrStatus: number; diff --git a/src/lib/web-worker/worker-node.ts b/src/lib/web-worker/worker-node.ts index 405b5520..e0d4eb88 100644 --- a/src/lib/web-worker/worker-node.ts +++ b/src/lib/web-worker/worker-node.ts @@ -1,5 +1,5 @@ import { cachedTreeProps } from './worker-constructors'; -import { callMethod, setter, sendToMain } from './worker-proxy'; +import { callMethod, setter, getter, sendToMain } from './worker-proxy'; import { CallType, NodeName, @@ -69,6 +69,11 @@ export const createNodeCstr = ( if (isIFrame) { // an iframe element's instanceId is also // the winId of its contentWindow + const src = getter(newNode, ['src']); + if (src.startsWith('javascript:')) { + const scriptContent = src.split('javascript:')[1]; + runScriptContent(env, instanceId, scriptContent, winId, ''); + } insertIframe(instanceId, newNode); } if (isScript) { From 86e1598df8132206d8b0e54852959cb7adf27b2c Mon Sep 17 00:00:00 2001 From: Miguel Montalvo Date: Wed, 23 Mar 2022 18:09:08 -0700 Subject: [PATCH 3/4] avoid calls to the main thread * using getInstanceStateValue() instead of getter() * using setInstanceStateValue() instead of setter() --- src/lib/types.ts | 1 + src/lib/web-worker/worker-iframe.ts | 8 ++++---- src/lib/web-worker/worker-node.ts | 6 +++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/lib/types.ts b/src/lib/types.ts index 6c7244d9..a23185d3 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -525,6 +525,7 @@ export const enum NodeName { export const enum StateProp { errorHandlers = 'error', loadHandlers = 'load', + src = 0, loadErrorStatus = 1, cssRules = 2, innerHTML = 3, diff --git a/src/lib/web-worker/worker-iframe.ts b/src/lib/web-worker/worker-iframe.ts index 8b9e5e11..9177173b 100644 --- a/src/lib/web-worker/worker-iframe.ts +++ b/src/lib/web-worker/worker-iframe.ts @@ -10,7 +10,7 @@ import { import { getPartytownScript, resolveUrl } from './worker-exec'; import { getter, sendToMain, setter } from './worker-proxy'; import { HTMLSrcElementDescriptorMap } from './worker-src-element'; -import { setInstanceStateValue } from './worker-state'; +import { setInstanceStateValue, getInstanceStateValue } from './worker-state'; import { StateProp, WebWorkerEnvironment, @@ -35,8 +35,8 @@ export const patchHTMLIFrameElement = (WorkerHTMLIFrameElement: any, env: WebWor src: { get() { - let src = getter(this, ['src']); - if (src.startsWith('javascript:')) { + let src = getInstanceStateValue(this, StateProp.src); + if (src && src.startsWith('javascript:')) { return src; } src = getIframeEnv(this).$location$.href; @@ -44,7 +44,7 @@ export const patchHTMLIFrameElement = (WorkerHTMLIFrameElement: any, env: WebWor }, set(src: string) { if (src.startsWith('javascript:')) { - setter(this, ['src'], src); + setInstanceStateValue(this, StateProp.src, src); return; } if (!src.startsWith('about:')) { diff --git a/src/lib/web-worker/worker-node.ts b/src/lib/web-worker/worker-node.ts index e0d4eb88..0bf78bd3 100644 --- a/src/lib/web-worker/worker-node.ts +++ b/src/lib/web-worker/worker-node.ts @@ -1,5 +1,5 @@ import { cachedTreeProps } from './worker-constructors'; -import { callMethod, setter, getter, sendToMain } from './worker-proxy'; +import { callMethod, setter, sendToMain } from './worker-proxy'; import { CallType, NodeName, @@ -69,8 +69,8 @@ export const createNodeCstr = ( if (isIFrame) { // an iframe element's instanceId is also // the winId of its contentWindow - const src = getter(newNode, ['src']); - if (src.startsWith('javascript:')) { + const src = getInstanceStateValue(newNode, StateProp.src); + if (src && src.startsWith('javascript:')) { const scriptContent = src.split('javascript:')[1]; runScriptContent(env, instanceId, scriptContent, winId, ''); } From 10d5ffe6b993b74316261de36fb53b5d8eb12588 Mon Sep 17 00:00:00 2001 From: Miguel Montalvo Date: Thu, 24 Mar 2022 01:31:17 -0700 Subject: [PATCH 4/4] check the presence of the source --- src/lib/web-worker/worker-iframe.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/lib/web-worker/worker-iframe.ts b/src/lib/web-worker/worker-iframe.ts index 9177173b..3d98f7b4 100644 --- a/src/lib/web-worker/worker-iframe.ts +++ b/src/lib/web-worker/worker-iframe.ts @@ -43,6 +43,9 @@ export const patchHTMLIFrameElement = (WorkerHTMLIFrameElement: any, env: WebWor return src.startsWith('about:') ? '' : src; }, set(src: string) { + if (!src) { + return; + } if (src.startsWith('javascript:')) { setInstanceStateValue(this, StateProp.src, src); return;