Move match actions to background.js.

Send notifications and play audio via message passing.
This commit is contained in:
Ceda EI 2020-09-20 12:35:43 +05:30
parent d2d435527e
commit 33fcf0ab21
3 changed files with 23 additions and 6 deletions

13
background.js Normal file
View File

@ -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);

View File

@ -13,7 +13,6 @@ function surroundText(haystack, wordList, padding) {
surroundingText += "..."; surroundingText += "...";
surroundingText += "<br>"; surroundingText += "<br>";
console.log(start, end, surroundingText);
}); });
return surroundingText.trim(); return surroundingText.trim();
} }
@ -52,15 +51,15 @@ function captionUpdate() {
} else { } else {
[ matched, surroundingText ] = simpleMatch(captions, storage.words); [ matched, surroundingText ] = simpleMatch(captions, storage.words);
} }
console.log(matched, surroundingText);
/* The lastNotification prevents repetitive notifications because /* The lastNotification prevents repetitive notifications because
* of constant updates. * of constant updates.
*/ */
if (matched && surroundingText !== lastNotification) { if (matched && surroundingText !== lastNotification) {
new Notification("Match found", {body: surroundingText}); browser.runtime.sendMessage({
type: "match",
match: surroundingText
});
lastNotification = surroundingText; lastNotification = surroundingText;
const audio = new Audio(browser.runtime.getURL("static/notif.mp3"));
audio.play();
} }
}) })
} }

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "Google Meet Caption Regex", "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.", "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", "homepage_url": "https://gitlab.com/ceda_ei/google-meet-captions-regex",
@ -22,11 +22,16 @@
"default_popup": "popup/build/index.html" "default_popup": "popup/build/index.html"
}, },
"background": {
"scripts": ["background.js"]
},
"content_scripts": [ "content_scripts": [
{ {
"matches": ["https://meet.google.com/*"], "matches": ["https://meet.google.com/*"],
"js": ["gmcr.js"] "js": ["gmcr.js"]
} }
], ],
"web_accessible_resources": ["static/*"] "web_accessible_resources": ["static/*"]
} }