-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
46 lines (41 loc) · 1.3 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// initial
const config = {
listenDomain: [],
syncURL: ''
}
// get config from chrome.storage
chrome.storage.sync.get(['listenDomain', 'syncURL'], ({ listenDomain = [], syncURL = '' }) => {
config.listenDomain = listenDomain
config.syncURL = syncURL
})
// 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: ${config.listenDomain.join(', ')}`,
})
})
// register cookies listener
chrome.cookies.onChanged.addListener(function(info) {
const {cause, cookie: {domain, name, path, value}, removed} = info
if(config.listenDomain.includes(domain) && removed === false && cause === 'explicit') {
chrome.cookies.set({
url: config.syncURL,
name,
value,
path,
}, msg => console.log('sync success:', msg))
}
})