From fb423f5b578436e3ae4ddec32ce398fbed364221 Mon Sep 17 00:00:00 2001 From: Deepankar Bajpeyi Date: Mon, 7 Oct 2024 12:11:05 +0200 Subject: [PATCH] feat: handle unknown urls --- build/integration/contentScope.js | 25 ++++++++++++++----------- src/features/password-import.js | 27 ++++++++++++++++----------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/build/integration/contentScope.js b/build/integration/contentScope.js index 17a81fdd1..81eb6d7e2 100644 --- a/build/integration/contentScope.js +++ b/build/integration/contentScope.js @@ -22114,18 +22114,25 @@ return await withExponentialBackoff(fn) } - init () { + async animateFromPath (path) { const animateElement = this.animateElement.bind(this); const findElement = this.findElement.bind(this); + const { name, style } = URL_ELEMENT_MAP[path] ?? {}; + if (name && style) { + const element = await findElement(name); + animateElement(element, style); + } + } + + init () { // FIXME: this is stolen from element-hiding.js, we would need a global util that would do this, // single page applications don't have a DOMContentLoaded event on navigations, so // we use proxy/reflect on history.pushState to find elements on page navigations + const animateFromPath = this.animateFromPath.bind(this); const historyMethodProxy = new DDGProxy(this, History.prototype, 'pushState', { async apply (target, thisArg, args) { - const pageURL = args[2].split('?')[0]; - const { name, style } = URL_ELEMENT_MAP[pageURL]; - const element = await findElement(name); - animateElement(element, style); + const path = args[2].split('?')[0]; + await animateFromPath(path); return DDGReflect.apply(target, thisArg, args) } }); @@ -22133,16 +22140,12 @@ // listen for popstate events in order to run on back/forward navigations window.addEventListener('popstate', async () => { console.log('pushState URL', window.location.pathname); - const { name, style } = URL_ELEMENT_MAP[window.location.pathname]; - const element = await findElement(name); - animateElement(element, style); + await animateFromPath(window.location.pathname); }); document.addEventListener('DOMContentLoaded', async () => { console.log('pushState URL', window.location.pathname); - const { name, style } = URL_ELEMENT_MAP[window.location.pathname]; - const element = await findElement(name); - animateElement(element, style); + await animateFromPath(window.location.pathname); }); } } diff --git a/src/features/password-import.js b/src/features/password-import.js index 4eef152a4..c147f5247 100644 --- a/src/features/password-import.js +++ b/src/features/password-import.js @@ -80,18 +80,25 @@ export default class PasswordImport extends ContentFeature { return await withExponentialBackoff(fn) } - init () { + async animateFromPath (path) { const animateElement = this.animateElement.bind(this) const findElement = this.findElement.bind(this) + const { name, style } = URL_ELEMENT_MAP[path] ?? {} + if (name && style) { + const element = await findElement(name) + animateElement(element, style) + } + } + + init () { // FIXME: this is stolen from element-hiding.js, we would need a global util that would do this, // single page applications don't have a DOMContentLoaded event on navigations, so // we use proxy/reflect on history.pushState to find elements on page navigations + const animateFromPath = this.animateFromPath.bind(this) const historyMethodProxy = new DDGProxy(this, History.prototype, 'pushState', { async apply (target, thisArg, args) { - const pageURL = args[2].split('?')[0] - const { name, style } = URL_ELEMENT_MAP[pageURL] - const element = await findElement(name) - animateElement(element, style) + const path = args[2].split('?')[0] + await animateFromPath(path) return DDGReflect.apply(target, thisArg, args) } }) @@ -99,16 +106,14 @@ export default class PasswordImport extends ContentFeature { // listen for popstate events in order to run on back/forward navigations window.addEventListener('popstate', async () => { console.log('pushState URL', window.location.pathname) - const { name, style } = URL_ELEMENT_MAP[window.location.pathname] - const element = await findElement(name) - animateElement(element, style) + await animateFromPath(window.location.pathname) + console.log("After popstate") }) document.addEventListener('DOMContentLoaded', async () => { console.log('pushState URL', window.location.pathname) - const { name, style } = URL_ELEMENT_MAP[window.location.pathname] - const element = await findElement(name) - animateElement(element, style) + await animateFromPath(window.location.pathname) + console.log("After popstate") }) } }