From 17771bd0deaf2bca17a35bc4473144dfd2f288d4 Mon Sep 17 00:00:00 2001 From: weizman Date: Tue, 10 Dec 2024 18:50:50 +0200 Subject: [PATCH 1/9] poc --- app/scripts/use-snow.js | 93 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/app/scripts/use-snow.js b/app/scripts/use-snow.js index 87d00be3dc87..285fd2917689 100644 --- a/app/scripts/use-snow.js +++ b/app/scripts/use-snow.js @@ -7,6 +7,98 @@ Changing this code must be done cautiously to avoid breaking the app! // eslint-disable-next-line import/unambiguous (function () { + function tameDOM(win) { + // return; + function generateTamedWindow(win) { + return new Proxy(win, { + get: (a,b,c) => { + if (b === 'document') { + return proxy; + } + if (![ + "nodeName", + "Element", + "HTMLElement", + "getComputedStyle", + "visualViewport", + "addEventListener", + "removeEventListener", + "toString", + "pageXOffset", + "pageYOffset", + ].includes(b)) { + throw 'NOT ALLOWED(window): ' + b; + } + let ret = Reflect.get(a,b); + if (typeof ret === 'function') { + ret = ret.bind(win); + } + return ret; + } + }); + } + function generateTamedDocument(doc, winp) { + return new Proxy(doc, { + get: (a,b,c) => { + if (b === 'defaultView') { + // return window; + return winp; + } + if (![ + "body", + "host", + "scrollingElement", + "nodeType", + "documentElement", + "activeElement", + "parentNode", + "addEventListener", + "createElement", + "createElementNS", + "createTextNode" + ].includes(b) && + !b.startsWith('__reactInternalInstance') && + !b.startsWith('__reactContainer') + ) { + throw 'NOT ALLOWED(document): ' + b; + } + let ret = Reflect.get(a,b); + if (typeof ret === 'function') { + ret = ret.bind(doc); + } + return ret; + } + }); + } + try {win.Node} catch {return;} + const {document, Object, Node} = win; + const winp = generateTamedWindow(win); + const proxy = generateTamedDocument(document, winp); + Object.defineProperty(Node.prototype, 'ownerDocument', {get: function() { + return proxy; + }} + ); + const getRootNode = Object.getOwnPropertyDescriptor(Node.prototype, 'getRootNode').value; + Object.defineProperty(Node.prototype, 'getRootNode', {value: function() { + const that = this === proxy ? document : this; + const ret = getRootNode.call(that); + if (ret === document) { + return proxy; + } + return ret; + }} + ); + const parentNode = Object.getOwnPropertyDescriptor(Node.prototype, 'parentNode').get; + Object.defineProperty(Node.prototype, 'parentNode', {get: function() { + const that = this === proxy ? document : this; + const ret = parentNode.call(that); + if (ret === document) { + return proxy; + } + return ret; + }} + ); + } const log = console.log.bind(console); // eslint-disable-next-line no-undef const isWorker = !self.document; @@ -21,6 +113,7 @@ Changing this code must be done cautiously to avoid breaking the app! // eslint-disable-next-line no-undef self.SNOW((win) => { log(msg, win); + tameDOM(win); scuttle(win); }, realm); } From da352af5bc6a83208ab05b3dacd91497e620f000 Mon Sep 17 00:00:00 2001 From: weizman Date: Tue, 10 Dec 2024 19:12:43 +0200 Subject: [PATCH 2/9] poc --- app/scripts/use-snow.js | 1 + 1 file changed, 1 insertion(+) diff --git a/app/scripts/use-snow.js b/app/scripts/use-snow.js index 285fd2917689..6688111b9291 100644 --- a/app/scripts/use-snow.js +++ b/app/scripts/use-snow.js @@ -47,6 +47,7 @@ Changing this code must be done cautiously to avoid breaking the app! if (![ "body", "host", + "shadowRoot", "scrollingElement", "nodeType", "documentElement", From f4e37abd6633b6d65f8e1205ef17e67285f406d5 Mon Sep 17 00:00:00 2001 From: weizman Date: Tue, 10 Dec 2024 21:31:16 +0200 Subject: [PATCH 3/9] poc --- app/scripts/use-snow.js | 1 + 1 file changed, 1 insertion(+) diff --git a/app/scripts/use-snow.js b/app/scripts/use-snow.js index 6688111b9291..5b9a3ff6b96a 100644 --- a/app/scripts/use-snow.js +++ b/app/scripts/use-snow.js @@ -56,6 +56,7 @@ Changing this code must be done cautiously to avoid breaking the app! "addEventListener", "createElement", "createElementNS", + "parentWindow", "createTextNode" ].includes(b) && !b.startsWith('__reactInternalInstance') && From 77751ac5f8a0f142e308072f58e518f230ce0c6d Mon Sep 17 00:00:00 2001 From: weizman Date: Wed, 11 Dec 2024 00:23:49 +0200 Subject: [PATCH 4/9] poc --- app/scripts/use-snow.js | 1 + 1 file changed, 1 insertion(+) diff --git a/app/scripts/use-snow.js b/app/scripts/use-snow.js index 5b9a3ff6b96a..0f6b10dc34cb 100644 --- a/app/scripts/use-snow.js +++ b/app/scripts/use-snow.js @@ -57,6 +57,7 @@ Changing this code must be done cautiously to avoid breaking the app! "createElement", "createElementNS", "parentWindow", + "compatMode", "createTextNode" ].includes(b) && !b.startsWith('__reactInternalInstance') && From ab2f53146cc4d248afb283b2e1227a6cd34e2507 Mon Sep 17 00:00:00 2001 From: weizman Date: Wed, 11 Dec 2024 10:08:55 +0200 Subject: [PATCH 5/9] poc --- app/scripts/use-snow.js | 1 + 1 file changed, 1 insertion(+) diff --git a/app/scripts/use-snow.js b/app/scripts/use-snow.js index 0f6b10dc34cb..1081ad60e394 100644 --- a/app/scripts/use-snow.js +++ b/app/scripts/use-snow.js @@ -58,6 +58,7 @@ Changing this code must be done cautiously to avoid breaking the app! "createElementNS", "parentWindow", "compatMode", + "elementFromPoint", "createTextNode" ].includes(b) && !b.startsWith('__reactInternalInstance') && From defc6eda0571e8c2500a3ef47ede71adddd8b9a1 Mon Sep 17 00:00:00 2001 From: weizman Date: Wed, 11 Dec 2024 10:27:40 +0200 Subject: [PATCH 6/9] poc --- app/scripts/use-snow.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/scripts/use-snow.js b/app/scripts/use-snow.js index 1081ad60e394..73bce013585c 100644 --- a/app/scripts/use-snow.js +++ b/app/scripts/use-snow.js @@ -59,6 +59,8 @@ Changing this code must be done cautiously to avoid breaking the app! "parentWindow", "compatMode", "elementFromPoint", + "elementsFromPoint", + "createEvent", "createTextNode" ].includes(b) && !b.startsWith('__reactInternalInstance') && From ef4ee926f10a3e933a5e287c67b2d4abe3e67d50 Mon Sep 17 00:00:00 2001 From: weizman Date: Wed, 11 Dec 2024 10:51:02 +0200 Subject: [PATCH 7/9] poc --- app/scripts/use-snow.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/scripts/use-snow.js b/app/scripts/use-snow.js index 73bce013585c..84251f2c2694 100644 --- a/app/scripts/use-snow.js +++ b/app/scripts/use-snow.js @@ -104,6 +104,14 @@ Changing this code must be done cautiously to avoid breaking the app! return ret; }} ); + const initMouseEvent = Object.getOwnPropertyDescriptor(MouseEvent.prototype, 'initMouseEvent').value; + Object.defineProperty(MouseEvent.prototype, 'initMouseEvent', {value: function (a,b,c,w,e,f,g,h,i,j,k,l,m,n,o) { + if (winp === w) { + w = win; + } + return initMouseEvent.call(this, a,b,c,w,e,f,g,h,i,j,k,l,m,n,o); + }} + ); } const log = console.log.bind(console); // eslint-disable-next-line no-undef From fc178c0d0bc96eda7a84f8be3234b418a7752ac8 Mon Sep 17 00:00:00 2001 From: weizman Date: Wed, 11 Dec 2024 11:11:57 +0200 Subject: [PATCH 8/9] poc --- app/scripts/use-snow.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/scripts/use-snow.js b/app/scripts/use-snow.js index 84251f2c2694..ae51c83218f3 100644 --- a/app/scripts/use-snow.js +++ b/app/scripts/use-snow.js @@ -77,7 +77,7 @@ Changing this code must be done cautiously to avoid breaking the app! }); } try {win.Node} catch {return;} - const {document, Object, Node} = win; + const {document, Object, Node, MouseEvent} = win; const winp = generateTamedWindow(win); const proxy = generateTamedDocument(document, winp); Object.defineProperty(Node.prototype, 'ownerDocument', {get: function() { From cd4cfc23b66912b516c97cf4902d887c5fa6a36f Mon Sep 17 00:00:00 2001 From: weizman Date: Wed, 11 Dec 2024 18:48:59 +0200 Subject: [PATCH 9/9] poc --- app/scripts/use-snow.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/scripts/use-snow.js b/app/scripts/use-snow.js index ae51c83218f3..fa31872baac9 100644 --- a/app/scripts/use-snow.js +++ b/app/scripts/use-snow.js @@ -77,9 +77,13 @@ Changing this code must be done cautiously to avoid breaking the app! }); } try {win.Node} catch {return;} - const {document, Object, Node, MouseEvent} = win; + const {document, Object, Document, Node, MouseEvent} = win; const winp = generateTamedWindow(win); const proxy = generateTamedDocument(document, winp); + Object.defineProperty(Document.prototype, 'defaultView', {get: function() { + return winp; + }} + ); Object.defineProperty(Node.prototype, 'ownerDocument', {get: function() { return proxy; }}