Skip to content

Commit 4975693

Browse files
author
Mihir
committed
Change UI of affordable element
- Fix extension run on every browser update
1 parent 4809c88 commit 4975693

File tree

3 files changed

+102
-37
lines changed

3 files changed

+102
-37
lines changed

src/background.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ let defaultSettings = {
77
budget: ""
88
}
99

10-
chrome.runtime.onInstalled.addListener(setConfigs);
11-
12-
function setConfigs() {
10+
chrome.runtime.onInstalled.addListener((details) => {
1311
chrome.storage.sync.get("settings", ({ settings }) => {
1412
if (!settings) {
1513
chrome.storage.sync.set({ settings: defaultSettings });
1614
}
1715
});
1816

19-
chrome.runtime.openOptionsPage();
20-
}
17+
if (details && details.reason === 'install') {
18+
chrome.runtime.openOptionsPage();
19+
}
20+
});

src/main.css

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,58 @@
3838
#link:hover {
3939
color: white;
4040
}
41+
42+
/* Affordable inline badge */
43+
.affordable-badge {
44+
display: inline-flex !important;
45+
align-items: center !important;
46+
gap: 6px !important;
47+
padding: 2px 8px !important;
48+
margin-left: 6px !important;
49+
border-radius: 999px !important;
50+
font-size: 0.9em !important;
51+
line-height: 1 !important;
52+
background-color: #ffffff !important;
53+
border: 1px solid rgba(0, 0, 0, 0.2) !important;
54+
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.06) !important;
55+
}
56+
57+
.affordable-badge__swatch {
58+
width: 6px !important;
59+
height: 0.9em !important;
60+
border-radius: 2px !important;
61+
flex: 0 0 6px !important;
62+
background-color: #4caf50 !important; /* default */
63+
}
64+
65+
.affordable-badge__text {
66+
color: #111111 !important;
67+
font-weight: 600 !important;
68+
font-variant-numeric: tabular-nums !important;
69+
-webkit-font-smoothing: antialiased !important;
70+
-moz-osx-font-smoothing: grayscale !important;
71+
font-family: Inter, system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, Arial, sans-serif !important;
72+
white-space: nowrap !important;
73+
}
74+
75+
@media (prefers-color-scheme: dark) {
76+
.affordable-badge {
77+
background-color: #121212 !important;
78+
border: 1px solid rgba(255, 255, 255, 0.24) !important;
79+
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.4) !important;
80+
}
81+
.affordable-badge__text { color: #ffffff !important; }
82+
}
83+
84+
/* Discrete affordability tiers (0=most affordable green → 10=least affordable red) */
85+
.aff-tier-0 { background-color: #2e7d32 !important; }
86+
.aff-tier-1 { background-color: #388e3c !important; }
87+
.aff-tier-2 { background-color: #43a047 !important; }
88+
.aff-tier-3 { background-color: #4caf50 !important; }
89+
.aff-tier-4 { background-color: #7cb342 !important; }
90+
.aff-tier-5 { background-color: #fbc02d !important; }
91+
.aff-tier-6 { background-color: #f57c00 !important; }
92+
.aff-tier-7 { background-color: #ef6c00 !important; }
93+
.aff-tier-8 { background-color: #e53935 !important; }
94+
.aff-tier-9 { background-color: #d32f2f !important; }
95+
.aff-tier-10 { background-color: #b71c1c !important; }

src/main.js

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ function getFromStorageSync(itemName, callback) {
99
function getAppendContent(productPrice, salary, percentageMode) {
1010
if (percentageMode) {
1111
let percentage = getPriceInSalaryPercentage(productPrice, salary);
12-
return `${brackets.left}${percentage}%${brackets.right}`
12+
return `${percentage}%`
1313
}
1414
let timeTakenToEarn = getTimeTakenToEarn(productPrice, salary);
15-
return ` ${brackets.left}${timeTakenToEarn}${brackets.right}`
15+
return `${timeTakenToEarn}`
1616
}
1717

1818
function getPriceInSalaryPercentage(productPrice, salary) {
@@ -274,46 +274,56 @@ function append(elementInfo, element, productPrice, settings, isPriceRange) {
274274
return;
275275
}
276276

277-
let span = document.createElement("span");
278-
span.setAttribute('id', 'affordable');
279-
desiredElement.appendChild(span);
277+
// Build badge
278+
let badge = document.createElement("span");
279+
badge.setAttribute('id', 'affordable');
280+
badge.className = 'affordable-badge';
281+
282+
let swatch = document.createElement("span");
283+
swatch.className = 'affordable-badge__swatch';
284+
285+
if (settings.colourCodePrices) {
286+
const percent = computeAffordabilityPercent(productPrice, settings.salary);
287+
const tier = percentToTier(percent);
288+
swatch.classList.add(`aff-tier-${tier}`);
289+
}
290+
291+
let text = document.createElement("span");
292+
text.className = 'affordable-badge__text';
293+
text.innerText = getAppendContent(productPrice, settings.salary, settings.percentageMode);
294+
295+
badge.appendChild(swatch);
296+
badge.appendChild(text);
297+
desiredElement.appendChild(badge);
298+
280299
if (settings.hoverMode) {
281300
// Hover Mode Attributes
282-
span.style.display = 'none'
283-
if (settings.colourCodePrices) {
284-
addColourBasedOnPercentIntensity(span, getPriceInSalaryPercentage(productPrice, settings.salary) / 100)
285-
}
286-
span.innerText = " " + getAppendContent(productPrice, settings.salary, settings.percentageMode)
301+
badge.style.display = 'none';
287302
element.addEventListener('mouseover', function handleMouseOver() {
288-
span.style.display = 'block';
303+
badge.style.display = 'inline-flex';
289304
});
290305
desiredElement.setAttribute("title", `It will take you ${getTimeTakenToEarn(productPrice, settings.salary)} to earn ${productPrice}`);
291306
element.addEventListener('mouseout', function handleMouseOut() {
292-
span.style.display = 'none';
307+
badge.style.display = 'none';
293308
});
294-
295-
} else {
296-
// Normal Mode Attributes
297-
let innerSpan = document.createElement("span")
298-
if (settings.colourCodePrices) {
299-
addColourBasedOnPercentIntensity(innerSpan, getPriceInSalaryPercentage(productPrice, settings.salary) / 100)
300-
}
301-
innerSpan.innerText = getAppendContent(productPrice, settings.salary, settings.percentageMode);
302-
span.appendChild(innerSpan)
303-
if (!isPriceRange) {
304-
span.setAttribute("style", "display:block");
305-
desiredElement.setAttribute("title", `It will take you ${getTimeTakenToEarn(productPrice, settings.salary)} to earn ${productPrice}`);
306-
}
309+
} else if (!isPriceRange) {
310+
badge.setAttribute("style", "display:inline-flex");
311+
desiredElement.setAttribute("title", `It will take you ${getTimeTakenToEarn(productPrice, settings.salary)} to earn ${productPrice}`);
307312
}
308313
}
309314

310-
function addColourBasedOnPercentIntensity(element, percent) {
311-
//value from 0 to 1
312-
if (percent > 1)
313-
percent = 1;
314-
var hue = ((1 - percent) * 120).toString(10);
315-
let backgroundColor = ["hsl(", hue, ",100%,50%)"].join("");
316-
element.style.color = backgroundColor;
315+
function computeAffordabilityPercent(productPrice, salary) {
316+
let percent = productPrice / salary;
317+
if (percent < 0) percent = 0;
318+
if (percent > 1) percent = 1;
319+
return percent;
320+
}
321+
322+
function percentToTier(percent) {
323+
let p = percent;
324+
if (p < 0) p = 0;
325+
if (p > 1) p = 1;
326+
return Math.round(p * 10);
317327
}
318328

319329
function parseElementValue(elementValue) {

0 commit comments

Comments
 (0)