A platform for community discussion. Free, open, simple.
<!DOCTYPE html>
<html lang="<%= I18n.locale %>">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta name="robots" content="noindex">
<title><%= t("discourse_events.livestream.zoom.frame_title") %></title>
<style>
html,
body {
height: 100%;
margin: 0;
overflow: hidden;
background: #000;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
"Helvetica Neue", Arial, sans-serif;
}
/* The whole frame takes the tap, so there is no aiming to be done, while
what is drawn inside it is button-sized. */
#join {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
padding: 0;
border: 0;
background: transparent;
font: inherit;
cursor: pointer;
-webkit-tap-highlight-color: transparent;
}
#join span {
display: inline-flex;
align-items: center;
gap: 0.5em;
padding: 0.7em 1.4em;
border-radius: 6px;
background: <%= @accent_color %>;
color: #fff;
font-size: 1.05em;
font-weight: 600;
line-height: 1;
box-shadow: 0 2px 6px rgb(0 0 0 / 45%);
}
#join:active span {
transform: translateY(1px);
}
#join[disabled] {
cursor: default;
}
#join[disabled] span {
opacity: 0.6;
box-shadow: none;
}
#join svg {
width: 1.1em;
height: 1.1em;
fill: currentcolor;
}
</style>
</head>
<body>
<%# Joining has to start from a tap taken inside this document: the SDK asks
for the microphone, and a mobile browser only prompts for it while the
page holds a user activation, which does not survive the navigation
here from the topic.
Asking earlier, from the tap that navigates here, does not help. Safari
scopes a microphone grant to the document that was given it, so the
meeting is asked again regardless and the user is prompted twice. %>
<button id="join" type="button">
<span>
<svg viewBox="0 0 576 512" aria-hidden="true"><path d="M336 64H48C21.5 64 0 85.5 0 112v288c0 26.5 21.5 48 48 48h288c26.5 0 48-21.5 48-48V112c0-26.5-21.5-48-48-48zm212.2 25.7L416 181.8v148.4l132.2 92.1c15.9 11 37.8-.2 37.8-19.7V109.4c0-19.5-21.9-30.7-37.8-19.7z"/></svg>
<%= t("discourse_events.livestream.zoom.frame_join") %>
</span>
</button>
<script nonce="<%= csp_nonce_placeholder %>">
window.zoomFrameConfig = <%= raw @config.to_json %>;
window.zoomFrameConfig.joiningLabel = "<%= j t("discourse_events.livestream.zoom.frame_joining") %>";
</script>
<script nonce="<%= csp_nonce_placeholder %>">
(function () {
const config = window.zoomFrameConfig;
// Vendored dependencies of the SDK: React must precede ReactDOM, and
// Redux precede its thunk.
const DEPENDENCIES = [
"react",
"react-dom",
"redux",
"redux-thunk",
"lodash",
];
// `meetingStatus` in the SDK's own numbering: 1 connecting, 2 connected,
// 3 disconnected, 4 reconnecting.
const MEETING_DISCONNECTED = 3;
// `state` rather than `type`: the page this is embedded in is not the
// only thing listening to the window, and `type` is a common enough key
// in other postMessage protocols to be taken for one of theirs.
function post(state) {
window.parent?.postMessage(
{ source: "discourse-zoom-frame", state },
window.location.origin
);
}
function loadScript(src) {
return new Promise(function (resolve, reject) {
const element = document.createElement("script");
element.src = src;
element.onload = resolve;
element.onerror = function () {
reject(new Error("Failed to load " + src));
};
document.head.appendChild(element);
});
}
// Whether the microphone is ours to use, which is also how it is asked
// for. Called from a tap it raises the prompt; called without one it
// settles what is already granted, since a browser will not prompt
// outside a user's activation — it just refuses.
//
// The grant is this document's, which is the one the SDK asks from, so
// it inherits it and the stream itself is no longer needed here.
async function microphoneReady() {
try {
const stream = await navigator.mediaDevices.getUserMedia({
audio: true,
});
stream.getTracks().forEach(function (track) {
track.stop();
});
return true;
} catch (error) {
// eslint-disable-next-line no-console
console.warn("Microphone unavailable for Zoom meeting", error);
return false;
}
}
async function join() {
for (const dependency of DEPENDENCIES) {
await loadScript(
config.cdn + "/" + config.sdkVersion + "/lib/vendor/" + dependency + ".min.js"
);
}
await loadScript(
config.cdn + "/zoom-meeting-" + config.sdkVersion + ".min.js"
);
const response = await fetch(config.signatureUrl, {
credentials: "same-origin",
headers: {
"Accept": "application/json",
"X-Requested-With": "XMLHttpRequest",
},
});
if (!response.ok) {
throw new Error("Signature request failed: " + response.status);
}
const payload = await response.json();
const ZoomMtg = window.ZoomMtg;
ZoomMtg.preLoadWasm();
ZoomMtg.prepareWebSDK();
ZoomMtg.i18n.load("en-US");
ZoomMtg.i18n.reload("en-US");
await new Promise(function (resolve, reject) {
ZoomMtg.init({
leaveUrl: config.leaveUrl,
patchJsMedia: true,
disableCallOut: true,
isSupportChat: false,
success: resolve,
error: reject,
});
});
// Leaving does not always take the window to `leaveUrl`: ending a
// meeting, being removed from one, or losing it altogether leaves
// Zoom sitting on a screen of its own with nothing on it to act on.
// The status is reported either way, so it is what the page around
// the frame is told by.
ZoomMtg.inMeetingServiceListener("onMeetingStatus", function (status) {
if (status && status.meetingStatus === MEETING_DISCONNECTED) {
post("left");
}
});
await new Promise(function (resolve, reject) {
ZoomMtg.join({
signature: payload.signature,
sdkKey: payload.sdk_key,
meetingNumber: payload.meeting_number,
passWord: payload.password || "",
userName: payload.user_name,
userEmail: payload.user_email,
success: resolve,
error: reject,
});
});
}
const joinButton = document.getElementById("join");
// Zoom left the meeting by navigating this window to `leaveUrl`, so
// there is nothing to join: the page around the frame decides where the
// user goes next.
if (config.hasLeft) {
joinButton.remove();
post("left");
return;
}
let starting = false;
async function start() {
if (starting) {
return;
}
starting = true;
joinButton.disabled = true;
joinButton.querySelector("span").textContent = config.joiningLabel;
try {
await join();
joinButton.remove();
} catch (error) {
// eslint-disable-next-line no-console
console.error("Error joining Zoom meeting", error);
joinButton.remove();
post("error");
}
}
joinButton.addEventListener("click", async function () {
await microphoneReady();
start();
});
// Whether the microphone has already been granted, asked in the one way
// that cannot itself raise a prompt. Requesting the microphone to find
// out would ask a second time for something the page before this one
// has often just been given.
async function microphoneAlreadyGranted() {
try {
const status = await navigator.permissions.query({
name: "microphone",
});
return status.state === "granted";
} catch {
// Not every browser will answer for the microphone. Saying no
// leaves the button to ask, which is always safe.
return false;
}
}
// A browser that carries a microphone grant between documents has
// nothing left to ask, so the meeting is joined outright and the button
// is never shown. Safari is not one of them: it grants per document,
// and there the button is what asks, from a tap.
microphoneAlreadyGranted().then(function (granted) {
if (granted) {
start();
}
});
})();
</script>
</body>
</html>