-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
142 lines (122 loc) · 3.42 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
class RequestTokenExtractor
{
constructor()
{
this.cacheSiteOpened = new Map();
this.registerListeners();
}
registerListeners()
{
chrome.webRequest.onSendHeaders.addListener(
this.handleSendHeaders.bind(this),
{
urls: ['https://piba-api.myvisit.com/*'],
types: ['main_frame', 'sub_frame', 'xmlhttprequest'],
},
['requestHeaders'],
);
chrome.webRequest.onCompleted.addListener(
this.handleSiteOpened.bind(this),
{
urls: ['https://piba.myvisit.com/*', 'https://myvisit.com/*'],
types: ['main_frame'],
}
);
chrome.action.onClicked.addListener((tab) => {
chrome.runtime.openOptionsPage(() => {});
});
chrome.runtime.onInstalled.addListener(details => {
if (details.reason === chrome.runtime.OnInstalledReason.INSTALL) {
chrome.runtime.openOptionsPage(() => {
console.log('Installed. Options page opened');
});
}
if (details.reason === chrome.runtime.OnInstalledReason.UPDATE) {
chrome.storage.sync.get('personalToken').then((data) => {
if (!data.personalToken) {
chrome.runtime.openOptionsPage(() => {
console.log('Updated. Options page opened');
});
}
});
// chrome.runtime.setUninstallURL('https://example.com/extension-survey');
}
});
}
injectScript(tabId, successCallback) {
chrome.scripting.executeScript({
target: {tabId: tabId},
files: ['injected-by-background.js'],
}, () => {
console.log('Try to inject injected-by-background.js');
successCallback();
});
}
handleSiteOpened(details) {
const tabId = details.tabId;
console.log('handleSiteOpened', details.url, details.statusCode, tabId);
if (details.statusCode !== 200) {
return;
}
this.cacheSiteOpened.set(tabId, details.url);
this.injectScript(tabId, () => {
chrome.tabs.sendMessage(details.tabId, {
action: 'site-opened',
url: details.url,
}, response => {
console.log('site-opened response', response, chrome.runtime.lastError);
});
})
}
handleSendHeaders(details)
{
const config = this.getConfig(details.requestHeaders);
if (config['application-api-key'])
{
console.log('requestHeaders: ', config);
const message = {
action: 'sync-config',
config: config,
};
chrome.tabs.sendMessage(details.tabId, message, response => {
console.log('sync-config', response, chrome.runtime.lastError);
if (!response && chrome.runtime.lastError) {
console.log('RETRY to inject injected-by-background.js');
this.injectScript(details.tabId, () => {
const openedUrlInTab = this.cacheSiteOpened.get(details.tabId);
if (openedUrlInTab) {
chrome.tabs.sendMessage(details.tabId, {
action: 'site-opened',
url: openedUrlInTab,
}, response => {
console.log('RETRY site-opened', response, chrome.runtime.lastError);
});
}
chrome.tabs.sendMessage(details.tabId, message, response => {
console.log('RETRY sync-config', response, chrome.runtime.lastError);
});
});
}
});
}
}
getConfig(headers)
{
const importantHeaders = {
'preparedvisittoken': null,
'application-api-key': null,
'application-name': null,
'user-agent': null,
};
for (const header of headers)
{
const name = header.name ? header.name.toLowerCase() : null;
if (name && importantHeaders.hasOwnProperty(name))
{
importantHeaders[name] = header.value;
}
}
return importantHeaders;
}
}
new RequestTokenExtractor();