diff --git a/background.js b/background.js index 12fd4aa..3edc103 100644 --- a/background.js +++ b/background.js @@ -2,51 +2,47 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -class Config { - constructor(config) { - this.config = config - } - - set(value) { - this.config = {...this.config, ...value} - } - - get(key) { - return key ? this.config[key] : this.config - } -} // initial -const config = new Config({ - listenDomain: [], - syncURL: '' -}) - -chrome.storage.sync.get(['listenDomain', 'syncURL'], value => config.set(value)) +const config = { + listenDomain: [], + syncURL: '' +} -// register listener -chrome.cookies.onChanged.addListener(function(info) { - const {cause, cookie: {domain, name, path, value}, removed} = info - if(config.get('listenDomain').includes(domain) && removed === false && cause === 'explicit') { - chrome.cookies.set({ - url: config.get('syncURL'), - name, - value, - path, - }, msg => console.log('sync success:', msg)) - } +// get config from chrome.storage +chrome.storage.sync.get(['listenDomain', 'syncURL'], ({ listenDomain = [], syncURL = '' }) => { + config.listenDomain = listenDomain + config.syncURL = syncURL }) -// msg handler -chrome.extension.onConnect.addListener(function(port) { - port.onMessage.addListener(function({ type, payload }) { - if (type === 'initial') { - config.set(payload) - chrome.notifications.create(null, { +// update config +chrome.storage.onChanged.addListener(function({ listenDomain, syncURL }) { + if (listenDomain) { + config.listenDomain = listenDomain.newValue + } + if (syncURL) { + config.syncURL = syncURL.newValue + } + + // notify + chrome.notifications.create(null, { type: 'basic', iconUrl: 'icon.png', title: 'Setting Successfully', - message: `Now Listening: ${payload.listenDomain.join(', ')}`, - }); + message: `Now Listening: ${config.listenDomain.join(', ')}`, + }) +}) + +console.log("listen") +// register cookies listener +chrome.cookies.onChanged.addListener(function(info) { + const {cause, cookie: {domain, name, path, value}, removed} = info + console.log(domain, config.listenDomain, "config.listenDomain") + if(config.listenDomain.includes(domain) && removed === false && cause === 'explicit') { + chrome.cookies.set({ + url: config.syncURL, + name, + value, + path, + }, msg => console.log('sync success:', msg)) } - }) }) diff --git a/manifest.json b/manifest.json index 56f1e33..7a175c6 100644 --- a/manifest.json +++ b/manifest.json @@ -1,8 +1,8 @@ { "name": "Auto Sync Cookie", "description": "listen and sync Cookie to target domain.", - "version": "0.1", - "permissions": ["tabs", "http://*/*", "https://*/*", "storage", "cookies", "notifications"], + "version": "0.2", + "permissions": ["storage", "cookies", "", "notifications"], "icons": { "16": "icon.png", "48": "icon.png", "128": "icon.png" }, "browser_action": { "default_title": "Listen & Sync Cookie to target domain.", diff --git a/popup.js b/popup.js index 15e2215..ead1dc4 100644 --- a/popup.js +++ b/popup.js @@ -2,28 +2,29 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -const port = chrome.extension.connect(); - -document.addEventListener('DOMContentLoaded', function() { +document.addEventListener('DOMContentLoaded', function(){ const ls = document.querySelector('#listen-domain') const sy = document.querySelector('#sync-url') const syncBtn = document.querySelector('#save-btn') - chrome.storage.sync.get(['listenDomain', 'syncURL'], ({ listenDomain, syncURL }) => { - ls.value = listenDomain.join('\n') || '' - sy.value = syncURL || '' - }) + // backfill config + chrome.storage.sync.get( + ['listenDomain', 'syncURL'], + ({ listenDomain = [], syncURL = '' }) => { + ls.value = listenDomain.join('\n') + sy.value = syncURL + } + ) + // set new config syncBtn.onclick = () => { const listenDomain = ls.value.trim().split(/\n/) const syncURL = sy.value.trim() if (listenDomain && syncURL) { chrome.storage.sync.set({ listenDomain, syncURL }) - port.postMessage({ - type: 'initial', - payload: { listenDomain, syncURL } - }) } } }) + +