-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
61 lines (53 loc) · 1.92 KB
/
content.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
// content.js
if (!window.bqCostObserverInitiated) {
window.bqCostObserverInitiated = true;
function computeCost(size, unit) {
const TB_TO_BYTES = 1e12;
const GB_TO_BYTES = 1e9;
const MB_TO_BYTES = 1e6;
const KB_TO_BYTES = 1e3;
const COST_PER_TB = 6.25;
let bytes;
switch (unit) {
case 'KB':
bytes = size * KB_TO_BYTES;
break;
case 'MB':
bytes = size * MB_TO_BYTES;
break;
case 'GB':
bytes = size * GB_TO_BYTES;
break;
case 'TB':
bytes = size * TB_TO_BYTES;
break;
default:
return 0;
}
return (bytes / TB_TO_BYTES) * COST_PER_TB;
}
function processAndUpdateText(node) {
const regex = /This query will process ([\d.,]+) ([KBGMT]B) when run\./;
const match = node.nodeValue.match(regex);
if (match) {
const size = parseFloat(match[1].replace(',', ''));
const unit = match[2];
const cost = computeCost(size, unit);
node.nodeValue = `This query will process ${size} ${unit} when run (Estimated Cost: $${cost.toFixed(2)})`;
}
}
const bqCostObserver = new MutationObserver(mutations => {
for (const mutation of mutations) {
if (mutation.type === 'characterData') {
processAndUpdateText(mutation.target);
} else if (mutation.type === 'childList') {
for (const node of mutation.addedNodes) {
if (node.nodeType === 3 && node.nodeValue.includes("This query will process")) {
processAndUpdateText(node);
}
}
}
}
});
bqCostObserver.observe(document.body, {childList: true, subtree: true, characterData: true});
}