-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
128 lines (115 loc) · 5.21 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
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
let
CI_KEYWORDS = [],
CS_KEYWORDS = [],
DOMAINS = [],
DIMMED_ENTRIES = [];
// Function to dim titles, reduce font size, add a dimming link, and persist dimmed state
function adjustTitlesAndPersistDimming() {
// Function to apply dimming effect
const applyDimmingEffect = (titleCells, subtext, entryId, spacingRowPrev, spacingRowNext, tdRank, tdVoteLinks, isDimming, doSave) => {
const opacity = isDimming ? "0.35" : "1"; // Dimmed or original opacity
const fontSize = isDimming ? "60%" : ""; // Dimmed or original font size
const imgSize = isDimming ? "7" : "12"; // Dimmed or original image size
const spacingHeightPrev = isDimming ? "4px" : "10px"; // Dimmed or original spacing
const spacingHeightNext = isDimming ? "6px" : "10px"; // Dimmed or original spacing
const voteArrowsSize = isDimming ? "7px" : "10px";
titleCells.forEach(cell => {
cell.style.opacity = opacity;
cell.style.fontSize = fontSize;
let img = cell.querySelector("img");
if (img) {
img.width = imgSize;
img.height = imgSize;
}
});
if (subtext) {
subtext.style.opacity = opacity;
subtext.style.fontSize = fontSize;
}
if (spacingRowPrev) {
spacingRowPrev.style.setProperty("height", spacingHeightPrev, "important");
}
if (spacingRowNext) {
spacingRowNext.style.setProperty("height", spacingHeightNext, "important");
}
if (tdRank) {
tdRank.removeAttribute("valign");
}
if (tdVoteLinks) {
let votearrows = tdVoteLinks.querySelectorAll(".votearrow");
votearrows.forEach(div => {
div.style.width = voteArrowsSize;
div.style.height = voteArrowsSize;
});
}
if (doSave) {
// Update the stored list of dimmed entries
const index = DIMMED_ENTRIES.indexOf(entryId);
if (isDimming && index === -1) {
DIMMED_ENTRIES.push(entryId);
} else if (!isDimming && index !== -1) {
DIMMED_ENTRIES.splice(index, 1);
}
chrome.storage.sync.set({ dimmedEntries: DIMMED_ENTRIES });
}
};
// Get all title rows on Hacker News homepage
const titleRows = document.querySelectorAll(".athing");
// Iterate over each title row
titleRows.forEach(row => {
const entryId = row.getAttribute("id"); // Get the entry"s unique ID
const titleLink = row.querySelector(".titleline > a");
const siteLink = row.querySelector(".sitestr");
const titleCells = row.querySelectorAll("td.title");
let subtextRow = row.nextElementSibling;
let subtext = subtextRow ? subtextRow.querySelector("td.subtext") : null;
let tdRank = row.querySelector("td:first-child");
let tdVoteLinks = row.querySelector("td:nth-child(2)");
let spacingRowPrev = row.previousElementSibling;
let spacingRowNext = subtextRow.nextElementSibling;
function escapeForRegex(string) {
return string.replace(/[/\-\\^$*+?.()|[\]{}]/g, "\\$&");
}
// Check if the entry should be initially dimmed
function checkTitle(title, site) {
return (
(site && DOMAINS.some(s => site.innerText.startsWith(s))) ||
CS_KEYWORDS.some(kw => (new RegExp(`(^|\\W)${escapeForRegex(kw)}(\\W|$)`)).test(title.innerText)) ||
CI_KEYWORDS.some(kw => (new RegExp(`(^|\\W)${escapeForRegex(kw)}(\\W|$)`)).test(title.innerText.toLowerCase()))
);
}
const isInitiallyDimmed = checkTitle(titleLink, siteLink) || DIMMED_ENTRIES.includes(entryId);
if (isInitiallyDimmed) {
applyDimmingEffect(titleCells, subtext, entryId, spacingRowPrev, spacingRowNext, tdRank, tdVoteLinks, true, false);
}
// Add or update the "dim/undim" link in subtext
if (subtext) {
// Locate or create the dim link
let dimLink = subtext.querySelector(".dimLink");
if (!dimLink) {
dimLink = document.createElement("a");
dimLink.href = "#";
dimLink.className = "dimLink"; // Add a class for easy identification
subtext.appendChild(document.createTextNode(" | "));
subtext.appendChild(dimLink);
}
dimLink.innerText = isInitiallyDimmed ? "undim" : "dim";
dimLink.onclick = (event) => {
event.preventDefault();
const isDimming = dimLink.innerText === "dim";
applyDimmingEffect(titleCells, subtext, entryId, spacingRowPrev, spacingRowNext, tdRank, tdVoteLinks, isDimming, true);
dimLink.innerText = isDimming ? "undim" : "dim"; // Toggle the link text
};
}
});
}
chrome.storage.sync.get(
{ ciKeywords: [], csKeywords: [], domains: [], dimmedEntries: [] },
(items) => {
CI_KEYWORDS = items.ciKeywords;
CS_KEYWORDS = items.csKeywords;
DOMAINS = items.domains;
DIMMED_ENTRIES = items.dimmedEntries;
adjustTitlesAndPersistDimming();
}
);