Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make worker more resilient to DOM API usage attempts #7425

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 7 additions & 1 deletion src/bricks/transformers/selectElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { userSelectElement } from "@/contentScript/pageEditor/elementPicker";

import { TransformerABC } from "@/types/bricks/transformerTypes";
import { type Schema } from "@/types/schemaTypes";
import { propertiesToSchema } from "@/validators/generic";
Expand Down Expand Up @@ -53,6 +53,12 @@ export class SelectElement extends TransformerABC {
);

async transform(): Promise<unknown> {
// The picker uses `bootstrap-switch-button`, which does a `window` check on load and breaks
// the MV3 background worker. Lazy-loading it keeps the background worker from breaking.
const { userSelectElement } = await import(
/* webpackChunkName: "editorContentScript" */ "@/contentScript/pageEditor/elementPicker"
Copy link
Contributor Author

@fregante fregante Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverting the change in #7381 and adding a more appropriate comment.

);

const { elements } = await userSelectElement();

const elementRefs = elements.map((element) =>
Expand Down
19 changes: 19 additions & 0 deletions static/background.worker.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
// Don't include `background.worker.js` in webpack, there's no advantage in doing so

function get() {
/*
When scripts use DOM APIs, the worker will crash on load with the error
`ReferenceError: window is not defined` without pointing to the correct line.
This listener defines some globals so that they return `undefined` instead
of throwing an error on access.

This line also lets you add a breakpoint so that you can stop the debugger and
see where the access originated from. Or temporarily add console.trace() here.
*/
}

Object.defineProperties(self, {
document: { get },
window: { get },
navigator: { get },
});

self.importScripts("./grayIconWhileLoading.js", "./background.js");
Loading