mirror of
https://github.com/zero-peak/ZeroOmega.git
synced 2025-02-02 02:58:13 -05:00
add source code reference;fix gist sync not work on first init;refactor code;
This commit is contained in:
parent
fa41a54784
commit
b2bbc5fd60
@ -5,6 +5,14 @@ Promise.longStackTraces()
|
|||||||
OmegaTargetCurrent.Log = Object.create(OmegaTargetCurrent.Log)
|
OmegaTargetCurrent.Log = Object.create(OmegaTargetCurrent.Log)
|
||||||
Log = OmegaTargetCurrent.Log
|
Log = OmegaTargetCurrent.Log
|
||||||
|
|
||||||
|
# TODO 将来可能代码需要重构下,这里写得有点乱. (suziwen1@gmail.com)
|
||||||
|
globalThis.isBrowserRestart = globalThis.startupCheck is undefined
|
||||||
|
startupCheck = globalThis.startupCheck ?= -> true
|
||||||
|
|
||||||
|
chrome.runtime.onStartup.addListener ->
|
||||||
|
globalThis.isBrowserRestart = true
|
||||||
|
|
||||||
|
|
||||||
unhandledPromises = []
|
unhandledPromises = []
|
||||||
unhandledPromisesId = []
|
unhandledPromisesId = []
|
||||||
unhandledPromisesNextId = 1
|
unhandledPromisesNextId = 1
|
||||||
@ -163,9 +171,11 @@ if chrome?.storage?.sync or browser?.storage?.sync
|
|||||||
|
|
||||||
proxyImpl = OmegaTargetCurrent.proxy.getProxyImpl(Log)
|
proxyImpl = OmegaTargetCurrent.proxy.getProxyImpl(Log)
|
||||||
state.set({proxyImplFeatures: proxyImpl.features})
|
state.set({proxyImplFeatures: proxyImpl.features})
|
||||||
options = new OmegaTargetCurrent.Options(null, storage, state, Log, sync,
|
options = new OmegaTargetCurrent.Options(storage, state, Log, sync,
|
||||||
proxyImpl)
|
proxyImpl)
|
||||||
|
|
||||||
|
options.initWithOptions(null, startupCheck)
|
||||||
|
|
||||||
options.externalApi = new OmegaTargetCurrent.ExternalApi(options)
|
options.externalApi = new OmegaTargetCurrent.ExternalApi(options)
|
||||||
options.externalApi.listen()
|
options.externalApi.listen()
|
||||||
|
|
||||||
@ -322,7 +332,6 @@ resetAllOptions = ->
|
|||||||
chrome.storage.sync.clear(),
|
chrome.storage.sync.clear(),
|
||||||
chrome.storage.local.clear()
|
chrome.storage.local.clear()
|
||||||
])
|
])
|
||||||
|
|
||||||
chrome.runtime.onMessage.addListener (request, sender, respond) ->
|
chrome.runtime.onMessage.addListener (request, sender, respond) ->
|
||||||
return unless request and request.method
|
return unless request and request.method
|
||||||
options.ready.then ->
|
options.ready.then ->
|
||||||
|
@ -9,14 +9,8 @@ Url = require('url')
|
|||||||
|
|
||||||
TEMPPROFILEKEY = 'tempProfileState'
|
TEMPPROFILEKEY = 'tempProfileState'
|
||||||
|
|
||||||
globalThis.isBrowserRestart = false
|
|
||||||
|
|
||||||
chrome.runtime.onStartup.addListener ->
|
chrome.runtime.onStartup.addListener ->
|
||||||
globalThis.isBrowserRestart = true
|
idbKeyval.del(TEMPPROFILEKEY)
|
||||||
console.log('delete temp profile')
|
|
||||||
idbKeyval.del(TEMPPROFILEKEY).then(->
|
|
||||||
console.log('delete temp profile success')
|
|
||||||
)
|
|
||||||
|
|
||||||
class ChromeOptions extends OmegaTarget.Options
|
class ChromeOptions extends OmegaTarget.Options
|
||||||
_inspect: null
|
_inspect: null
|
||||||
@ -28,7 +22,6 @@ class ChromeOptions extends OmegaTarget.Options
|
|||||||
chrome.alarms.onAlarm.addListener (alarm) =>
|
chrome.alarms.onAlarm.addListener (alarm) =>
|
||||||
switch alarm.name
|
switch alarm.name
|
||||||
when 'omega.updateProfile'
|
when 'omega.updateProfile'
|
||||||
console.log('update profile interval')
|
|
||||||
@ready.then( =>
|
@ready.then( =>
|
||||||
@updateProfile()
|
@updateProfile()
|
||||||
)
|
)
|
||||||
@ -73,12 +66,10 @@ class ChromeOptions extends OmegaTarget.Options
|
|||||||
chrome.tabs.reload(tab.id)
|
chrome.tabs.reload(tab.id)
|
||||||
)
|
)
|
||||||
|
|
||||||
init: ->
|
init: (startupCheck) ->
|
||||||
super()
|
super(startupCheck)
|
||||||
@ready.then =>
|
@ready.then =>
|
||||||
console.log('get temp profile')
|
|
||||||
idbKeyval.get(TEMPPROFILEKEY).then (tempProfileState) =>
|
idbKeyval.get(TEMPPROFILEKEY).then (tempProfileState) =>
|
||||||
console.log('init temp profile:', tempProfileState)
|
|
||||||
# tempProfileState =
|
# tempProfileState =
|
||||||
# { _tempProfile,
|
# { _tempProfile,
|
||||||
# _tempProfileActive}
|
# _tempProfileActive}
|
||||||
|
@ -8,7 +8,8 @@ isPushing = false
|
|||||||
|
|
||||||
state = null
|
state = null
|
||||||
|
|
||||||
optionFilename = 'ZeroOmega.json'
|
mainLetters = ['Z','e', 'r', 'o', 'O', 'm','e', 'g', 'a']
|
||||||
|
optionFilename = mainLetters.concat(['.json']).join('')
|
||||||
gistId = ''
|
gistId = ''
|
||||||
gistToken = ''
|
gistToken = ''
|
||||||
gistHost = 'https://api.github.com'
|
gistHost = 'https://api.github.com'
|
||||||
@ -135,7 +136,7 @@ getGist = (gistId) ->
|
|||||||
|
|
||||||
updateGist = (gistId, options) ->
|
updateGist = (gistId, options) ->
|
||||||
postBody = {
|
postBody = {
|
||||||
description: 'ZeroOmega Sync'
|
description: mainLetters.concat([' Sync']).join('')
|
||||||
files: {}
|
files: {}
|
||||||
}
|
}
|
||||||
postBody.files[optionFilename] = {
|
postBody.files[optionFilename] = {
|
||||||
@ -275,6 +276,8 @@ class ChromeSyncStorage extends OmegaTarget.Storage
|
|||||||
return Promise.resolve({})
|
return Promise.resolve({})
|
||||||
Promise.resolve(@storage.remove(keys))
|
Promise.resolve(@storage.remove(keys))
|
||||||
.catch(ChromeSyncStorage.parseStorageErrors)
|
.catch(ChromeSyncStorage.parseStorageErrors)
|
||||||
|
destroy: ->
|
||||||
|
idbKeyval.clear(@syncStore)
|
||||||
flush: ({data}) ->
|
flush: ({data}) ->
|
||||||
entries = []
|
entries = []
|
||||||
result = null
|
result = null
|
||||||
|
@ -56,20 +56,26 @@ class Options
|
|||||||
value = profile
|
value = profile
|
||||||
return value
|
return value
|
||||||
|
|
||||||
constructor: (options, @_storage, @_state, @log, @sync, @proxyImpl) ->
|
constructor: (@_storage, @_state, @log, @sync, @proxyImpl) ->
|
||||||
@_options = {}
|
@_options = {}
|
||||||
@_tempProfileRules = {}
|
@_tempProfileRules = {}
|
||||||
@_tempProfileRulesByProfile = {}
|
@_tempProfileRulesByProfile = {}
|
||||||
@_storage ?= Storage()
|
@_storage ?= Storage()
|
||||||
@_state ?= Storage()
|
@_state ?= Storage()
|
||||||
@log ?= Log
|
@log ?= Log
|
||||||
|
|
||||||
|
|
||||||
|
###
|
||||||
|
# 拆开比较合理些
|
||||||
|
###
|
||||||
|
initWithOptions: (options, startupCheck) ->
|
||||||
if not options?
|
if not options?
|
||||||
@init()
|
@init(startupCheck)
|
||||||
else
|
else
|
||||||
@ready = @_storage.remove().then(=>
|
@ready = @_storage.remove().then(=>
|
||||||
@_storage.set(options)
|
@_storage.set(options)
|
||||||
).then =>
|
).then =>
|
||||||
@init()
|
@init(startupCheck)
|
||||||
|
|
||||||
###*
|
###*
|
||||||
# Attempt to load options from local and remote storage.
|
# Attempt to load options from local and remote storage.
|
||||||
@ -174,9 +180,15 @@ class Options
|
|||||||
# Attempt to initialize (or reinitialize) options.
|
# Attempt to initialize (or reinitialize) options.
|
||||||
# @returns {Promise<OmegaOptions>} A promise that is fulfilled on ready.
|
# @returns {Promise<OmegaOptions>} A promise that is fulfilled on ready.
|
||||||
###
|
###
|
||||||
init: ->
|
init: (startupCheck = -> true) ->
|
||||||
|
# startupCheck 一定要放在 isBrowserRestart 后面
|
||||||
|
# TODO (suziwen1@gmail.com)
|
||||||
|
# 1. 好像有 bug , 一直没法重现,但就是很不经意就能出现,概率很小的样子
|
||||||
|
# 2. 有全局变量,容易污染代码,需要重构初始化流程
|
||||||
@ready = @loadOptions().then(=>
|
@ready = @loadOptions().then(=>
|
||||||
if globalThis.isBrowserRestart and @_options['-startupProfileName']
|
if globalThis.isBrowserRestart and
|
||||||
|
startupCheck() and
|
||||||
|
@_options['-startupProfileName']
|
||||||
@applyProfile(@_options['-startupProfileName'])
|
@applyProfile(@_options['-startupProfileName'])
|
||||||
else
|
else
|
||||||
@_state.get({
|
@_state.get({
|
||||||
@ -1033,6 +1045,7 @@ class Options
|
|||||||
@sync.enabled = false
|
@sync.enabled = false
|
||||||
@_syncWatchStop?()
|
@_syncWatchStop?()
|
||||||
@_syncWatchStop = null
|
@_syncWatchStop = null
|
||||||
|
@sync.destroy()
|
||||||
return
|
return
|
||||||
|
|
||||||
if syncOptions == 'conflict'
|
if syncOptions == 'conflict'
|
||||||
@ -1059,7 +1072,7 @@ class Options
|
|||||||
@sync.enabled = true
|
@sync.enabled = true
|
||||||
@init()
|
@init()
|
||||||
else
|
else
|
||||||
if remoteOptions.schemaVersion
|
if remoteOptions?.schemaVersion
|
||||||
@sync.flush({data: remoteOptions}).then( =>
|
@sync.flush({data: remoteOptions}).then( =>
|
||||||
@sync.enabled = false
|
@sync.enabled = false
|
||||||
@_state.set({'syncOptions': 'conflict'})
|
@_state.set({'syncOptions': 'conflict'})
|
||||||
|
@ -220,6 +220,8 @@ class OptionsSync
|
|||||||
})
|
})
|
||||||
init: (args) ->
|
init: (args) ->
|
||||||
@storage.init(args)
|
@storage.init(args)
|
||||||
|
destroy: ->
|
||||||
|
@storage.destroy()
|
||||||
flush: ({data}) ->
|
flush: ({data}) ->
|
||||||
@storage.flush({data})
|
@storage.flush({data})
|
||||||
|
|
||||||
|
5
omega-web/lib/csso.js
vendored
5
omega-web/lib/csso.js
vendored
File diff suppressed because one or more lines are too long
6
omega-web/lib/idb-keyval.js
vendored
6
omega-web/lib/idb-keyval.js
vendored
File diff suppressed because one or more lines are too long
4
omega-web/lib/moment-with-locales.js
vendored
4
omega-web/lib/moment-with-locales.js
vendored
@ -1,3 +1,7 @@
|
|||||||
|
/**
|
||||||
|
* https://github.com/moment/moment/
|
||||||
|
* 源地址
|
||||||
|
**/
|
||||||
;(function (global, factory) {
|
;(function (global, factory) {
|
||||||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
||||||
typeof define === 'function' && define.amd ? define(factory) :
|
typeof define === 'function' && define.amd ? define(factory) :
|
||||||
|
Loading…
Reference in New Issue
Block a user