-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoce-script.js
79 lines (69 loc) · 2.05 KB
/
oce-script.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
const REQUEST_URL =
'https://vv-agency-anthill-dam.veevavault.com/ui/binders/section';
const BINDER_ID_PARAM_NAME = 'documentId';
chrome.devtools.network.onRequestFinished.addListener((event) => {
if (event?.request?.url === REQUEST_URL) {
event.getContent((body) => {
const {
payload: { itemsInChunk },
} = JSON.parse(body);
const binderId = getBinderId(
event.request?.postData?.params,
itemsInChunk
);
const defaultResult = {
presentationJSON: {
id: withVersion(binderId),
nodes: [],
},
slideMap: {},
};
const data = itemsInChunk.reduce((acc, { docId, name }) => {
acc.slideMap[name] = {
id: docId,
sequenceName: name,
};
acc.presentationJSON.nodes.push({ id: withVersion(docId) });
return acc;
}, defaultResult);
hideLoading();
initControls(data);
});
}
});
function getBinderId(requestParams, chunks) {
return (
chunks?.[0]?.binderId ||
requestParams?.find(({ name }) => name === BINDER_ID_PARAM_NAME)?.value ||
'NOT_FOUND'
);
}
function withVersion(value) {
return `${value}_0_1`;
}
function initControls(data) {
const controls = document.getElementById('controls');
controls.classList.remove('hidden');
controls.addEventListener('click', (event) => {
const handlersMap = {
slideMapBtn: ({ slideMap }) => copyToClipboard(JSON.stringify(slideMap)),
presentationJsonBtn: ({ presentationJSON }) => copyToClipboard(JSON.stringify(presentationJSON)),
};
if (!handlersMap?.[event.target.id]) {
console.log('Handler not found!!!');
return;
}
handlersMap[event.target.id](data);
});
}
function copyToClipboard(value) {
const textarea = document.createElement('textarea');
textarea.textContent = value;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
}
function hideLoading() {
document.getElementById('loader').classList.add('hidden');
}