Skip to content

Commit

Permalink
🐛 处理on事件的移除
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Apr 29, 2024
1 parent 03987b0 commit c69c208
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/runtime/content/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe("proxy context", () => {
console.log("eval");
},
addEventListener: () => {},
removeEventListener: () => {},
location: "ok",
};
init.set("onload", true);
Expand All @@ -25,6 +26,8 @@ describe("proxy context", () => {
_this["onload"] = "ok";
expect(_this["onload"]).toEqual("ok");
expect(global["onload"]).toEqual(null);
_this["onload"] = undefined;
expect(_this["onload"]).toEqual(undefined);
});

it("update", () => {
Expand Down
9 changes: 8 additions & 1 deletion src/runtime/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,14 @@ export function proxyContext(
}
// 只处理onxxxx的事件
if (has(global, name) && name.startsWith("on")) {
global.addEventListener(name.slice(2), val);
if (val === undefined) {
global.removeEventListener(name.slice(2), thisContext[name]);
} else {
if (thisContext[name]) {
global.removeEventListener(name.slice(2), thisContext[name]);
}
global.addEventListener(name.slice(2), val);
}
thisContext[name] = val;
return true;
}
Expand Down

0 comments on commit c69c208

Please sign in to comment.