Please carefully consider whether this is the behaviour you require, as a consequence of doing this is that sessions will need to be restarted if the user refreshers their browser by mistake or for another reason.
The following code prevents the Cobrowse session continuing after a page refresh:
window.addEventListener("beforeunload", () => {This does not end the session but it prevents the agent from seeing the user’s screen. You can then end the session on the agent side using any of the available methods.
CobrowseIO.stop()
}, {capture: true});