-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(): cross-browser compatibility (Firefox and Chrome support)
- Loading branch information
Showing
12 changed files
with
435 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* Wrapper function to set the popup of the browser action. | ||
* @param popup - The URL of the HTML file to be used as a popup. An empty string clears the popup. | ||
* @returns A Promise that resolves when the popup is set. | ||
*/ | ||
function setBrowserActionPopup(popup: string): Promise<void> { | ||
return new Promise<void>((resolve, reject) => { | ||
if (typeof browser !== 'undefined' && browser.browserAction) { | ||
// Firefox WebExtensions API | ||
browser.browserAction.setPopup({ popup }) | ||
.then(() => { | ||
resolve(); | ||
}) | ||
.catch((error) => { | ||
reject(error); | ||
}); | ||
} else if (typeof chrome !== 'undefined' && chrome.browserAction) { | ||
// Chrome Extension API | ||
chrome.browserAction.setPopup({ popup }, () => { | ||
if (chrome.runtime.lastError) { | ||
reject(chrome.runtime.lastError); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
} else { | ||
reject(new Error('Unsupported browser.')); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Wrapper function to set the title of the browser action. | ||
* @param title - The title text to set. | ||
* @returns A Promise that resolves when the title is set. | ||
*/ | ||
function setBrowserActionTitle(title: string): Promise<void> { | ||
return new Promise<void>((resolve, reject) => { | ||
if (typeof browser !== 'undefined' && browser.browserAction) { | ||
// Firefox WebExtensions API | ||
browser.browserAction.setTitle({ title }) | ||
.then(() => { | ||
resolve(); | ||
}) | ||
.catch((error) => { | ||
reject(error); | ||
}); | ||
} else if (typeof chrome !== 'undefined' && chrome.browserAction) { | ||
// Chrome Extension API | ||
chrome.browserAction.setTitle({ title }, () => { | ||
if (chrome.runtime.lastError) { | ||
reject(chrome.runtime.lastError); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
} else { | ||
reject(new Error('Unsupported browser.')); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Wrapper function to add a click event listener for the browser action. | ||
* @param listener - The callback function to be executed when the browser action is clicked. | ||
*/ | ||
function onClicked(listener: () => void): void { | ||
if (typeof browser !== 'undefined' && browser.browserAction) { | ||
// Firefox WebExtensions API | ||
browser.browserAction.onClicked.addListener(listener); | ||
} else if (typeof chrome !== 'undefined' && chrome.browserAction) { | ||
// Chrome Extension API | ||
chrome.browserAction.onClicked.addListener(listener); | ||
} else { | ||
console.error('Unsupported browser.'); | ||
} | ||
} | ||
|
||
export const browserAction = { | ||
setBrowserActionPopup, | ||
setBrowserActionTitle, | ||
onClicked | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { storage } from './storage'; | ||
import { runtime } from './runtime'; | ||
import { browserAction } from './browser-action'; | ||
import { tabs } from './tabs'; | ||
|
||
export const api = { | ||
storage, | ||
runtime, | ||
browserAction, | ||
tabs, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* Get the extension ID, which is unique to each extension. | ||
* @returns The extension ID. | ||
*/ | ||
function getExtensionId(): string | null { | ||
if (typeof browser !== 'undefined' && browser.runtime) { | ||
// Firefox WebExtensions API | ||
return browser.runtime.id; | ||
} else if (typeof chrome !== 'undefined' && chrome.runtime) { | ||
// Chrome Extension API | ||
return chrome.runtime.id; | ||
} else { | ||
console.error('Unsupported browser.'); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Get the background page of the extension. | ||
* @param callback - A callback function that receives the background page as an argument. | ||
*/ | ||
function getBackgroundPage(callback: (backgroundPage: any) => void): void { | ||
if (typeof browser !== 'undefined' && browser.runtime) { | ||
// Firefox WebExtensions API | ||
browser.runtime.getBackgroundPage().then(callback); | ||
} else if (typeof chrome !== 'undefined' && chrome.runtime) { | ||
// Chrome Extension API | ||
chrome.runtime.getBackgroundPage(callback); | ||
} else { | ||
console.error('Unsupported browser.'); | ||
} | ||
} | ||
|
||
/** | ||
* Open the options page of the extension, if defined in the manifest. | ||
*/ | ||
function openOptionsPage(): void { | ||
console.log('here') | ||
if (typeof browser !== 'undefined' && browser.runtime) { | ||
// Firefox WebExtensions API | ||
browser.runtime.openOptionsPage(); | ||
} else if (typeof chrome !== 'undefined' && chrome.runtime) { | ||
// Chrome Extension API | ||
chrome.runtime.openOptionsPage(); | ||
} else { | ||
console.error('Unsupported browser.'); | ||
} | ||
} | ||
|
||
/** | ||
* Send a message to the extension's background script. | ||
* @param message - The message to send. | ||
* @param callback - A callback function that will be called with the response (if any). | ||
*/ | ||
function sendMessage(message: any, callback?: (response: any) => void): void { | ||
if (typeof browser !== 'undefined' && browser.runtime) { | ||
// Firefox WebExtensions API | ||
browser.runtime.sendMessage(message, callback); | ||
} else if (typeof chrome !== 'undefined' && chrome.runtime) { | ||
// Chrome Extension API | ||
chrome.runtime.sendMessage(message, callback); | ||
} else { | ||
console.error('Unsupported browser.'); | ||
} | ||
} | ||
|
||
/** | ||
* Wrapper function to add a message event listener for the runtime. | ||
* @param listener - The callback function to be executed when a message is received. | ||
*/ | ||
function onMessage(listener: (msg: any, sender: any) => void): void { | ||
if (typeof browser !== 'undefined' && browser.runtime) { | ||
// Firefox WebExtensions API | ||
browser.runtime.onMessage.addListener(listener); | ||
} else if (typeof chrome !== 'undefined' && chrome.runtime) { | ||
// Chrome Extension API | ||
chrome.runtime.onMessage.addListener(listener); | ||
} else { | ||
console.error('Unsupported browser.'); | ||
} | ||
} | ||
|
||
export const runtime = { | ||
getExtensionId, | ||
getBackgroundPage, | ||
openOptionsPage, | ||
sendMessage, | ||
onMessage, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* Wrapper function to get items from storage. | ||
* @param keys - A string, array of strings, or object with keys to retrieve from storage. | ||
* @param callback - A callback function that receives the retrieved items. | ||
*/ | ||
function getStorageItems( | ||
keys: string | string[] | Record<string, any>, | ||
callback: (result: Record<string, any>) => void | ||
): void { | ||
if (typeof browser !== 'undefined' && browser.storage) { | ||
// Firefox WebExtensions API | ||
browser.storage.sync.get(keys).then(callback); | ||
} else if (typeof chrome !== 'undefined' && chrome.storage) { | ||
// Chrome Extension API | ||
chrome.storage.sync.get(keys, callback); | ||
} else { | ||
console.error('Unsupported browser.'); | ||
} | ||
} | ||
|
||
/** | ||
* Wrapper function to set items in storage. | ||
* @param items - An object containing items to be stored. | ||
* @param callback - A callback function to be executed after the items have been stored. | ||
*/ | ||
function setStorageItems(items: Record<string, any>, callback?: () => void): void { | ||
if (typeof browser !== 'undefined' && browser.storage) { | ||
// Firefox WebExtensions API | ||
browser.storage.sync.set(items).then(callback); | ||
} else if (typeof chrome !== 'undefined' && chrome.storage) { | ||
// Chrome Extension API | ||
chrome.storage.sync.set(items, callback); | ||
} else { | ||
console.error('Unsupported browser.'); | ||
} | ||
} | ||
|
||
/** | ||
* Wrapper function to remove items from storage. | ||
* @param keys - A string or array of strings representing keys to remove from storage. | ||
* @param callback - A callback function to be executed after the items have been removed. | ||
*/ | ||
function removeStorageItems(keys: string | string[], callback?: () => void): void { | ||
if (typeof browser !== 'undefined' && browser.storage) { | ||
// Firefox WebExtensions API | ||
browser.storage.sync.remove(keys).then(callback); | ||
} else if (typeof chrome !== 'undefined' && chrome.storage) { | ||
// Chrome Extension API | ||
chrome.storage.sync.remove(keys, callback); | ||
} else { | ||
console.error('Unsupported browser.'); | ||
} | ||
} | ||
|
||
/** | ||
* Wrapper function to clear all items from storage. | ||
* @param callback - A callback function to be executed after storage is cleared. | ||
*/ | ||
function clearStorage(callback?: () => void): void { | ||
if (typeof browser !== 'undefined' && browser.storage) { | ||
// Firefox WebExtensions API | ||
browser.storage.sync.clear().then(callback); | ||
} else if (typeof chrome !== 'undefined' && chrome.storage) { | ||
// Chrome Extension API | ||
chrome.storage.sync.clear(callback); | ||
} else { | ||
console.error('Unsupported browser.'); | ||
} | ||
} | ||
|
||
export const storage = { | ||
getStorageItems, | ||
setStorageItems, | ||
removeStorageItems, | ||
clearStorage | ||
} |
Oops, something went wrong.