-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactions.js
More file actions
91 lines (79 loc) · 2.42 KB
/
actions.js
File metadata and controls
91 lines (79 loc) · 2.42 KB
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
export const ADD_ACTIVE_TAB = 'ADD_ACTIVE_TAB';
export const ADD_REQUEST = 'ADD_REQUEST';
export const SET_REQUEST_COMPLETE = 'SET_REQUEST_COMPLETE';
export const ADD_SCRAPED_DATA = 'ADD_SCRAPED_DATA';
export const RESET_TAB_DATA = "RESET_TAB_DATA";
const isCloudinaryByResponseHeader = (headers) => {
let isCloudinary = false;
headers.forEach((hd) => {
const headerName = hd.name.toLowerCase();
const headerValue = hd.value.toLowerCase();
if(headerName === "server" && headerValue === "cloudinary") {
isCloudinary = true;
}
});
return isCloudinary;
}
export const addTab = (tabId) => {
return {
type: ADD_ACTIVE_TAB,
tabId: tabId || chrome.tabs.TAB_ID_NONE
}
};
export const resetTab = (tabId) => {
return {
type: RESET_TAB_DATA,
tabId,
};
};
export const addRequest = (request, error = false) => {
const { tabId = chrome.tabs.TAB_ID_NONE, requestId, url, timeStamp } = request;
let tips = [];
if (!url.includes('f_auto')) {
tips.push('Image format selection - consider using automatic format selection f_auto')
}
if (!url.includes('q_auto')) {
tips.push('Fixed quality used, consider replacing with automatic quality selection q_auto')
}
if (url.includes('f_gif') || url.includes('.gif')) {
tips.push('Convert animated GIF to MP4')
}
if (url.includes('c_fill') && (!url.includes('g_auto') && !url.includes('g_face') && !url.includes('g_faces') )) {
tips.push('Crop without content-aware gravity, consider using g_auto')
}
let warnings = []
if (!url.includes('q_')) {
warnings.push('Image quality not set, add q_auto')
}
return {
type: ADD_REQUEST,
tabId: tabId,
request: {
requestId,
url,
startTime: timeStamp,
status: 'pending',
tips,
warnings,
error
}
}
}
export const addScrapedData = (data, tabId) => {
return {
type: ADD_SCRAPED_DATA,
tabId,
data
}
}
export const setRequestComplete = (request) => {
const { tabId = chrome.tabs.TAB_ID_NONE, timeStamp, requestId, responseHeaders } = request;
return {
...addRequest(request),
type: SET_REQUEST_COMPLETE,
tabId: tabId,
requestId,
timeStamp,
isCloudinary: isCloudinaryByResponseHeader(responseHeaders)
};
}