-
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.
adapt common.js for running using the chrome V3 API
- Loading branch information
Showing
5 changed files
with
648 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Higher-order function to select the correct version of a function | ||
function selectVersion(v2Function, v3Function) { | ||
return chrome.runtime.getManifest().manifest_version === 2 ? v2Function : v3Function; | ||
} | ||
|
||
// Create a wrapper function for chrome.storage.sync.get that returns a Promise | ||
function getFromStoragePromiseWrapper(key) { | ||
return new Promise((resolve, reject) => { | ||
chrome.storage.sync.get(key, (result) => { | ||
if (chrome.runtime.lastError) { | ||
reject(chrome.runtime.lastError); | ||
} else { | ||
resolve(result); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
// Use chrome.storage.sync.get that returns a Promise | ||
function getFromStoragePromise(key) { | ||
return chrome.storage.sync.get(key); | ||
} | ||
|
||
// Export the correct functions based on the manifest version | ||
export const getFromStorage = selectVersion(getFromStoragePromiseWrapper, getFromStoragePromise); | ||
|
||
// Create a wrapper function for chrome.storage.sync.set that returns a Promise | ||
function setToStoragePromiseWrapper(obj) { | ||
return new Promise((resolve, reject) => { | ||
chrome.storage.sync.set(obj, () => { | ||
if (chrome.runtime.lastError) { | ||
reject(chrome.runtime.lastError); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
// Use chrome.storage.sync.set that returns a Promise | ||
function setToStoragePromise(obj) { | ||
return chrome.storage.sync.set(obj); | ||
} | ||
|
||
// Export the correct functions based on the manifest version | ||
export const setToStorage = selectVersion(setToStoragePromiseWrapper, setToStoragePromise); | ||
|
||
// Create a wrapper function for chrome.notifications.create that returns a Promise | ||
function createNotificationPromiseWrapper(id, options) { | ||
return new Promise((resolve, reject) => { | ||
chrome.notifications.create(id, options, (notificationId) => { | ||
if (chrome.runtime.lastError) { | ||
reject(chrome.runtime.lastError); | ||
} else { | ||
resolve(notificationId); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
// Use chrome.notifications.create that returns a Promise | ||
function createNotificationPromise(id, options) { | ||
return chrome.notifications.create(id, options); | ||
} | ||
|
||
export const createNotification = selectVersion(createNotifactionPromiseWrapper, createNotificationPromise); | ||
|
||
// Create a wrapper function for chrome.tabs.query that returns a Promise | ||
function queryTabsPromiseWrapper(queryInfo) { | ||
return new Promise((resolve, reject) => { | ||
chrome.tabs.query(queryInfo, (tabs) => { | ||
if (chrome.runtime.lastError) { | ||
reject(chrome.runtime.lastError); | ||
} else { | ||
resolve(tabs); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
// Use chrome.tabs.query that returns a Promise | ||
function queryTabsPromise(queryInfo) { | ||
return chrome.tabs.query(queryInfo); | ||
} | ||
|
||
export const queryTabs = selectVersion(queryTabsPromiseWrapper, queryTabsNotificationPromise); | ||
|
||
// V2 version of addNotificationClickListener | ||
function addNotificationClickListenerWithNotifactions(clickHandler) { | ||
chrome.notifications.onClicked.addListener((notificationId) => { | ||
clickHandler(notificationId); | ||
}); | ||
} | ||
|
||
// V3 version of addNotificationClickListener | ||
function addNotificationClickListenerWithAction(clickHandler) { | ||
chrome.action.onClicked.addListener((tab) => { | ||
clickHandler(tab.id); | ||
}); | ||
} | ||
|
||
// Export the correct function based on the manifest version | ||
export const addNotificationClickListener = | ||
selectVersion(addNotificationClickListenerWithNotifactions, addNotificationClickListenerWithAction); |
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
Oops, something went wrong.