Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor messageHandler.js to reuse badge.displayBrowserActionBadge #1561

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions src/js/background/badge.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
const MAJOR_VERSIONS = ["2.3.0", "2.4.0"];
const badge = {
async init() {
const currentWindow = await browser.windows.getCurrent();
this.displayBrowserActionBadge(currentWindow);
async init() {
this.displayBrowserActionBadge("showVersionIndicator");
},

async displayBrowserActionBadge() {
const extensionInfo = await backgroundLogic.getExtensionInfo();
const storage = await browser.storage.local.get({ browserActionBadgesClicked: [] });
disableAddon(tabId) {
browser.browserAction.disable(tabId);
browser.browserAction.setTitle({ tabId, title: "Containers disabled in Private Browsing Mode" });
},

if (MAJOR_VERSIONS.indexOf(extensionInfo.version) > -1 &&
storage.browserActionBadgesClicked.indexOf(extensionInfo.version) < 0) {
browser.browserAction.setBadgeBackgroundColor({ color: "rgba(0,217,0,255)" });
browser.browserAction.setBadgeText({ text: "NEW" });
async displayBrowserActionBadge(action) {
const extensionInfo = await backgroundLogic.getExtensionInfo();
function changeBadgeColorText(color, text){
browser.browserAction.setBadgeBackgroundColor({color: color});
browser.browserAction.setBadgeText({text: text});
}
if(action==="showVersionIndicator") {
const ActionBadgesClickedStorage = await browser.storage.local.get({browserActionBadgesClicked: []});
if (MAJOR_VERSIONS.indexOf(extensionInfo.version) > -1 &&
ActionBadgesClickedStorage.browserActionBadgesClicked.indexOf(extensionInfo.version) < 0) {
changeBadgeColorText("rgba(0,217,0,255)", "NEW");
}
}
else if (action==="showAchievement") {
const achievementsStorage = await browser.storage.local.get({achievements: []});
achievementsStorage.achievements.push({"name": "manyContainersOpened", "done": false});
// use set and spread to create a unique array
const achievements = [...new Set(achievementsStorage.achievements)];
browser.storage.local.set({achievements});
changeBadgeColorText("rgba(0,217,0,255)", "NEW");
}
}

};

badge.init();
16 changes: 4 additions & 12 deletions src/js/background/messageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ const messageHandler = {
}, {urls: ["<all_urls>"], types: ["main_frame"]});

browser.tabs.onCreated.addListener((tab) => {
if (tab.incognito) {
badge.disableAddon(tab.id);
}
// lets remember the last tab created so we can close it if it looks like a redirect
this.lastCreatedTab = tab;
if (tab.cookieStoreId) {
Expand Down Expand Up @@ -168,23 +171,12 @@ const messageHandler = {

// When the user opens their _ tab, give them the achievement
if (countOfContainerTabsOpened === 100) {
const storage = await browser.storage.local.get({achievements: []});
storage.achievements.push({"name": "manyContainersOpened", "done": false});
// use set and spread to create a unique array
const achievements = [...new Set(storage.achievements)];
browser.storage.local.set({achievements});
browser.browserAction.setBadgeBackgroundColor({color: "rgba(0,217,0,255)"});
browser.browserAction.setBadgeText({text: "NEW"});
badge.displayBrowserActionBadge("showAchievement");
}
},

async onFocusChangedCallback(windowId) {
assignManager.removeContextMenu();
// browserAction loses background color in new windows ...
// https://bugzil.la/1314674
// https://github.com/mozilla/testpilot-containers/issues/608
// ... so re-call displayBrowserActionBadge on window changes
badge.displayBrowserActionBadge();
browser.tabs.query({active: true, windowId}).then((tabs) => {
if (tabs && tabs[0]) {
assignManager.calculateContextMenu(tabs[0]);
Expand Down