-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontent.js
More file actions
129 lines (110 loc) · 3.4 KB
/
Copy pathcontent.js
File metadata and controls
129 lines (110 loc) · 3.4 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
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
let isEnabled = true;
let observer = null;
function convertToDecimal(probability) {
const decimal = probability / 100;
if (decimal <= 0) return "N/A";
// Calculate decimal odds: 1 / probability
return (1 / decimal).toFixed(3);
}
function updatePrices() {
if (!isEnabled) return;
// Method 1: Handle spans with the specific class (for split text nodes)
const priceSpans = document.querySelectorAll('span[class*="bEWXcN"]');
priceSpans.forEach((span) => {
// Skip if we've already processed this span
if (span.textContent.includes("(")) return;
// Match the full price including the ¢ symbol
const match = span.textContent.match(/(\d+\.?\d*)¢/);
if (!match) return;
const probability = parseFloat(match[1]);
const decimal = convertToDecimal(probability);
// Add decimal odds in parentheses before the original price
span.textContent = `(${decimal}) ${span.textContent}`;
});
// Method 2: Handle single text nodes containing the full price
const walker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_TEXT,
{
acceptNode: function (node) {
// Skip nodes that are inside the price spans we already processed
if (node.parentElement?.classList?.contains("bEWXcN"))
return NodeFilter.FILTER_REJECT;
return node.nodeValue.includes("¢")
? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_REJECT;
},
}
);
let node;
while ((node = walker.nextNode())) {
// Skip if we've already processed this node
if (node.nodeValue.includes("(")) continue;
// Extract numbers followed by ¢
const prices = node.nodeValue.match(/(\d+\.?\d*)¢/g);
if (!prices) continue;
let newText = node.nodeValue;
prices.forEach((price) => {
const probability = parseFloat(price);
const decimal = convertToDecimal(probability);
newText = newText.replace(price, `(${decimal}) ${price}`);
});
node.nodeValue = newText;
}
}
function removeDecimalOdds() {
// Method 1: Clean up spans with the specific class
const priceSpans = document.querySelectorAll('span[class*="bEWXcN"]');
priceSpans.forEach((span) => {
span.textContent = span.textContent.replace(/\(\d+\.\d+\) /g, "");
});
// Method 2: Clean up other text nodes
const walker = document.createTreeWalker(
document.body,
NodeFilter.SHOW_TEXT,
{
acceptNode: function (node) {
if (node.parentElement?.classList?.contains("bEWXcN"))
return NodeFilter.FILTER_REJECT;
return node.nodeValue.includes("¢")
? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_REJECT;
},
}
);
let node;
while ((node = walker.nextNode())) {
node.nodeValue = node.nodeValue.replace(/\(\d+\.\d+\) /g, "");
}
}
function setupObserver() {
if (observer) {
observer.disconnect();
}
observer = new MutationObserver(() => {
if (isEnabled) {
updatePrices();
}
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
}
// Listen for messages from background script
chrome.runtime.onMessage.addListener((message) => {
isEnabled = message.isEnabled;
if (isEnabled) {
updatePrices();
} else {
removeDecimalOdds();
}
});
// Check initial state
chrome.runtime.sendMessage({ action: "getState" }, (response) => {
isEnabled = response.isEnabled;
if (isEnabled) {
updatePrices();
}
setupObserver();
});