Skip to content

Commit

Permalink
Fix MV3 on Firefox for Android (#7464)
Browse files Browse the repository at this point in the history
* Fix on FIrefox for Android

* Format code

* Just use manifest icons when unmuting

* Fix double chrome

* Check if chrome.contextMenus is undefined directly

* More fixes

* Use optional chaining (`?.`) when possible

* Add code comment

---------

Co-authored-by: Samq64 <Samq64@users.noreply.github.com>
Co-authored-by: World_Languages <mundofinky@gmail.com>
  • Loading branch information
3 people committed May 29, 2024
1 parent b4389bc commit 90e0675
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
16 changes: 10 additions & 6 deletions background/handle-notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ chrome.storage.local.get("muted", (obj) => {
scratchAddons.muted = obj.muted;
});

chrome.contextMenus.removeAll();
chrome.contextMenus?.removeAll();
let currentMenuItem = null;

chrome.contextMenus.onClicked.addListener(({ parentMenuItemId, menuItemId }) => {
// NOTE: chrome.contextMenus equals `undefined` on Firefox for Android!

chrome.contextMenus?.onClicked.addListener(({ parentMenuItemId, menuItemId }) => {
if (parentMenuItemId === "mute") {
const mins = Number(menuItemId.split("_")[1]);
contextMenuMuted();
Expand All @@ -47,6 +49,7 @@ chrome.contextMenus.onClicked.addListener(({ parentMenuItemId, menuItemId }) =>
});

function contextMenuUnmuted() {
if (chrome.contextMenus === undefined) return; // Firefox for Android
if (currentMenuItem === "unmute") chrome.contextMenus.remove("unmute");
currentMenuItem = "mute";
chrome.contextMenus.create({
Expand All @@ -62,17 +65,18 @@ function contextMenuUnmuted() {
contexts: [BROWSER_ACTION],
});
}
// This seems to be run when the extension is loaded, so we'll just set the right icon here.
const prerelease = chrome.runtime.getManifest().version_name.includes("-prerelease");
chrome.browserAction.setIcon({
path: {
16: prerelease ? chrome.runtime.getURL("images/icon-blue-16.png") : chrome.runtime.getURL("images/icon-16.png"),
32: prerelease ? chrome.runtime.getURL("images/icon-blue-32.png") : chrome.runtime.getURL("images/icon-32.png"),
16: chrome.runtime.getURL(chrome.runtime.getManifest().icons["16"]),
32: chrome.runtime.getURL(chrome.runtime.getManifest().icons["32"]),
},
});
}

function contextMenuMuted() {
if (chrome.contextMenus === undefined) return; // Firefox for Android
// Note: in theory, this function is unreachable
// in FF for Android, but we early-return anyway.
if (currentMenuItem === "mute") chrome.contextMenus.remove("mute");
currentMenuItem = "unmute";
chrome.contextMenus.create({
Expand Down
1 change: 1 addition & 0 deletions webpages/popup/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="../set-lang.js" type="module"></script>
<script src="../check-unsupported.js" defer></script>
<style>
Expand Down

0 comments on commit 90e0675

Please sign in to comment.