mirror of
https://github.com/zero-peak/ZeroOmega.git
synced 2025-01-22 15:08:12 -05:00
Fetch options from SwitchySharp if supported.
This commit is contained in:
parent
c30b3cf8d7
commit
f890993ad6
@ -98,6 +98,9 @@ actionForUrl = (url) ->
|
||||
result.condition.conditionType)
|
||||
details += "#{condition} => #{dispName(result.profileName)}\n"
|
||||
|
||||
if not details
|
||||
details = options.printProfile(current)
|
||||
|
||||
icon =
|
||||
if profile.name == current.name and options.isCurrentProfileStatic()
|
||||
if direct
|
||||
@ -119,6 +122,8 @@ actionForUrl = (url) ->
|
||||
storage = new OmegaTargetCurrent.Storage(chrome.storage.local, 'local')
|
||||
state = new OmegaTargetCurrent.BrowserStorage(localStorage, 'omega.local.')
|
||||
options = new OmegaTargetCurrent.Options(null, storage, state, Log)
|
||||
options.switchySharp = new OmegaTargetCurrent.SwitchySharp()
|
||||
options.switchySharp.monitor()
|
||||
|
||||
tabs = new OmegaTargetCurrent.ChromeTabs(actionForUrl)
|
||||
tabs.watch()
|
||||
|
@ -1,7 +1,8 @@
|
||||
module.exports =
|
||||
Storage: require('./src/storage')
|
||||
Options: require('./src/options')
|
||||
ChromeTabs: require('./src/tabs.coffee')
|
||||
ChromeTabs: require('./src/tabs')
|
||||
SwitchySharp: require('./src/switchysharp')
|
||||
Url: require('url')
|
||||
|
||||
for name, value of require('omega-target')
|
||||
|
@ -20,7 +20,7 @@ class ChromeOptions extends OmegaTarget.Options
|
||||
xhr(dest_url).get(1)
|
||||
|
||||
updateProfile: (args...) ->
|
||||
super(args...).then (results) =>
|
||||
super(args...).then (results) ->
|
||||
error = false
|
||||
for own profileName, result of results
|
||||
if result instanceof Error
|
||||
@ -215,25 +215,33 @@ class ChromeOptions extends OmegaTarget.Options
|
||||
|
||||
upgrade: (options, changes) ->
|
||||
super(options).catch (err) =>
|
||||
if not options?['schemaVersion']
|
||||
if options?['config'] or localStorage['config']
|
||||
oldOptions = if options?['config'] then options else localStorage
|
||||
i18n = {
|
||||
upgrade_profile_auto: chrome.i18n.getMessage('upgrade_profile_auto')
|
||||
}
|
||||
try
|
||||
# Upgrade from SwitchySharp.
|
||||
upgraded = require('./upgrade')(oldOptions, i18n)
|
||||
catch ex
|
||||
OmegaTarget.Log.error(ex)
|
||||
if upgraded
|
||||
if localStorage['config']
|
||||
Object.getPrototypeOf(localStorage).clear.call(localStorage)
|
||||
return this && super(upgraded, upgraded)
|
||||
else
|
||||
return Promise.reject new Error('No options set.')
|
||||
return Promise.reject err if options?['schemaVersion']
|
||||
getOldOptions = if @switchySharp
|
||||
@switchySharp.getOptions().timeout(1000)
|
||||
else
|
||||
Promise.reject()
|
||||
|
||||
Promise.reject err
|
||||
getOldOptions = getOldOptions.catch ->
|
||||
if options?['config']
|
||||
Promise.resolve options
|
||||
else if localStorage['config']
|
||||
Promise.resolve localStorage
|
||||
else
|
||||
Promise.reject new Error('No options set.')
|
||||
|
||||
getOldOptions.then (oldOptions) =>
|
||||
i18n = {
|
||||
upgrade_profile_auto: chrome.i18n.getMessage('upgrade_profile_auto')
|
||||
}
|
||||
try
|
||||
# Upgrade from SwitchySharp.
|
||||
upgraded = require('./upgrade')(oldOptions, i18n)
|
||||
catch ex
|
||||
@log.error(ex)
|
||||
return Promise.reject ex
|
||||
if localStorage['config']
|
||||
Object.getPrototypeOf(localStorage).clear.call(localStorage)
|
||||
return this && super(upgraded, upgraded)
|
||||
|
||||
module.exports = ChromeOptions
|
||||
|
||||
|
56
omega-target-chromium-extension/src/switchysharp.coffee
Normal file
56
omega-target-chromium-extension/src/switchysharp.coffee
Normal file
@ -0,0 +1,56 @@
|
||||
OmegaTarget = require('omega-target')
|
||||
OmegaPac = OmegaTarget.OmegaPac
|
||||
Promise = OmegaTarget.Promise
|
||||
|
||||
module.exports = class SwitchySharp
|
||||
extId: 'oheiiidjlelbjilmglommidcfknfidpd'
|
||||
port: null
|
||||
|
||||
monitor: ->
|
||||
if not port? and not @_monitorTimerId
|
||||
@_monitorTimerId = setInterval @_monitorRun.bind(this), 5000
|
||||
@_monitorRun()
|
||||
|
||||
getOptions: ->
|
||||
if not @_getOptions
|
||||
@_getOptions = new Promise (resolve) =>
|
||||
@_getOptionsResolver = resolve
|
||||
@monitor()
|
||||
@_getOptions
|
||||
|
||||
_getOptions: null
|
||||
_getOptionsResolver: null
|
||||
_monitorTimerId: null
|
||||
|
||||
_onMessage: (msg) ->
|
||||
switch msg?.action
|
||||
when 'state'
|
||||
# State changed.
|
||||
OmegaTarget.Log.log(msg)
|
||||
when 'options'
|
||||
@_getOptionsResolver?(msg.options)
|
||||
|
||||
_onDisconnect: (msg) ->
|
||||
@port = null
|
||||
@_getOptions = null
|
||||
@_getOptionsResolver = null
|
||||
@monitor()
|
||||
|
||||
_monitorRun: ->
|
||||
if @_connect()
|
||||
if @_monitorTimerId
|
||||
clearInterval @_monitorTimerId
|
||||
@_monitorTimerId = null
|
||||
if @_getOptionsResolver
|
||||
@port.postMessage({action: 'getOptions'})
|
||||
|
||||
_connect: ->
|
||||
if not @port
|
||||
@port = chrome.runtime.connect(@extId)
|
||||
@port.onDisconnect.addListener(@_onDisconnect.bind(this))
|
||||
@port.onMessage.addListener(@_onMessage.bind(this))
|
||||
try
|
||||
@port.postMessage({action: 'disable'})
|
||||
catch
|
||||
@port = null
|
||||
return @port?
|
Loading…
Reference in New Issue
Block a user