This repository has been archived by the owner on Apr 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
96 lines (82 loc) · 2.55 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var defaults = {
cookie: 'SACSID',
endpoint: 'https://localhost:8081/_dev-server/propagate-cookie',
watchChanges: false
};
var sendData = function(endpointUri, data, callback) {
var xhr = new XMLHttpRequest();
xhr.open('POST', endpointUri, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
callback();
} else {
callback(new Error('Rerver respond with non-success status: ', xhr.status));
}
}
}
xhr.send(JSON.stringify(data));
};
var main = function() {
var settings = defaults;
var updateSettings = function() {
chrome.storage.sync.get(defaults, function(_settings) {
settings = _settings;
});
};
chrome.storage.onChanged.addListener(function(changes, area) {
if (area === 'sync') {
setTimeout(updateSettings);
}
});
chrome.runtime.onMessage.addListener(function(message) {
var domain = message.payload.domain;
var protocol = message.payload.protocol;
if (message.type === 'GET_TAB_DOMAIN') {
chrome.cookies.getAll({
domain: domain,
name: settings.cookie
}, function(cookies) {
var cookie = null;
if (cookies.length) {
cookie = cookies[0];
sendData(settings.endpoint, {
host: protocol + '//' + domain,
domain: domain,
cookie: cookie.name + '=' + cookie.value
}, function(err) {
if (err) {
chrome.notifications.create({
type: 'basic',
iconUrl: 'img/warning.png',
title: '[Propagate Cookie] Request server error',
message: 'Error occured while access the endpoint ' +
err.toString() + '.'
});
console.error(err);
}
});
} else {
chrome.notifications.create({
type: 'basic',
iconUrl: 'img/warning.png',
title: '[Propagete Cookie] Can\'t find cookie',
message: 'Can\'t find cookie with name `' + settings.cookie + '`' +
'Please check the extension settings in chrome dev tools.'
});
}
});
}
});
updateSettings();
chrome.browserAction.onClicked.addListener(function() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var tab = tabs[0];
chrome.tabs.executeScript(tab.id, {
file: 'commands/get_domain.js'
});
});
});
};
main();