add source code reference;fix gist sync not work on first init;refactor code;

This commit is contained in:
suziwen 2024-08-12 12:32:50 +08:00
parent fa41a54784
commit b2bbc5fd60
8 changed files with 54 additions and 23 deletions

View File

@ -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 ->

View File

@ -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}

View File

@ -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

View File

@ -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'})

View File

@ -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})

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -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) :