Message from JavaScript discussions

May 2019

— I'm trying to auto-trigger one event (call a function), but facing problems..


const n = document.createElement("button");
n.addEventListener("click", B)
B(); // I added this line in order to auto-trigger the function instead of clic, but error raises

...

function B(e) {
e.preventDefault(), e.stopImmediatePropagation(), e.isTrusted && !g && (m({
working: !0
}), async function(e) {
...
}

...

 new MutationObserver(h).observe(document.body, {
childList: !0,
subtree: !0
}), h()


Uncaught TypeError: Cannot read property 'preventDefault' of undefined
at B (script.js:3606)
at MutationObserver.h (script.js:3241)

Message permanent page

— 

Your function B requires a event parameter to work. But if you don’t use the event object in the important part of your function you can simply use an if(e) to conditionally execute those lines that require “e” to be defined.

— Thanks Christian! Will try removing event and tweaking a bit that func 🤞🏻

— 🎉

— Uhm looks like B() depends a lot of e

— Oh

— You can try to call it in other event trigger like document load event

— 👍 thanks Chris, will try now

— Ok, write here if you need any help

— 🙏🏻😊

— No success

— I will share the code, is part of a Chrome extension (script.js):
pastebin.com/hWa0JZzL

target function is function h(), which is called by new MutationObserver(h)

h()
just modifies the DOM content, adds a button element and addEventListener(click, B) to this button, so when this button is called, function B(e) is executed

my additions are just:
function nestake() and new MutationObserver(nestake)

Message permanent page