Fetch options from SwitchySharp if supported.

This commit is contained in:
FelisCatus 2014-11-12 23:34:27 +08:00
parent c30b3cf8d7
commit f890993ad6
4 changed files with 90 additions and 20 deletions

View File

@ -98,6 +98,9 @@ actionForUrl = (url) ->
result.condition.conditionType) result.condition.conditionType)
details += "#{condition} => #{dispName(result.profileName)}\n" details += "#{condition} => #{dispName(result.profileName)}\n"
if not details
details = options.printProfile(current)
icon = icon =
if profile.name == current.name and options.isCurrentProfileStatic() if profile.name == current.name and options.isCurrentProfileStatic()
if direct if direct
@ -119,6 +122,8 @@ actionForUrl = (url) ->
storage = new OmegaTargetCurrent.Storage(chrome.storage.local, 'local') storage = new OmegaTargetCurrent.Storage(chrome.storage.local, 'local')
state = new OmegaTargetCurrent.BrowserStorage(localStorage, 'omega.local.') state = new OmegaTargetCurrent.BrowserStorage(localStorage, 'omega.local.')
options = new OmegaTargetCurrent.Options(null, storage, state, Log) options = new OmegaTargetCurrent.Options(null, storage, state, Log)
options.switchySharp = new OmegaTargetCurrent.SwitchySharp()
options.switchySharp.monitor()
tabs = new OmegaTargetCurrent.ChromeTabs(actionForUrl) tabs = new OmegaTargetCurrent.ChromeTabs(actionForUrl)
tabs.watch() tabs.watch()

View File

@ -1,7 +1,8 @@
module.exports = module.exports =
Storage: require('./src/storage') Storage: require('./src/storage')
Options: require('./src/options') Options: require('./src/options')
ChromeTabs: require('./src/tabs.coffee') ChromeTabs: require('./src/tabs')
SwitchySharp: require('./src/switchysharp')
Url: require('url') Url: require('url')
for name, value of require('omega-target') for name, value of require('omega-target')

View File

@ -20,7 +20,7 @@ class ChromeOptions extends OmegaTarget.Options
xhr(dest_url).get(1) xhr(dest_url).get(1)
updateProfile: (args...) -> updateProfile: (args...) ->
super(args...).then (results) => super(args...).then (results) ->
error = false error = false
for own profileName, result of results for own profileName, result of results
if result instanceof Error if result instanceof Error
@ -215,25 +215,33 @@ class ChromeOptions extends OmegaTarget.Options
upgrade: (options, changes) -> upgrade: (options, changes) ->
super(options).catch (err) => super(options).catch (err) =>
if not options?['schemaVersion'] return Promise.reject err if options?['schemaVersion']
if options?['config'] or localStorage['config'] getOldOptions = if @switchySharp
oldOptions = if options?['config'] then options else localStorage @switchySharp.getOptions().timeout(1000)
i18n = { else
upgrade_profile_auto: chrome.i18n.getMessage('upgrade_profile_auto') Promise.reject()
}
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.')
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 module.exports = ChromeOptions

View 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?