-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
75 lines (62 loc) · 2.2 KB
/
server.js
File metadata and controls
75 lines (62 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const express = require('express');
const {launchPersistentContext, waitAndClickJoinButton} = require('./persistent-browser');
const app = express();
const port = 8080;
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));
app.use((req, res, next) => console.log(req.method, req.url, 'request received') || next());
let browserContext = null;
let page = null;
(async () => {
({ context: browserContext, page } = await launchPersistentContext());
await page.goto(`http://localhost:${port}/idle.html`);
})();
// Helper to parse URL or meet code
function parseMeetInput(input) {
const meetCodeRegex = /^[a-z]{3}-[a-z]{4}-[a-z]{3}$/i;
if (input.startsWith('https://meet.google.com/')) {
return input;
} else if (meetCodeRegex.test(input)) {
return `https://meet.google.com/${input}`;
} else {
return null;
}
}
app.post('/join', async (req, res) => {
const userInput = req.body.url;
const meetUrl = parseMeetInput(userInput);
if (!meetUrl) {
return res.send('❌ Invalid Google Meet URL or Code');
}
try {
await page.goto(meetUrl);
const joined = await waitAndClickJoinButton(page);
if (!joined) {
return res.send('❌ Failed to join meeting');
}
res.send('✅ Meeting launched and joined on TV');
} catch (err) {
console.error('Error joining meeting:', err);
res.send('❌ Failed to join meeting');
}
});
app.get('/reset', async (req, res) => {
try {
const leaveBtn = await page.locator('[aria-label="Leave call"]').first();
if (await leaveBtn.isVisible()) {
await leaveBtn.click();
console.log('✅ Clicked "Leave call"');
await page.waitForTimeout(500);
} else {
console.warn('⚠️ Leave button not found');
}
await page.goto(`http://localhost:${port}/idle.html`);
res.send('✅ TV reset to idle screen');
} catch (err) {
console.warn('⚠️ Failed to leave call or go to idle:', err);
res.send('❌ Failed to reset');
}
});
app.listen(port, () => {
console.log(`TV Meet Controller running on http://localhost:${port}`);
});