From 33fcf0ab2105b37c8d5b9dc7245224e7a6b5dae4 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Sun, 20 Sep 2020 12:35:43 +0530 Subject: [PATCH] Move match actions to background.js. Send notifications and play audio via message passing. --- background.js | 13 +++++++++++++ gmcr.js | 9 ++++----- manifest.json | 7 ++++++- 3 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 background.js diff --git a/background.js b/background.js new file mode 100644 index 0000000..a858565 --- /dev/null +++ b/background.js @@ -0,0 +1,13 @@ +function handleMatch(match) { + new Notification("Match found", {body: match}); + const audio = new Audio(browser.runtime.getURL("static/notif.mp3")); + audio.play(); +} + + +function handleMessage(request) { + if (request.type == "match") + return handleMatch(request.match); +} + +browser.runtime.onMessage.addListener(handleMessage); diff --git a/gmcr.js b/gmcr.js index 74e3aeb..081408e 100644 --- a/gmcr.js +++ b/gmcr.js @@ -13,7 +13,6 @@ function surroundText(haystack, wordList, padding) { surroundingText += "..."; surroundingText += "
"; - console.log(start, end, surroundingText); }); return surroundingText.trim(); } @@ -52,15 +51,15 @@ function captionUpdate() { } else { [ matched, surroundingText ] = simpleMatch(captions, storage.words); } - console.log(matched, surroundingText); /* The lastNotification prevents repetitive notifications because * of constant updates. */ if (matched && surroundingText !== lastNotification) { - new Notification("Match found", {body: surroundingText}); + browser.runtime.sendMessage({ + type: "match", + match: surroundingText + }); lastNotification = surroundingText; - const audio = new Audio(browser.runtime.getURL("static/notif.mp3")); - audio.play(); } }) } diff --git a/manifest.json b/manifest.json index ce2996b..c4ca8f1 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Google Meet Caption Regex", - "version": "1.2", + "version": "1.3", "description": "Send a notification when a caption in Google Meet matches a certain regex.", "homepage_url": "https://gitlab.com/ceda_ei/google-meet-captions-regex", @@ -22,11 +22,16 @@ "default_popup": "popup/build/index.html" }, + "background": { + "scripts": ["background.js"] + }, + "content_scripts": [ { "matches": ["https://meet.google.com/*"], "js": ["gmcr.js"] } ], + "web_accessible_resources": ["static/*"] }