Skip to content

Commit

Permalink
- remove unused permission
Browse files Browse the repository at this point in the history
- change communication between components
  • Loading branch information
s0urcelab committed Jun 15, 2020
1 parent 7a4ce55 commit 04dfbc3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 52 deletions.
74 changes: 35 additions & 39 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
})
})
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -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", "<all_urls>", "notifications"],
"icons": { "16": "icon.png", "48": "icon.png", "128": "icon.png" },
"browser_action": {
"default_title": "Listen & Sync Cookie to target domain.",
Expand Down
23 changes: 12 additions & 11 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
})
}
}

})


0 comments on commit 04dfbc3

Please sign in to comment.