That doesn’t respond to an event. I need an event listener in the page script that does what I want, in particular, to stop the propagation of the event.
How can a page script call an event handler in a content script?
My suggestion definitely does that.
Anyway. Calling .stopImmediatePropagation() from a capturing handler on the window on an event in a content script should also stop the event from reaching page handlers.
The page script isn’t mine. It is whatever the user has loaded, from some other domain. The page script cannot call as an event handler a function in the content script, even if exported, due to a permission error. I don’t know if this is deliberate or a bug. For example I tried
let handler = exportFunction(onScroll, win, {defineAs: 'onScroll'});
win.addEventListener('scroll', handler, true);
but still there is a permission error, even though the handler is an exported function within the page’s window. Only by compiling code in the context of the page can I get something that works. I was just wondering if there was a better way.
You could cancel the scroll event you get and re-fire it from your content script. Make sure to export the event object you’re firing to the page context using cloneInto. You shouldn’t need your own listener infrastructure.
tell us what exactly you do now and what exact error you get
tell us what it is you want to achieve.
I have an extension that responds to 'wheel' events and calls .preventDefault() on them just fine. I can hardly imagine that you are unable to receive 'scroll' events.
Ah, you’re attaching on win, whatever that is and not the window global. When you add a listener to the window or document etc. you shouldn’t need to export the handler.
That is the non-X-Rayed version of the object (waived shims version) and thus you do indeed have to export the listener, however it is not recommended to attach DOM listeners to that version of window, as the page may have tampered with it.