Skip to content

Commit

Permalink
Merge pull request #36 from Marshal27/fix/window_onload_overwrite
Browse files Browse the repository at this point in the history
fix: window onload overwrite
  • Loading branch information
Marshal27 authored Jan 5, 2023
2 parents d96c832 + 99cb96e commit 847ea36
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,21 +756,33 @@ class StyleElementController extends NodeController<HTMLStyleElement> {
}
}

const shadowOnLoadCallbacks: (() => void)[] = [];
let isOnLoadSet = false;
class ShadowRootController extends NodeController<ShadowRoot> {
private controller: AbortController | null = null;
private mo: MutationObserver;

constructor(node: ShadowRoot, mo: MutationObserver) {
super(node);
this.mo = mo;
if (!isOnLoadSet) {
window.onload = () => {
for (const func of shadowOnLoadCallbacks) {
func();
}
}
isOnLoadSet = true;
}
}

connected(): void {
window.onload = () => {
const styleElement = document.createElement("style");
styleElement.textContent = `* { ${CUSTOM_PROPERTY_TYPE}: inherit; ${CUSTOM_PROPERTY_NAME}: inherit; }`;
this.node.appendChild(styleElement);
}
shadowOnLoadCallbacks.push(
() => {
const styleElement = document.createElement("style");
styleElement.textContent = `* { ${CUSTOM_PROPERTY_TYPE}: inherit; ${CUSTOM_PROPERTY_NAME}: inherit; }`;
this.node.appendChild(styleElement);
}
)
this.mo?.observe(this.node, {
childList: true,
subtree: true,
Expand Down

0 comments on commit 847ea36

Please sign in to comment.