Repository navigation
Expand file tree
/
Copy pathcontent.js
More file actions
303 lines (260 loc) · 10.4 KB
/
Copy pathcontent.js
File metadata and controls
303 lines (260 loc) · 10.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// 'asetukset'
let highlightEnabled = true;
let hidePostsEnabled = true;
let shoutboxEnabled = true;
let postIds = [];
let fakePostIds = [];
// vierityspainikkeet
function createScrollButton(iconClass, onClick, label) {
const button = document.createElement("button");
button.className = "scroll-button";
button.innerHTML = `<span class="${iconClass}">${label}</span>`;
button.onclick = onClick;
return button;
}
function scrollToTop() {
window.scrollTo({ top: 0, behavior: "smooth" });
}
function scrollToBottom() {
window.scrollTo({ top: document.body.scrollHeight, behavior: "smooth" });
}
// Shoutboxin piilotus
function enableShoutbox() {
const elements = ['#shoutbox', '#shouts', '#shout-form'];
elements.forEach(selector => {
const element = document.querySelector(selector);
if (element) {
element.style.display = 'none';
}
});
console.log('Shoutbox piilotettu');
}
// Shoutboxin näyttö
function disableShoutbox() {
const elements = ['#shoutbox', '#shouts', '#shout-form'];
elements.forEach(selector => {
const element = document.querySelector(selector);
if (element) {
element.style.display = '';
}
});
console.log('Shoutbox näytetty');
}
// Viestien piilotus
function hidePosts(ids) {
if (window.location.pathname.includes('/sodat/')) {
chrome.storage.local.get(['hidelistaPostIndex'], function(data) {
const skipIdx = data.hidelistaPostIndex;
document.querySelectorAll('.post').forEach((post, idx) => {
const userId = post.getAttribute('data-user-id');
if (idx !== skipIdx && userId && ids.includes(userId)) {
post.style.display = 'none';
}
});
console.log('Posts hidden:', ids);
});
}
}
// Viestien näyttäminen
function showPosts(ids) {
if (window.location.pathname.includes('/sodat/')) {
document.querySelectorAll('.post').forEach(post => {
const userId = post.getAttribute('data-user-id');
if (userId && ids.includes(userId)) {
post.style.display = '';
}
});
console.log('Viestit näytetty:', ids);
}
}
// Viestien korostus
function applyPostHighlighting() {
const baseURL = 'https://ylilauta.org/sodat/';
if (window.location.href.startsWith(baseURL)) {
const config = { upvoteThreshold: 5 };
document.querySelectorAll(".post").forEach(post => {
const upvoteElement = post.querySelector(".post-button .post-upvotes");
if (upvoteElement) {
const upvoteCount = parseInt(upvoteElement.textContent);
if (upvoteCount > config.upvoteThreshold) {
post.style.border = "3px solid green";
post.style.backgroundColor = "rgba(0, 255, 0, 0.1)";
}
}
});
console.log('Korostus käytössä');
}
}
// Korostuksen poisto
function removePostHighlighting() {
document.querySelectorAll(".post").forEach(post => {
post.style.border = '';
post.style.backgroundColor = '';
});
console.log('Korostus pois käytöstä');
}
// Poista vanhat CSS-säännöt, jotka piilottavat kultapainikkeen
function removeCSSRules() {
const stylesheets = document.styleSheets;
Array.from(stylesheets).forEach(stylesheet => {
try {
const rules = stylesheet.cssRules || stylesheet.rules;
if (!rules) return;
for (let j = rules.length - 1; j >= 0; j--) {
const rule = rules[j].cssText;
if (rule.includes("#navbar .button-gold-buy") ||
rule.includes("a.button-gold-buy") ||
rule.includes("a.button-gold-buy @media (max-width: 900px)")) {
stylesheet.deleteRule(j);
}
}
} catch (e) {
console.error("Tyylisääntöjä ei voitu muokata:", e);
}
});
}
removeCSSRules();
// Korvaa kultapainike vierityspainikkeilla
function replaceGoldBuyButtons() {
const targetButtons = document.querySelectorAll(
'.button.button-gold-buy, a.button.gold-button'
);
targetButtons.forEach(buttonElement => {
buttonElement.removeAttribute('onclick');
if (buttonElement.tagName.toLowerCase() === 'a') {
buttonElement.removeAttribute('href');
}
const newButtonElement = buttonElement.cloneNode(true);
buttonElement.parentNode.replaceChild(newButtonElement, buttonElement);
newButtonElement.innerHTML = '';
const buttonContainer = document.createElement('div');
buttonContainer.className = 'scroll-button-container';
buttonContainer.appendChild(createScrollButton('icon-enter-up2', scrollToTop, 'Ylös'));
buttonContainer.appendChild(createScrollButton('icon-enter-down2', scrollToBottom, 'Alas'));
newButtonElement.appendChild(buttonContainer);
});
}
// Hidelistan hakuviestien perusteella
function updateHidelistaCache() {
const posts = document.querySelectorAll('.post-message, .post-content');
let candidates = [];
const markerRegex = /#hidelista/i;
const guidanceKeywords = ["hidetysohje", "rybo", "rybot", "ohje", "#rybolista", "muistutus"];
const minLength = 100;
// Tyhjennetään aiemmat id:t välittömästi, että piilotus toimii heti
postIds = [];
chrome.storage.local.set({ postIds: [] }, () => {
console.log("HIDELISTA välimuisti tyhjennetty.");
});
posts.forEach((post, idx) => {
const rawText = post.innerText.trim();
const lowerText = rawText.toLowerCase();
if (rawText.length < minLength) return;
if (markerRegex.test(rawText)) {
// Vanha tyyli: etsitään "ID x" -numerot
const oldStyleIds = [];
const idMatches = rawText.matchAll(/ID\s?(\d+)/gi);
for (const match of idMatches) {
oldStyleIds.push(parseInt(match[1], 10));
}
if (oldStyleIds.length > 0) {
if (!oldStyleIds.includes(1)) oldStyleIds.push(1);
oldStyleIds.sort((a, b) => a - b);
candidates.push({ numbers: oldStyleIds, idx });
return;
}
// Uusi tyyli: ohjetekstin sisältö ja rivien numerot
if (guidanceKeywords.filter(g => lowerText.includes(g)).length >= 2) {
let lines = rawText.split(/\r?\n/).map(l => l.trim()).filter(l => l.length > 0);
if (markerRegex.test(lines[0])) lines.shift();
let numbers = [];
for (const line of lines) {
let cleanedLine = line.replace(/\([^)]*\)/g, '').trim();
if (/^\d+$/.test(cleanedLine)) numbers.push(parseInt(cleanedLine, 10));
}
numbers = numbers.filter(n => n < 1000);
if (numbers.length >= 3) candidates.push({ numbers, idx });
}
}
});
if (candidates.length > 0) {
let bestCandidate = candidates.reduce((prev, curr) => {
if (curr.numbers.length > prev.numbers.length) return curr;
if (curr.numbers.length === prev.numbers.length)
return (curr.idx < prev.idx ? curr : prev);
return prev;
});
let uniqueNumbers = Array.from(new Set(bestCandidate.numbers)).sort((a, b) => a - b);
postIds = uniqueNumbers.map(String);
chrome.storage.local.set({
postIds,
hidelistaPostIndex: bestCandidate.idx
}, () => {
console.log("HIDELISTA välimuisti päivitetty:", postIds);
if (hidePostsEnabled && window.location.pathname.includes('/sodat/')) {
hidePosts(postIds);
}
});
} else {
console.log("HIDELISTA-viestiä ei löytynyt.");
postIds = [];
chrome.storage.local.set({ postIds: [] });
if (window.location.pathname.includes('/sodat/')) {
showPosts(postIds);
}
}
}
// Yläotsikko /sodat/-sivulle tilatiedoilla
function insertStatusHeader() {
if (!window.location.pathname.includes('/sodat/')) return;
chrome.storage.local.get(['highlightEnabled', 'hidePostsEnabled', 'shoutboxEnabled'], function(data) {
const highlightEnabled = data.highlightEnabled !== undefined ? data.highlightEnabled : true;
const hidePostsEnabled = data.hidePostsEnabled !== undefined ? data.hidePostsEnabled : true;
const shoutboxEnabled = data.shoutboxEnabled !== undefined ? data.shoutboxEnabled : true;
const header = document.createElement('h2');
header.style.fontSize = '1.2rem';
header.style.margin = '10px';
header.style.padding = '10px';
header.style.border = '2px solid #ccc';
header.style.borderRadius = '8px';
header.style.background = '#f5f5f5';
header.style.color = '#333';
header.style.textAlign = 'center';
header.style.fontFamily = 'sans-serif';
header.innerHTML = `
<span>${highlightEnabled ? '✅ Korostus käytössä' : '❌ Korostus pois'}</span> |
<span>${hidePostsEnabled ? '✅ Piilotus käytössä' : '❌ Piilotus pois'}</span> |
<span>${shoutboxEnabled ? '✅ Shoutbox käytössä' : '❌ Shoutbox pois'}</span>
`;
document.body.insertBefore(header, document.body.firstChild);
});
}
// PÄÄSUORITUS
// Alustetaan tilat local storagesta
chrome.storage.local.get(['highlightEnabled', 'hidePostsEnabled', 'shoutboxEnabled', 'postIds'], function(data) {
if (data.highlightEnabled !== undefined) highlightEnabled = data.highlightEnabled;
if (data.hidePostsEnabled !== undefined) hidePostsEnabled = data.hidePostsEnabled;
if (data.shoutboxEnabled !== undefined) shoutboxEnabled = data.shoutboxEnabled;
if (data.postIds !== undefined) postIds = data.postIds;
// Suorita asetusten mukaiset toimenpiteet
if (highlightEnabled) {
applyPostHighlighting();
} else {
removePostHighlighting();
}
if (hidePostsEnabled) {
hidePosts(postIds);
} else {
showPosts(postIds);
}
if (!shoutboxEnabled) {
enableShoutbox();
} else {
disableShoutbox();
}
insertStatusHeader();
replaceGoldBuyButtons();
updateHidelistaCache();
});
// Pidä hidelista päivityksissä esim. 5 minuutin välein
setInterval(updateHidelistaCache, 5 * 60 * 1000);