Skip to content

Commit 1bc3d7d

Browse files
committed
Use sync storage if available, fallback to local.
1 parent ebb7060 commit 1bc3d7d

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/background.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,26 @@
33
const GITHUB_TOKEN_KEY = 'x-github-token'
44
const TOKEN_FEATURE_INFORMATION_KEY = 'user-knows-token-feature'
55

6+
const storage = chrome.storage.sync || chrome.storage.local
7+
68
function setGithubToken (key, cb) {
79
const obj = {}
810
obj[GITHUB_TOKEN_KEY] = key
911

10-
chrome.storage.local.set(obj, function () {
12+
storage.set(obj, function () {
1113
alert('Your Github token has been set successfully. Reload the Github page to see changes.')
1214

1315
cb()
1416
})
1517
}
1618

1719
function handleOldGithubToken (cb) {
18-
chrome.storage.local.get(GITHUB_TOKEN_KEY, function (storedData) {
20+
storage.get(GITHUB_TOKEN_KEY, function (storedData) {
1921
const oldGithubToken = storedData[GITHUB_TOKEN_KEY]
2022

2123
if (oldGithubToken) {
2224
if (confirm('You have already set your Github token. Do you want to remove it?')) {
23-
chrome.storage.local.remove(GITHUB_TOKEN_KEY, function () {
25+
storage.remove(GITHUB_TOKEN_KEY, function () {
2426
alert('You have successfully removed Github token. Click extension icon again to set a new token.')
2527

2628
cb(false)
@@ -38,11 +40,11 @@ function userNowKnowsAboutGithubTokenFeature (cb) {
3840
const obj = {}
3941
obj[TOKEN_FEATURE_INFORMATION_KEY] = true
4042

41-
chrome.storage.local.set(obj, cb)
43+
storage.set(obj, cb)
4244
}
4345

4446
function informUserAboutGithubTokenFeature () {
45-
chrome.storage.local.get(TOKEN_FEATURE_INFORMATION_KEY, function (storedData) {
47+
storage.get(TOKEN_FEATURE_INFORMATION_KEY, function (storedData) {
4648
const userKnows = storedData[TOKEN_FEATURE_INFORMATION_KEY]
4749

4850
if (!userKnows) {

src/inject.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const API = 'https://api.github.com/repos/'
44
const LI_TAG_ID = 'github-repo-size'
55
const GITHUB_TOKEN_KEY = 'x-github-token'
66

7+
const storage = chrome.storage.sync || chrome.storage.local
8+
79
let githubToken
810

911
function isTree (uri) {
@@ -158,7 +160,7 @@ function checkForRepoPage () {
158160
}
159161
}
160162

161-
chrome.storage.local.get(GITHUB_TOKEN_KEY, function (data) {
163+
storage.get(GITHUB_TOKEN_KEY, function (data) {
162164
githubToken = data[GITHUB_TOKEN_KEY]
163165

164166
chrome.storage.onChanged.addListener(function (changes, namespace) {

0 commit comments

Comments
 (0)