-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
138 lines (118 loc) · 3.81 KB
/
init.lua
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
logger = hs.logger.new("GoogleMeet", "info")
local function loadfile(filename)
local filepath = hs.spoons.resourcePath(filename)
local file = io.open(filepath, "r")
if file == nil then
logger.e("Could not open file: " .. filename)
return
end
local contents = file:read("*a")
file:close()
return contents
end
-- Function to execute JavaScript via AppleScript
local function executeJavaScript(jsCode)
local contents = loadfile("run-js-on-google-meet.applescript")
local oldString = "alert%('Hello, world!'%)"
local newString = jsCode:gsub('"', '\\"')
local modifiedContents = string.gsub(contents, oldString, newString)
-- logger.d("Applescript: \n" .. modifiedContents)
local ok, output, _ = hs.osascript.applescript(modifiedContents)
logger.d("ok: ", ok, " output: ", output, " raw output: ", _)
if not ok then
logger.e("Applescript failed: \n" .. modifiedContents)
return ok
end
return output
end
local function executeJavaScriptFromFile(file)
local jsCode = loadfile(file)
return executeJavaScript(jsCode)
end
-- Helper function to execute a JS function with a selector
local function clickElement(selector)
logger.d("Clicking element: " .. selector)
local contents = loadfile("click-element.js")
local jsCode = string.gsub(contents, "<<selector>>", selector)
return executeJavaScript(jsCode)
end
-- Utility function to execute Google Meet commands
local function executeMeetCmd(toggleFeature, shortcut)
hs.application.launchOrFocus("Google Meet")
hs.timer.doAfter(0.1, function()
hs.eventtap.keyStroke({"cmd"}, shortcut)
end)
logger.i("Toggled " .. toggleFeature .. " on Google Meet")
return true
end
local function toggleMic()
logger.i('Toggle Microphone')
if not clickElement('button[aria-label="Turn off microphone (⌘ + d)"]') then
clickElement('button[aria-label="Turn on microphone (⌘ + d)"]')
end
end
local function toggleCamera()
logger.i('Toggle camera')
if not clickElement('button[aria-label="Turn off camera (⌘ + e)"]') then
clickElement('button[aria-label="Turn on camera (⌘ + e)"]')
end
end
local function joinActualMeeting()
clickElement('button[jsname="Qx7uuf"]')
end
-- Function to join the next meeting
local function joinNextMeeting()
logger.i("Joining Meeting")
if executeJavaScriptFromFile("click-on-closest-time.js") then
hs.timer.doAfter(5, joinActualMeeting)
return true
else
logger.e('Failed to click on the next meeting')
end
return false
end
local function joinMeetingOnSchedule()
logger.i("Checking if meeting started, if started, click it")
if executeJavaScriptFromFile("click-on-meeting-starting.js") then
hs.timer.doAfter(7, joinActualMeeting)
return true
end
return false
end
-- Function to join the next meeting
local function LeaveMeetingAndJoinNext()
logger.i("Leaving meeting and joinin the next one")
clickElement('button[aria-label="Leave call"]')
hs.timer.doAfter(4, function()
clickElement('button[jsname="dqt8Pb"]')
hs.timer.doAfter(4, function()
joinNextMeeting()
end)
end)
return true
end
-- Create the Spoon object
local obj = {}
-- Metadata
obj.name = "GoogleMeet"
obj.version = "1.0"
obj.author = "Noam Elfanbaum"
function obj:bindHotKeys(mapping)
local spec = {
toggleMic = toggleMic,
toggleCamera = toggleCamera,
joinNextMeeting = joinNextMeeting,
LeaveMeetingAndJoinNext = LeaveMeetingAndJoinNext
}
hs.spoons.bindHotkeysToSpec(spec, mapping)
return self
end
function obj:start()
hs.timer.new(10, joinMeetingOnSchedule):start()
return self
end
function obj:setLogLevel(level)
logger.setLogLevel(level)
return self
end
return obj