JSFiddle - React, Tailwind, and code Playground
HTML
<head>
<script src="https://static.opentok.com/v2/js/opentok.min.js"></script>
</head>
<button id="user-gestured">
Some sort of user gesture
</button>
JavaScript
function initpublisher() {
OT.initPublisher('publisher', {
videoSource: "screen"
}, (err) => {
if (err) {
console.warn(err);
} else {
alert('check console - no errors');
}
});
}
// That will failed, initpublisher() is called outside of any user interaction or gesture event listener, which will result in an InvalidAccessError because the browser requires a user gesture to access the screen capture feature.
initpublisher() ;
async function userHandler() {
// it will work without throwing an error because it is being called in response to a user gesture (click, double-click, tap, key-press ..)
initpublisher();
}
document.querySelector('#user-gestured').onclick = userHandler;