forked from walters954/why-salesforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
setting and reading to/from the same key
- Loading branch information
Alfredo
committed
Feb 22, 2024
1 parent
e155407
commit 93ad7b0
Showing
4 changed files
with
37 additions
and
47 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 |
---|---|---|
@@ -1,31 +1,41 @@ | ||
const isChrome = navigator.userAgent.includes("Chrome"); | ||
const browserObj = isChrome ? chrome : browser; | ||
const whyKey = 'sfmWhySF'; | ||
|
||
function addKey(items, callback){ | ||
items.key = whyKey; | ||
callback(items); | ||
} | ||
|
||
async function getStorage(callback){ | ||
console.log({isChrome}); | ||
if(isChrome) | ||
return chrome.storage.sync.get([whyKey], callback);//chrome | ||
//firefox | ||
const result = await browser.storage.sync.get([whyKey]); | ||
callback(await result); | ||
browserObj.storage.sync.get([whyKey], items => { | ||
addKey(items, callback); | ||
}); | ||
} | ||
|
||
function setStorage(tabs){ | ||
// Save it using the Chrome extension storage API. | ||
chrome.storage.sync.set({whyKey: tabs}, function() { | ||
function setStorage(tabs, callback){ | ||
const set = {}; | ||
set[whyKey] = tabs; | ||
browserObj.storage.sync.set(set, function() { | ||
//TODO notify user of save | ||
console.log("saved"); | ||
console.log("saved",set); | ||
callback(null); | ||
}); | ||
} | ||
|
||
// need to use message in order to work in private windows | ||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { | ||
browserObj.runtime.onMessage.addListener((request, sender, sendResponse) => { | ||
const message = request.message; | ||
if(message.what === "get") | ||
return getStorage(sendResponse); | ||
if(message.what === "set") | ||
return setStorage(message.tabs); | ||
if(message.what === "getKey") | ||
return sendResponse(whyKey) | ||
console.error(`invalid message: ${message}`); | ||
if(message == null || message.what == null){ | ||
console.error(`invalid message: ${message}`); | ||
sendResponse(null); | ||
return false; | ||
} | ||
if(message.what === "get"){ | ||
getStorage(sendResponse); | ||
} | ||
else if(message.what === "set"){ | ||
setStorage(message.tabs, sendResponse); | ||
} | ||
return true;// will call sendResponse asynchronously | ||
}); |
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