Release Hudra 0.10 beta 1
This commit is contained in:
+215
-45
@@ -2,6 +2,80 @@ const sessions = new Map();
|
||||
const tabToSession = new Map();
|
||||
const windowToSession = new Map();
|
||||
const previousWindowStates = new Map();
|
||||
const sessionStorageKey = "hudraActiveSessions";
|
||||
let persistStatePromise = Promise.resolve();
|
||||
|
||||
function rebuildSessionIndexes() {
|
||||
tabToSession.clear();
|
||||
windowToSession.clear();
|
||||
|
||||
sessions.forEach((session) => {
|
||||
if (session.audienceTabId != null) {
|
||||
tabToSession.set(session.audienceTabId, session.id);
|
||||
}
|
||||
if (session.audienceWindowId != null) {
|
||||
windowToSession.set(session.audienceWindowId, session.id);
|
||||
}
|
||||
if (session.controlWindowId != null) {
|
||||
windowToSession.set(session.controlWindowId, session.id);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function restoreSessionState() {
|
||||
const stored = await chrome.storage.session.get(sessionStorageKey);
|
||||
const state = stored[sessionStorageKey];
|
||||
if (!state) return;
|
||||
|
||||
for (const storedSession of state.sessions || []) {
|
||||
if (!storedSession?.id) continue;
|
||||
sessions.set(storedSession.id, {
|
||||
...storedSession,
|
||||
pageTitle: "Hudra",
|
||||
current: 0,
|
||||
count: 0,
|
||||
audienceHidden: false,
|
||||
audienceZoom: 100,
|
||||
audienceScrollY: 0,
|
||||
currentPreview: "",
|
||||
nextPreview: ""
|
||||
});
|
||||
}
|
||||
|
||||
for (const [windowId, windowState] of state.previousWindowStates || []) {
|
||||
previousWindowStates.set(Number(windowId), windowState);
|
||||
}
|
||||
|
||||
rebuildSessionIndexes();
|
||||
}
|
||||
|
||||
const sessionStateReady = restoreSessionState().catch((error) => {
|
||||
console.warn("Hudra could not restore its active sessions:", error);
|
||||
});
|
||||
|
||||
async function ensureSessionStateReady() {
|
||||
await sessionStateReady;
|
||||
}
|
||||
|
||||
function persistSessionState() {
|
||||
const state = {
|
||||
sessions: [...sessions.values()].map((session) => ({
|
||||
id: session.id,
|
||||
mode: session.mode,
|
||||
sourceTabId: session.sourceTabId,
|
||||
audienceTabId: session.audienceTabId,
|
||||
audienceWindowId: session.audienceWindowId,
|
||||
controlWindowId: session.controlWindowId
|
||||
})),
|
||||
previousWindowStates: [...previousWindowStates.entries()]
|
||||
};
|
||||
|
||||
persistStatePromise = persistStatePromise
|
||||
.catch(() => {})
|
||||
.then(() => chrome.storage.session.set({ [sessionStorageKey]: state }));
|
||||
|
||||
return persistStatePromise;
|
||||
}
|
||||
|
||||
function makeSessionId() {
|
||||
return `${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
||||
@@ -32,6 +106,57 @@ function chooseSecondaryDisplay(displays) {
|
||||
return { primary, secondary };
|
||||
}
|
||||
|
||||
function isSupportedLessonUrl(value) {
|
||||
try {
|
||||
const url = new URL(value);
|
||||
const hostname = url.hostname.toLowerCase();
|
||||
return url.protocol === "https:" && (
|
||||
hostname === "qlearn.eq.edu.au" ||
|
||||
hostname.endsWith(".qlearn.eq.edu.au") ||
|
||||
hostname.endsWith(".instructure.com")
|
||||
);
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function showPageNotice(tabId, message) {
|
||||
await chrome.scripting.executeScript({
|
||||
target: { tabId },
|
||||
files: ["page-detection.js"]
|
||||
});
|
||||
|
||||
await chrome.scripting.executeScript({
|
||||
target: { tabId },
|
||||
func: (noticeMessage) => window.__hudraPageDetector.showNotice(noticeMessage),
|
||||
args: [message]
|
||||
});
|
||||
}
|
||||
|
||||
async function inspectLessonPage(tabId) {
|
||||
await chrome.scripting.executeScript({
|
||||
target: { tabId },
|
||||
files: ["page-detection.js"]
|
||||
});
|
||||
|
||||
const results = await chrome.scripting.executeScript({
|
||||
target: { tabId },
|
||||
func: () => window.__hudraPageDetector.inspect()
|
||||
});
|
||||
|
||||
return results[0]?.result || { supported: false, reason: "INSPECTION_FAILED" };
|
||||
}
|
||||
|
||||
function messageForUnsupportedPage(reason) {
|
||||
if (reason === "NO_LESSON_TITLE") {
|
||||
return "Hudra couldn’t start because this lesson has no H1 title.";
|
||||
}
|
||||
if (reason === "EMPTY_LESSON") {
|
||||
return "Hudra couldn’t start because this lesson has no presentable content.";
|
||||
}
|
||||
return "Hudra couldn’t start because this page does not contain a supported lesson.";
|
||||
}
|
||||
|
||||
async function waitForTabComplete(tabId, timeoutMs = 20000) {
|
||||
const existing = await chrome.tabs.get(tabId).catch(() => null);
|
||||
if (existing?.status === "complete") return;
|
||||
@@ -57,7 +182,7 @@ async function waitForTabComplete(tabId, timeoutMs = 20000) {
|
||||
async function injectPresenter(tabId, config) {
|
||||
await chrome.scripting.executeScript({
|
||||
target: { tabId },
|
||||
files: ["presenter.js"]
|
||||
files: ["page-detection.js", "presenter.js"]
|
||||
});
|
||||
|
||||
await chrome.tabs.sendMessage(tabId, {
|
||||
@@ -67,6 +192,7 @@ async function injectPresenter(tabId, config) {
|
||||
}
|
||||
|
||||
async function startSingleScreen(tab) {
|
||||
await ensureSessionStateReady();
|
||||
if (!tab.id || tab.windowId == null) return;
|
||||
|
||||
const sessionId = makeSessionId();
|
||||
@@ -93,12 +219,14 @@ async function startSingleScreen(tab) {
|
||||
sessions.set(sessionId, session);
|
||||
tabToSession.set(tab.id, sessionId);
|
||||
windowToSession.set(tab.windowId, sessionId);
|
||||
await persistSessionState();
|
||||
|
||||
await injectPresenter(tab.id, { mode: "single", sessionId });
|
||||
await chrome.windows.update(tab.windowId, { state: "fullscreen" });
|
||||
}
|
||||
|
||||
async function startDualScreen(tab, primary, secondary) {
|
||||
await ensureSessionStateReady();
|
||||
if (!tab.id || !tab.url) return;
|
||||
|
||||
const sessionId = makeSessionId();
|
||||
@@ -153,6 +281,7 @@ async function startDualScreen(tab, primary, secondary) {
|
||||
tabToSession.set(audienceTab.id, sessionId);
|
||||
windowToSession.set(audienceWindow.id, sessionId);
|
||||
if (controlWindow.id != null) windowToSession.set(controlWindow.id, sessionId);
|
||||
await persistSessionState();
|
||||
|
||||
await waitForTabComplete(audienceTab.id);
|
||||
await injectPresenter(audienceTab.id, { mode: "audience", sessionId });
|
||||
@@ -180,6 +309,7 @@ async function startDualScreen(tab, primary, secondary) {
|
||||
}
|
||||
|
||||
async function endSession(sessionId, reason = "ended") {
|
||||
await ensureSessionStateReady();
|
||||
const session = sessions.get(sessionId);
|
||||
if (!session) return;
|
||||
|
||||
@@ -197,10 +327,12 @@ async function endSession(sessionId, reason = "ended") {
|
||||
|
||||
const previousState = previousWindowStates.get(session.audienceWindowId) || "maximized";
|
||||
previousWindowStates.delete(session.audienceWindowId);
|
||||
await persistSessionState();
|
||||
await chrome.windows.update(session.audienceWindowId, { state: previousState }).catch(async () => {
|
||||
await chrome.windows.update(session.audienceWindowId, { state: "maximized" }).catch(() => {});
|
||||
});
|
||||
} else {
|
||||
await persistSessionState();
|
||||
if (session.audienceWindowId != null) {
|
||||
await chrome.windows.remove(session.audienceWindowId).catch(() => {});
|
||||
}
|
||||
@@ -211,6 +343,7 @@ async function endSession(sessionId, reason = "ended") {
|
||||
}
|
||||
|
||||
async function sendCommand(sessionId, command, value) {
|
||||
await ensureSessionStateReady();
|
||||
const session = sessions.get(sessionId);
|
||||
if (!session?.audienceTabId) return;
|
||||
|
||||
@@ -227,6 +360,7 @@ async function sendCommand(sessionId, command, value) {
|
||||
}
|
||||
|
||||
async function toggleHudraForTab(tab) {
|
||||
await ensureSessionStateReady();
|
||||
if (!tab?.id) return;
|
||||
|
||||
const windowSessionId = tab.windowId != null ? windowToSession.get(tab.windowId) : null;
|
||||
@@ -241,6 +375,30 @@ async function toggleHudraForTab(tab) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isSupportedLessonUrl(tab.url)) {
|
||||
await showPageNotice(
|
||||
tab.id,
|
||||
"Hudra works on QLearn and Canvas lesson pages hosted by Instructure."
|
||||
).catch(() => {});
|
||||
return;
|
||||
}
|
||||
|
||||
let inspection;
|
||||
try {
|
||||
inspection = await inspectLessonPage(tab.id);
|
||||
} catch (error) {
|
||||
await showPageNotice(
|
||||
tab.id,
|
||||
"Hudra couldn’t inspect this page. Wait for it to finish loading and try again."
|
||||
).catch(() => {});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!inspection.supported) {
|
||||
await showPageNotice(tab.id, messageForUnsupportedPage(inspection.reason)).catch(() => {});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const displays = await getUsableDisplays();
|
||||
const { primary, secondary } = chooseSecondaryDisplay(displays);
|
||||
@@ -270,31 +428,33 @@ chrome.commands.onCommand.addListener(async (command) => {
|
||||
|
||||
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
||||
if (message?.type === "QLEARN_PRESENTER_STATE") {
|
||||
const session = sessions.get(message.sessionId);
|
||||
if (!session) return;
|
||||
ensureSessionStateReady().then(() => {
|
||||
const session = sessions.get(message.sessionId);
|
||||
if (!session) return;
|
||||
|
||||
session.current = Number(message.current) || 0;
|
||||
session.count = Number(message.count) || 0;
|
||||
session.pageTitle = message.pageTitle || session.pageTitle;
|
||||
session.audienceHidden = Boolean(message.audienceHidden);
|
||||
session.audienceZoom = Number(message.audienceZoom) || 100;
|
||||
session.audienceScrollY = Number(message.audienceScrollY) || 0;
|
||||
session.currentPreview = message.currentPreview || "";
|
||||
session.nextPreview = message.nextPreview || "";
|
||||
session.current = Number(message.current) || 0;
|
||||
session.count = Number(message.count) || 0;
|
||||
session.pageTitle = message.pageTitle || session.pageTitle;
|
||||
session.audienceHidden = Boolean(message.audienceHidden);
|
||||
session.audienceZoom = Number(message.audienceZoom) || 100;
|
||||
session.audienceScrollY = Number(message.audienceScrollY) || 0;
|
||||
session.currentPreview = message.currentPreview || "";
|
||||
session.nextPreview = message.nextPreview || "";
|
||||
|
||||
chrome.runtime.sendMessage({
|
||||
type: "QLEARN_PRESENTER_STATE_UPDATE",
|
||||
sessionId: session.id,
|
||||
current: session.current,
|
||||
count: session.count,
|
||||
pageTitle: session.pageTitle,
|
||||
mode: session.mode,
|
||||
audienceHidden: session.audienceHidden,
|
||||
audienceZoom: session.audienceZoom,
|
||||
audienceScrollY: session.audienceScrollY,
|
||||
currentPreview: session.currentPreview,
|
||||
nextPreview: session.nextPreview
|
||||
}).catch(() => {});
|
||||
chrome.runtime.sendMessage({
|
||||
type: "QLEARN_PRESENTER_STATE_UPDATE",
|
||||
sessionId: session.id,
|
||||
current: session.current,
|
||||
count: session.count,
|
||||
pageTitle: session.pageTitle,
|
||||
mode: session.mode,
|
||||
audienceHidden: session.audienceHidden,
|
||||
audienceZoom: session.audienceZoom,
|
||||
audienceScrollY: session.audienceScrollY,
|
||||
currentPreview: session.currentPreview,
|
||||
nextPreview: session.nextPreview
|
||||
}).catch(() => {});
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -309,31 +469,41 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
||||
}
|
||||
|
||||
if (message?.type === "QLEARN_PRESENTER_GET_SESSION") {
|
||||
const session = sessions.get(message.sessionId);
|
||||
sendResponse(session ? {
|
||||
sessionId: session.id,
|
||||
mode: session.mode,
|
||||
current: session.current,
|
||||
count: session.count,
|
||||
pageTitle: session.pageTitle,
|
||||
audienceHidden: session.audienceHidden,
|
||||
audienceZoom: session.audienceZoom,
|
||||
audienceScrollY: session.audienceScrollY,
|
||||
currentPreview: session.currentPreview,
|
||||
nextPreview: session.nextPreview
|
||||
} : null);
|
||||
ensureSessionStateReady().then(() => {
|
||||
const session = sessions.get(message.sessionId);
|
||||
if (session?.audienceTabId != null) {
|
||||
chrome.tabs.sendMessage(session.audienceTabId, {
|
||||
type: "QLEARN_PRESENTER_COMMAND",
|
||||
command: "reportState"
|
||||
}).catch(() => {});
|
||||
}
|
||||
sendResponse(session ? {
|
||||
sessionId: session.id,
|
||||
mode: session.mode,
|
||||
current: session.current,
|
||||
count: session.count,
|
||||
pageTitle: session.pageTitle,
|
||||
audienceHidden: session.audienceHidden,
|
||||
audienceZoom: session.audienceZoom,
|
||||
audienceScrollY: session.audienceScrollY,
|
||||
currentPreview: session.currentPreview,
|
||||
nextPreview: session.nextPreview
|
||||
} : null);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
chrome.windows.onRemoved.addListener((windowId) => {
|
||||
const sessionId = windowToSession.get(windowId);
|
||||
if (!sessionId) return;
|
||||
const session = sessions.get(sessionId);
|
||||
if (!session) return;
|
||||
ensureSessionStateReady().then(() => {
|
||||
const sessionId = windowToSession.get(windowId);
|
||||
if (!sessionId) return;
|
||||
const session = sessions.get(sessionId);
|
||||
if (!session) return;
|
||||
|
||||
// Closing either dual-screen window ends the whole presentation.
|
||||
if (session.mode === "dual") {
|
||||
endSession(sessionId, "window-closed").catch(() => {});
|
||||
}
|
||||
// Closing either dual-screen window ends the whole presentation.
|
||||
if (session.mode === "dual") {
|
||||
endSession(sessionId, "window-closed").catch(() => {});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user