-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathrequestInterceptors.js
56 lines (46 loc) · 1.89 KB
/
requestInterceptors.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
import { pushRedirectChain, receiveInterceptedResponse } from 'store/request/actions';
const browser = window.browser || window.chrome;
/** We need to ignore fonts and resources not initiated by the user */
const blacklistedUrls = [
'https://fonts.gstatic.com/s/opensans/v14/PRmiXeptR36kaC0GEAetxvR_54zmj3SbGZQh3vCOwvY.woff',
'https://fonts.gstatic.com/s/opensans/v14/k3k702ZOKiLJc3WVjuplzKRDOzjiPcYnFooOUGCOsRk.woff',
'https://fonts.gstatic.com/s/opensans/v14/cJZKeOuBrn4kERxqtaUH3bO3LdcAZYWl9Si6vvxL-qU.woff',
'https://fonts.gstatic.com/s/opensans/v14/DXI1ORHCpsQm3Vp6mXoaTaRDOzjiPcYnFooOUGCOsRk.woff',
];
const beforeRedirectInterceptor = ({ getState, dispatch }) => response => {
const state = getState();
const lastRequestTime = state.request.lastRequestTime;
dispatch(pushRedirectChain({
...response,
time: response.timeStamp - lastRequestTime,
}));
};
const completedInterceptor = ({ getState, dispatch }) => response => {
// Ignore fonts and other stuff not initiated by the user
if ((response.originUrl && !response.originUrl.includes('dist/index.html'))
|| blacklistedUrls.includes(response.url)) {
return;
}
const state = getState();
const lastRequestTime = state.request.lastRequestTime;
dispatch(receiveInterceptedResponse({
...response,
time: response.timeStamp - lastRequestTime,
}));
};
// eslint-disable-next-line import/prefer-default-export
export const initializeInterceptors = store => {
// Use chrome.tabs without promises for cross-browser support
chrome.tabs.getCurrent(currentTab => {
browser.webRequest.onBeforeRedirect.addListener(
beforeRedirectInterceptor(store),
{ urls: ['<all_urls>'], tabId: currentTab.id },
['responseHeaders'],
);
browser.webRequest.onCompleted.addListener(
completedInterceptor(store),
{ urls: ['<all_urls>'], tabId: currentTab.id },
['responseHeaders'],
);
});
};