Skip to content

Commit

Permalink
1.3.0: save to gdrive
Browse files Browse the repository at this point in the history
  • Loading branch information
cnwangjie committed Aug 31, 2018
1 parent 312f1f3 commit 7eafd10
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 33 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
### v1.3.0 8/XX/2018
### v1.3.0 8/31/2018

- feat: better onetab sync server (it is provided by a self-hosted server, with unlimited storage space, allow authorize with google / github account now) !IMPORTANT
- feat: save list to google drive (we will allow user access the saved file and import manually currently) !IMPORTANT
- change: some icons changed
- change: add a gitter link
- change: add a gitter link (you can get communicate with the developer instantly)
- feat: allow set list color just need to click the tabs label
- change: new logo

Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<p align="center">
<img src="./src/assets/icons/icon_128.png">
<img src="https://user-images.githubusercontent.com/36993664/44917039-f208ad80-ad3f-11e8-85e9-e29489f0ffb4.png">
</p>

<h1 align="center">better-onetab</h1>

<p align="center">
<a href="https://circleci.com/gh/cnwangjie/better-onetab"><img src="https://img.shields.io/circleci/project/github/cnwangjie/better-onetab/master.svg?style=flat-square" alt="CircleCI"></a>
<a href="https://chrome.google.com/webstore/detail/better-onetab/eookhngofldnbnidjlbkeecljkfpmfpg"><img src="https://img.shields.io/chrome-web-store/v/eookhngofldnbnidjlbkeecljkfpmfpg.svg?style=flat-square" alt="Chrome Web Store"></a>
Expand Down
4 changes: 1 addition & 3 deletions README.zh.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<p align="center">
<img src="./src/assets/icons/icon_128.png">
<img src="https://user-images.githubusercontent.com/36993664/44917039-f208ad80-ad3f-11e8-85e9-e29489f0ffb4.png">
</p>

<h1 align="center">better-onetab</h1>

<p align="center">
<a href="https://circleci.com/gh/cnwangjie/better-onetab"><img src="https://img.shields.io/circleci/project/github/cnwangjie/better-onetab/master.svg?style=flat-square" alt="CircleCI"></a>
<a href="https://chrome.google.com/webstore/detail/better-onetab/eookhngofldnbnidjlbkeecljkfpmfpg"><img src="https://img.shields.io/chrome-web-store/v/eookhngofldnbnidjlbkeecljkfpmfpg.svg?style=flat-square" alt="Chrome Web Store"></a>
Expand Down
6 changes: 6 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -295,5 +295,11 @@
},
"ui_beta_warn": {
"message": "This is a feature in the test, if you find any problem please send me a feedback."
},
"ui_save_to_gdrive": {
"message": "Save list to google drive"
},
"ui_save_immediately": {
"message": "save immediately"
}
}
6 changes: 6 additions & 0 deletions src/_locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -286,5 +286,11 @@
},
"ui_beta_warn": {
"message": "这是一个测试中的功能,如果遇到了任何问题请向我反馈。"
},
"ui_save_to_gdrive": {
"message": "将列表保存至 google drive"
},
"ui_save_immediately": {
"message": "立即保存"
}
}
4 changes: 3 additions & 1 deletion src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ const init = async () => {
}
}
if (msg.uploadImmediate) {
boss.uploadImmediate()
boss.uploadImmediate().catch(() => {
browser.runtime.sendMessage({uploaded: {error: true}})
})
}
if (msg.forceUpdate) {
boss.forceUpdate(msg.forceUpdate)
Expand Down
11 changes: 2 additions & 9 deletions src/common/service/boss.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,13 @@ const setLists = lists => fetchData('/api/lists', 'PUT', {lists})
const getOpts = () => fetchData('/api/opts')
const setOpts = opts => fetchData('/api/opts', 'PUT', {opts})

const getRemoteInfo = () => {
return getInfo().catch(err => {
console.error('[boss]: first time to get info error:', err)
return getInfo()
})
}

const forceDownloadRemoteImmediate = async () => {
if (!await hasToken()) return
const {conflict} = await browser.storage.local.get('conflict')
if (conflict) return browser.runtime.sendMessage({downloaded: {conflict}})
const localInfo = await browser.storage.local.get(['listsUpdatedAt', 'optsUpdatedAt'])
const {listsUpdatedAt, optsUpdatedAt} = _.defaults(localInfo, {listsUpdatedAt: 0, optsUpdatedAt: 0})
const info = await getRemoteInfo()
const info = await getInfo()
const works = []
if (Date.parse(info.listsUpdatedAt) > listsUpdatedAt) {
works.push(async () => {
Expand Down Expand Up @@ -151,7 +144,7 @@ const forceUpdate = async ({lists, opts}) => {
const uploadImmediate = async () => {
const localInfo = await browser.storage.local.get(['listsUpdatedAt', 'optsUpdatedAt'])
const {listsUpdatedAt, optsUpdatedAt} = _.defaults(localInfo, {listsUpdatedAt: 0, optsUpdatedAt: 0})
const info = await getRemoteInfo()
const info = await getInfo()
const todo = {}
const conflict = (await browser.storage.local.get('conflict')).conflict || {}
if (Date.parse(info.listsUpdatedAt) === listsUpdatedAt) {
Expand Down
35 changes: 29 additions & 6 deletions src/common/service/gdrive.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import _ from 'lodash'
import browser from 'webextension-polyfill'

const GOOGLE_ACCESS_TOKEN_KEY = 'at'
const STORAGE_FOLDER_NAME = 'better-onetab-storage'
const STORAGE_FOLDER_ID_KEY = 'sfid'

const clearTokenWhenExpired = _.debounce(() => {
localStorage[GOOGLE_ACCESS_TOKEN_KEY] = null
}, 60 * 1000)
const clearToken = () => {
delete localStorage[GOOGLE_ACCESS_TOKEN_KEY]
}

const clearTokenWhenExpired = _.debounce(clearToken, 60 * 1000)

const getAuth = async () => {
if (localStorage[GOOGLE_ACCESS_TOKEN_KEY])
return localStorage[GOOGLE_ACCESS_TOKEN_KEY]

console.debug('[gdrive]: getting token')
const token = await new Promise((resolve, reject) => {
chrome.identity.getAuthToken({ 'interactive': true }, token => {
const err = chrome.runtime.lastError
Expand All @@ -20,11 +24,13 @@ const getAuth = async () => {
})
})
localStorage[GOOGLE_ACCESS_TOKEN_KEY] = token
console.debug('[gdrive]: got token ' + token)
clearTokenWhenExpired()
return token
}

export const loadGapi = async () => {
console.debug('[gdrive]: loading gapi')
return new Promise(resolve => {
window.gapiLoaded = () => setTimeout(resolve, 0)
const po = document.createElement('script')
Expand Down Expand Up @@ -132,6 +138,15 @@ const createFile = async filename => {
return res.result
}

const getAllFileInStorageFolder = async () => {
const gapi = await prepareGapi()
const folder = await getStorageFolder()
const res = await gapi.client.drive.files.list({
q: `'${folder.id}' in parents`,
})
return res.result.files
}

const getFileInStorageFolder = async filename => {
const gapi = await prepareGapi()
const folder = await getStorageFolder()
Expand Down Expand Up @@ -171,14 +186,24 @@ const forceSaveFile = async (data, filename) => {
})
}

const saveCurrentTabLists = async () => {
const {lists} = await browser.storage.local.get('lists')
if (_.isEmpty(lists)) return
const filename = 'list_save_' + new Date().toISOString() + '.json'
return forceSaveFile(lists, filename)
}

const gdrive = {
getAuth,
createFile,
uploadJSON,
forceSaveFile,
getAllFileInStorageFolder,
saveCurrentTabLists,
clearToken,
}

window.gdrive = gdrive
if (DEBUG) window.gdrive = gdrive

export default gdrive

Expand Down Expand Up @@ -447,5 +472,3 @@ MediaUploader.prototype.buildUrl_ = function(id, params, baseUrl) {
}
return url;
};


2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "__MSG_ext_name__",
"version": "1.2.1",
"version": "1.3.0",
"default_locale": "en",
"description": "__MSG_ext_desc__",
"author": "WangJie <[email protected]>",
Expand Down
74 changes: 66 additions & 8 deletions src/page/SyncInfo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@


<v-alert
:value="syncError"
color="error"
icon="warning"
:value="alert.status"
:color="alert.status"
:icon="alert.status === 'success' ? 'check_circle' : 'warning'"
outline
transition="fade-transition"
>
SYNC ERROR {{ syncError && syncError.message ? syncError.message : '' }}
{{ alert && alert.msg ? alert.msg : '' }}
</v-alert>

<v-list>
Expand Down Expand Up @@ -118,6 +118,48 @@
</div>
</div>

<v-divider></v-divider>

<v-list>
<v-list-tile>
<v-list-tile-content>
<v-list-tile-title>
{{ __('ui_save_to_gdrive') }}
</v-list-tile-title>
</v-list-tile-content>
<v-list-tile-action>
<v-btn
depressed small color="success" @click="saveToGdrive"
:loading="saving">
{{ __('ui_save_immediately') }}
</v-btn>
</v-list-tile-action>
</v-list-tile>

<!-- <v-list-tile>
<v-list-tile-content>
<v-layout wrap style="width:100%">
<v-flex xs8>
<v-subheader>
Timed upload
</v-subheader>
</v-flex>
<v-flex xs4>
<v-select
class="select-amend"
:items="uploadTimerItems"
v-model="uploadTimerSelected"
label=""
item-text=""
item-value="value"
@change=""
></v-select>
</v-flex>
</v-layout>
</v-list-tile-content>
</v-list-tile> -->
</v-list>

</v-card-text>
</v-card>
</v-flex>
Expand All @@ -129,6 +171,7 @@
import __ from '@/common/i18n'
import {formatTime, one} from '@/common/utils'
import boss from '@/common/service/boss'
import gdrive from '@/common/service/gdrive'
import browser from 'webextension-polyfill'
if (DEBUG) window.boss = boss
Expand All @@ -140,7 +183,10 @@ export default {
hasToken: null,
conflict: null,
logging: false,
syncError: null,
alert: { status: '', msg: '' },
uploadTimerItems: ['never', 'daily', 'hourly', 'onchange'],
uploadTimerSelected: null,
saving: false,
}
},
created() {
Expand Down Expand Up @@ -200,10 +246,10 @@ export default {
try {
await boss.getToken(auth)
await this.afterFirstAuth()
this.syncError = null
this.alert = { status: 'success', msg: 'success' }
} catch (e) {
console.error(e)
this.syncError = e
this.alert = { status: 'error', msg: e.message }
} finally {
this.logging = false
}
Expand All @@ -214,7 +260,19 @@ export default {
this.bossinfo = {}
this.$emit('login')
},
saveToGdrive: one(async function () {
this.saving = true
try {
await gdrive.saveCurrentTabLists()
this.alert = { status: 'success', msg: 'success' }
} catch (e) {
console.error(e)
this.alert = { status: 'error', msg: e.result.error.message }
gdrive.clearToken()
} finally {
this.saving = false
}
}),
}
}
</script>

0 comments on commit 7eafd10

Please sign in to comment.