1.0.6 • Published 6 months ago

is-trusted v1.0.6

Weekly downloads
-
License
ISC
Repository
-
Last release
6 months ago

is-trusted

Intercepts addEventListener, attachEvent and on events by checking origin of event

Usage

Append index.js script tag before all scripts in head

API

window.addEventListener("addlistenerwrap", (event: CustomEvent<Function>)); // fires on addEventListener/attachEvent
window.addEventListener("oneventwrap", (event: CustomEvent<Function>)); // fires when wrapping the original function for additional middleware
window.addEventListener("addlistenerviolation", (event: CustomEvent<Args>)); // fires on addEventListener/attachEvent non-trusted event origin
window.addEventListener("oneventviolation", (event: CustomEvent<Args>)); // fires on "on" event non-trusted origin

Test

(() => {
  const b = document.createElement("b");

  b.addEventListener("click", () => console.log("trusted"));
  b.textContent = "click";

  document.body.prepend(b);
})();

document.querySelector("b").click(); // event does not fire
document.querySelector("b").dispatchEvent(new MouseEvent("click")); // event does not fire

// Event fires on manual click
1.0.6

6 months ago

1.0.5

7 months ago

1.0.4

7 months ago

1.0.3

7 months ago

1.0.2

7 months ago

1.0.0

7 months ago