From 188ee5df4fcf81a4121db1744e1337697255cc0c Mon Sep 17 00:00:00 2001 From: Justin Baur <19896123+justindbaur@users.noreply.github.com> Date: Wed, 28 Sep 2022 18:06:10 -0400 Subject: [PATCH] Add Types for Opera API (#3631) * Add global types * Update to self * Remove main.backgroun changes * Remove one more change --- apps/browser/src/globals.d.ts | 132 +++++++++++++++++- .../services/browserPlatformUtils.service.ts | 8 +- 2 files changed, 135 insertions(+), 5 deletions(-) diff --git a/apps/browser/src/globals.d.ts b/apps/browser/src/globals.d.ts index 7307f696ae..9662c3d71b 100644 --- a/apps/browser/src/globals.d.ts +++ b/apps/browser/src/globals.d.ts @@ -1,4 +1,134 @@ declare function escape(s: string): string; declare function unescape(s: string): string; -declare let opr: any; +/** + * @link https://dev.opera.com/extensions/addons-api/ + */ +type OperaAddons = { + /** + * @link https://dev.opera.com/extensions/addons-api/#method-installextension + */ + installExtension: ( + id: string, + success_callback: () => void, + error_callback: (errorMessage: string) => void + ) => void; +}; + +type OperaEvent = { + addListener: (callback: (state: T) => void) => void; +}; + +/** + * @link https://dev.opera.com/extensions/sidebar-action-api/#type-colorarray + */ +type ColorArray = [number, number, number, number]; + +/** + * @link https://dev.opera.com/extensions/sidebar-action-api/#type-imagedatatype + */ +type ImageDataType = ImageData; + +/** + * @link https://dev.opera.com/extensions/sidebar-action-api/ + */ +type OperaSidebarAction = { + /** + * @link https://dev.opera.com/extensions/sidebar-action-api/#method-settitle + */ + setTitle: (details: { title: string; tabId?: number }) => void; + /** + * @link https://dev.opera.com/extensions/sidebar-action-api/#method-gettitle + */ + getTitle: (details: { tabId?: number }, callback: (result: string) => void) => void; + /** + * @link https://dev.opera.com/extensions/sidebar-action-api/#method-seticon + */ + setIcon: ( + details: { + imageData?: ImageDataType | Record; + path?: string | Record; + tabId?: number; + }, + callback?: () => void + ) => void; + /** + * @link https://dev.opera.com/extensions/sidebar-action-api/#method-setpanel + */ + setPanel: (details: { tabId?: number; panel: string }) => void; + /** + * @link https://dev.opera.com/extensions/sidebar-action-api/#method-getpanel + */ + getPanel: (details: { tabId?: number }, callback: (result: string) => void) => void; + /** + * *Not supported on mac* + * + * @link https://dev.opera.com/extensions/sidebar-action-api/#method-setbadgetext + */ + setBadgeText: (details: { text: string; tabId?: number }) => void; + /** + * *Not supported on mac* + * + * @link https://dev.opera.com/extensions/sidebar-action-api/#method-getbadgetext + */ + getBadgeText: (details: { tabId?: number }, callback: (result: string) => void) => void; + /** + * *Not supported on mac* + * + * @link https://dev.opera.com/extensions/sidebar-action-api/#method-setbadgebackgroundcolor + */ + setBadgeBackgroundColor: (details: { color: ColorArray | string; tabId?: number }) => void; + /** + * *Not supported on mac* + * + * @link https://dev.opera.com/extensions/sidebar-action-api/#method-getbadgebackgroundcolor + */ + getBadgeBackgroundColor: ( + details: { tabId?: number }, + callback: (result: ColorArray) => void + ) => void; + /** + * *Not supported on mac* + * + * @link https://dev.opera.com/extensions/sidebar-action-api/#events-onfocus + */ + onFocus: OperaEvent; + /** + * *Not supported on mac* + * + * @link https://dev.opera.com/extensions/sidebar-action-api/#events-onblur + */ + onBlur: OperaEvent; +}; + +type Opera = { + addons: OperaAddons; + sidebarAction: OperaSidebarAction; +}; + +declare namespace chrome { + /** + * This is for firefoxes sidebar action and it is based on the opera one but with a few less methods + * + * @link https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/sidebarAction + */ + let sidebarAction: + | Omit< + OperaSidebarAction, + | "setBadgeText" + | "getBadgeText" + | "setBadgeBackgroundColor" + | "getBadgeBackgroundColor" + | "onFocus" + | "onBlur" + > + | undefined; +} + +interface Window { + opr: Opera | undefined; + opera: unknown; +} + +declare let opr: Opera | undefined; +declare let opera: unknown | undefined; declare let safari: any; diff --git a/apps/browser/src/services/browserPlatformUtils.service.ts b/apps/browser/src/services/browserPlatformUtils.service.ts index 5e31d64dc0..a9f1c35567 100644 --- a/apps/browser/src/services/browserPlatformUtils.service.ts +++ b/apps/browser/src/services/browserPlatformUtils.service.ts @@ -33,8 +33,8 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService ) { this.deviceCache = DeviceType.FirefoxExtension; } else if ( - (!!(window as any).opr && !!opr.addons) || - !!(window as any).opera || + (self.opr && self.opr.addons) || + self.opera || navigator.userAgent.indexOf(" OPR/") >= 0 ) { this.deviceCache = DeviceType.OperaExtension; @@ -42,7 +42,7 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService this.deviceCache = DeviceType.EdgeExtension; } else if (navigator.userAgent.indexOf(" Vivaldi/") !== -1) { this.deviceCache = DeviceType.VivaldiExtension; - } else if ((window as any).chrome && navigator.userAgent.indexOf(" Chrome/") !== -1) { + } else if (window.chrome && navigator.userAgent.indexOf(" Chrome/") !== -1) { this.deviceCache = DeviceType.ChromeExtension; } else if (navigator.userAgent.indexOf(" Safari/") !== -1) { this.deviceCache = DeviceType.SafariExtension; @@ -335,7 +335,7 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService } sidebarViewName(): string { - if ((window as any).chrome.sidebarAction && this.isFirefox()) { + if (window.chrome.sidebarAction && this.isFirefox()) { return "sidebar"; } else if (this.isOpera() && typeof opr !== "undefined" && opr.sidebarAction) { return "sidebar_panel";