Skip to content

Commit

Permalink
fix: enable google contact search in firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
ioanmo226 committed Aug 30, 2024
1 parent a84810e commit 6347fe6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -861,9 +861,10 @@ export class ComposeRecipientsModule extends ViewModule<ComposeView> {
.on(
'click',
this.view.setHandler(async () => {
const authResult = await GoogleOAuth.newAuthPopup({
const authResult = await BrowserMsg.send.bg.await.reconnectAcctAuthPopup({
acctEmail: this.view.acctEmail,
scopes: GoogleOAuth.defaultScopes('contacts'),
screenDimensions: Ui.getScreenDimensions(),
});
if (authResult.result === 'Success') {
this.googleContactsSearchEnabled = true;
Expand Down
34 changes: 20 additions & 14 deletions extension/js/common/browser/browser-msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ export class BrowserMsg {
private static async sendAwait(destString: string | undefined, name: string, bm?: Dict<unknown>, awaitRes = false): Promise<Bm.Response> {
bm = bm || {};
// console.debug(`sendAwait ${name} to ${destString || 'bg'}`, bm);
const isBackgroundPage = Env.isBackgroundPage();
const isBackgroundPage = await Env.isBackgroundPage();
if (isBackgroundPage && BrowserMsg.HANDLERS_REGISTERED_BACKGROUND && typeof destString === 'undefined') {
// calling from bg script to bg script: skip messaging
const handler: Bm.AsyncRespondingHandler = BrowserMsg.HANDLERS_REGISTERED_BACKGROUND[name];
Expand All @@ -605,7 +605,7 @@ export class BrowserMsg {
return await BrowserMsg.sendRaw(destString, name, bm, awaitRes);
}

private static sendRaw(destString: string | undefined, name: string, bm: Dict<unknown>, awaitRes = false): Promise<Bm.Response> {
private static async sendRaw(destString: string | undefined, name: string, bm: Dict<unknown>, awaitRes = false): Promise<Bm.Response> {
const msg: Bm.Raw = {
name,
data: { bm },
Expand All @@ -614,7 +614,7 @@ export class BrowserMsg {
stack: Catch.stackTrace(),
};
// eslint-disable-next-line no-null/no-null
if (!Env.isBackgroundPage() && msg.to !== null) {
if (!(await Env.isBackgroundPage()) && msg.to !== null) {
const validMsg: Bm.RawWithWindowExtensions = { ...msg, to: msg.to };
// send via window messaging in parallel
Catch.try(async () => {
Expand Down Expand Up @@ -661,19 +661,25 @@ export class BrowserMsg {
};
try {
if (chrome.runtime) {
if (Env.isBackgroundPage()) {
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
for (const tab of tabs) {
chrome.tabs.sendMessage(Number(tab.id), msg, resolve);
Env.isBackgroundPage()
.then(isBackgroundPage => {
if (isBackgroundPage) {
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
for (const tab of tabs) {
chrome.tabs.sendMessage(Number(tab.id), msg, resolve);
}
});
} else {
if (awaitRes) {
chrome.runtime.sendMessage(msg, processRawMsgResponse);
} else {
void chrome.runtime.sendMessage(msg);
}
}
})
.catch((e: unknown) => {
throw e;
});
} else {
if (awaitRes) {
chrome.runtime.sendMessage(msg, processRawMsgResponse);
} else {
void chrome.runtime.sendMessage(msg);
}
}
} else {
BrowserMsg.renderFatalErrCorner('Error: missing chrome.runtime', 'RED-RELOAD-PROMPT');
}
Expand Down
12 changes: 6 additions & 6 deletions extension/js/common/browser/chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { AbstractStore } from '../platform/store/abstract-store.js';

type ChromeStorageType = 'local' | 'session';

const handleFatalErr = (reason: 'storage_undefined', error: Error) => {
const handleFatalErr = async (reason: 'storage_undefined', error: Error) => {
try {
if (Env.isBackgroundPage()) {
if (await Env.isBackgroundPage()) {
throw error;
} else if (Env.isContentScript()) {
console.error('Incomplete extension environment in content script', error);
Expand Down Expand Up @@ -46,7 +46,7 @@ export const windowsCreate = async (q: chrome.windows.CreateData): Promise<chrom
export const storageGet = async (storageType: ChromeStorageType, keys: string[]): Promise<Dict<unknown>> => {
return await new Promise((resolve, reject) => {
if (typeof chrome.storage === 'undefined') {
handleFatalErr('storage_undefined', new Error('storage is undefined'));
void handleFatalErr('storage_undefined', new Error('storage is undefined'));
} else {
const storage = chrome.storage[storageType];
storage.get(keys, result => {
Expand All @@ -65,7 +65,7 @@ export const storageGet = async (storageType: ChromeStorageType, keys: string[])
export const storageGetAll = async (storageType: ChromeStorageType): Promise<{ [key: string]: unknown }> => {
return await new Promise(resolve => {
if (typeof chrome.storage === 'undefined') {
handleFatalErr('storage_undefined', new Error('storage is undefined'));
void handleFatalErr('storage_undefined', new Error('storage is undefined'));
} else {
const storage = chrome.storage[storageType];
storage.get(resolve);
Expand All @@ -76,7 +76,7 @@ export const storageGetAll = async (storageType: ChromeStorageType): Promise<{ [
export const storageSet = async (storageType: ChromeStorageType, values: Dict<unknown>): Promise<void> => {
return await new Promise(resolve => {
if (typeof chrome.storage === 'undefined') {
handleFatalErr('storage_undefined', new Error('storage is undefined'));
void handleFatalErr('storage_undefined', new Error('storage is undefined'));
} else {
const storage = chrome.storage[storageType];
storage.set(values, resolve);
Expand All @@ -87,7 +87,7 @@ export const storageSet = async (storageType: ChromeStorageType, values: Dict<un
export const storageRemove = async (storageType: ChromeStorageType, keys: string[]): Promise<void> => {
return await new Promise(resolve => {
if (typeof chrome.storage === 'undefined') {
handleFatalErr('storage_undefined', new Error('storage is undefined'));
void handleFatalErr('storage_undefined', new Error('storage is undefined'));
} else {
const storage = chrome.storage[storageType];
storage.remove(keys, resolve);
Expand Down
7 changes: 6 additions & 1 deletion extension/js/common/browser/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ export class Env {
}

// Check if the current context is a Service Worker
public static isBackgroundPage() {
public static async isBackgroundPage() {
// In firefox, window&document is not null even in background page
if (typeof browser !== 'undefined' && typeof browser.runtime?.getBackgroundPage !== 'undefined') {
const backgroundPage = await browser.runtime.getBackgroundPage();
return backgroundPage === window;
}
return typeof window === 'undefined' && typeof document === 'undefined';
}

Expand Down

0 comments on commit 6347fe6

Please sign in to comment.