Skip to content

Commit

Permalink
Replace unreliable onBeforeUnload with tabs.onRemoved
Browse files Browse the repository at this point in the history
  • Loading branch information
vivek-nexus committed Jun 20, 2024
1 parent 9146a3e commit 6476fd2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 41 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Simple Google Meet transcripts. Private and open source.

![marquee-large](/assets/marquee-large.png)

Extension status: 🟢 OPERATIONAL (v2.1.2)
Extension status: 🟢 OPERATIONAL (v2.1.3)

<br />
<br />
Expand Down
38 changes: 25 additions & 13 deletions extension/background.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
console.log(message.type)
if (message.type == "save_and_download") {
chrome.storage.local.set(
{
transcript: message.transcript,
chatMessages: message.chatMessages,
meetingTitle: message.meetingTitle,
meetingStartTimeStamp: message.meetingStartTimeStamp
},
function () {
console.log("Saved transcript and meta data, downloading now if non empty")
// Download only if any transcript is present, irrespective of chat messages
if (message.transcript.length > 0)
downloadTranscript()
if (message.type == "new_meeting_started") {
// Saving current tab id, to download transcript when this tab is closed
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const tabId = tabs[0].id
chrome.storage.local.set({ meetingTabId: tabId }, function () {
console.log("Meeting tab id saved")
})
})
}
if (message.type == "download") {
// Invalidate tab id since transcript is downloaded, prevents double downloading of transcript from tab closed event listener
chrome.storage.local.set({ meetingTabId: null }, function () {
console.log("Meeting tab id cleared")
})
downloadTranscript()
}
return true
})

// Download transcript if meeting tab is closed
chrome.tabs.onRemoved.addListener(function (tabid) {
chrome.storage.local.get(["meetingTabId"], function (data) {
if (tabid == data.meetingTabId) {
console.log("Successfully intercepted tab close")
downloadTranscript()
// Clearing meetingTabId to prevent misfires of onRemoved until next meeting actually starts
chrome.storage.local.set({ meetingTabId: null }, function () {
console.log("Meeting tab id cleared for next meeting")
})
}
})
})

function downloadTranscript() {
chrome.storage.local.get(["userName", "transcript", "chatMessages", "meetingTitle", "meetingStartTimeStamp"], function (result) {
if (result.userName && result.transcript && result.chatMessages) {
Expand Down
31 changes: 4 additions & 27 deletions extension/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ checkExtensionStatus().then(() => {
// CRITICAL DOM DEPENDENCY. Wait until the meeting end icon appears, used to detect meeting start
checkElement(".google-material-icons", "call_end").then(() => {
console.log("Meeting started")
chrome.runtime.sendMessage({ type: "new_meeting_started" }, function (response) {
console.log(response);
});
hasMeetingStarted = true

try {
Expand Down Expand Up @@ -134,19 +137,14 @@ checkExtensionStatus().then(() => {


//*********** MEETING END ROUTINES **********//
// Event listener to capture browser tab or window close
window.addEventListener("beforeunload", unloadCallback)

// CRITICAL DOM DEPENDENCY. Event listener to capture meeting end button click by user
contains(".google-material-icons", "call_end")[0].parentElement.addEventListener("click", () => {
// To suppress further errors
hasMeetingEnded = true
// Remove unload event listener registered earlier, to prevent double downloads. Otherwise, unload event will trigger the callback, when user navigates away from meeting end page.
window.removeEventListener("beforeunload", unloadCallback)
transcriptObserver.disconnect()
chatMessagesObserver.disconnect()

// Push any data in the buffer variables to the transcript array, but avoid pushing blank ones. Handles one or more speaking when meeting ends.
// Push any data in the buffer variables to the transcript array, but avoid pushing blank ones. Needed to handle one or more speaking when meeting ends.
if ((personNameBuffer != "") && (transcriptTextBuffer != ""))
pushBufferToTranscript()
// Save to chrome storage and send message to download transcript from background script
Expand Down Expand Up @@ -272,27 +270,6 @@ const commonCSS = `background: rgb(255 255 255 / 10%);
font-family: 'Google Sans',Roboto,Arial,sans-serif;
box-shadow: rgba(0, 0, 0, 0.16) 0px 10px 36px 0px, rgba(0, 0, 0, 0.06) 0px 0px 0px 1px;`;

// Pushes any data in the buffer to transcript and tells background script to save it and download it
function unloadCallback() {
// To suppress further errors
hasMeetingEnded = true
// Push any data in the buffer variables to the transcript array, but avoid pushing blank ones. Handles one or more speaking when meeting ends.
if ((personNameBuffer != "") && (transcriptTextBuffer != ""))
pushBufferToTranscript()
// Send a message to save to chrome storage as well as download. Saving is offloaded to background script, since browser often aborts this long operation on unload
chrome.runtime.sendMessage(
{
type: "save_and_download",
transcript: transcript,
chatMessages: chatMessages,
meetingTitle: meetingTitle,
meetingStartTimeStamp: meetingStartTimeStamp,
},
function (response) {
console.log(response)
})
}

// Callback function to execute when transcription mutations are observed.
function transcriber(mutationsList, observer) {
// Delay for 1000ms to allow for text corrections by Meet.
Expand Down

0 comments on commit 6476fd2

Please sign in to comment.