-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
419 lines (351 loc) · 13.8 KB
/
content.js
File metadata and controls
419 lines (351 loc) · 13.8 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
(function () {
/*
microweb Content Script
Supports multiple color area selections with fixed/floating types
Per-page area storage using URL as key
*/
if (window.microwebListenerAttached) {
console.log("microweb - Script already running.");
return;
}
window.microwebListenerAttached = true;
// -- State --
var isMonochrome = true;
var isHidingEngagement = true;
var selectionMode = false;
var editingAreaId = null;
var newAreaType = 'floating';
var colorAreas = [];
var nextAreaId = 1;
var pageKey = getPageKey();
// -- Elements --
var overlayContainer = null;
var selectionLayer = null;
var selectionBox = null;
function getPageKey() {
// Use hostname + pathname as unique key for this page
return window.location.hostname + window.location.pathname;
}
function init() {
console.log("microweb - Starting for page:", pageKey);
createOverlays();
loadSettings();
window.addEventListener('scroll', handleScroll);
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action === "ping") {
sendResponse({ status: "ok", pageKey: pageKey });
return;
}
if (request.action === "updateSettings") {
isMonochrome = request.settings.monochrome;
isHidingEngagement = request.settings.hideEngagement;
updateState();
sendResponse({ status: "ok" });
} else if (request.action === "toggleSelectionMode") {
editingAreaId = null;
newAreaType = request.areaType || 'floating';
enterSelectionMode();
sendResponse({ status: "ok" });
} else if (request.action === "resetArea") {
colorAreas = [];
saveAreas();
updateOverlay();
sendResponse({ areas: colorAreas, pageKey: pageKey });
} else if (request.action === "getAreas") {
sendResponse({ areas: colorAreas, pageKey: pageKey });
} else if (request.action === "deleteArea") {
deleteArea(request.areaId);
sendResponse({ areas: colorAreas, pageKey: pageKey });
} else if (request.action === "editArea") {
editingAreaId = request.areaId;
var area = colorAreas.find(function (a) { return a.id === request.areaId; });
if (area) newAreaType = area.type;
highlightAreaForEdit(request.areaId);
sendResponse({ status: "ok" });
} else if (request.action === "toggleAreaType") {
toggleAreaType(request.areaId);
sendResponse({ areas: colorAreas, pageKey: pageKey });
}
return true;
});
}
function createOverlays() {
if (document.getElementById('microweb-root')) return;
overlayContainer = document.createElement('div');
overlayContainer.id = 'microweb-root';
document.body.appendChild(overlayContainer);
selectionLayer = document.createElement('div');
selectionLayer.id = 'microweb-selection-layer';
document.body.appendChild(selectionLayer);
selectionLayer.addEventListener('mousedown', handleMouseDown);
selectionLayer.addEventListener('mousemove', handleMouseMove);
selectionLayer.addEventListener('mouseup', handleMouseUp);
overlayContainer.style.display = 'none';
}
function updateState() {
updateOverlay();
if (isHidingEngagement) {
document.documentElement.classList.add('mw-hide-engagement');
} else {
document.documentElement.classList.remove('mw-hide-engagement');
}
}
function handleScroll() {
updateOverlay();
}
function updateOverlay() {
if (!overlayContainer) return;
if (!isMonochrome) {
overlayContainer.style.display = 'none';
return;
}
overlayContainer.style.display = 'block';
overlayContainer.innerHTML = '';
if (colorAreas.length === 0) {
var full = document.createElement('div');
full.className = 'mw-overlay-full';
overlayContainer.appendChild(full);
} else if (colorAreas.length === 1) {
createSingleAreaOverlay(colorAreas[0]);
} else {
createMultiAreaOverlay();
}
}
function getViewportArea(area) {
var scrollY = window.scrollY || window.pageYOffset;
var y;
if (area.type === 'fixed') {
y = area.originalY - scrollY;
} else {
y = area.y;
}
return {
x: area.x,
y: y,
width: area.width,
height: area.height
};
}
function createSingleAreaOverlay(area) {
var vw = window.innerWidth;
var vh = window.innerHeight;
var rect = getViewportArea(area);
if (rect.y > 0) {
createPartialOverlay(0, 0, vw, rect.y);
}
var bottomStart = rect.y + rect.height;
if (bottomStart < vh) {
createPartialOverlay(0, bottomStart, vw, vh - bottomStart);
}
if (rect.x > 0) {
createPartialOverlay(0, Math.max(0, rect.y), rect.x, Math.min(rect.height, vh));
}
var rightStart = rect.x + rect.width;
if (rightStart < vw) {
createPartialOverlay(rightStart, Math.max(0, rect.y), vw - rightStart, Math.min(rect.height, vh));
}
}
function createMultiAreaOverlay() {
var vw = window.innerWidth;
var vh = window.innerHeight;
var rects = colorAreas.map(function (area) {
return getViewportArea(area);
}).filter(function (r) {
return r.y + r.height > 0 && r.y < vh && r.x + r.width > 0 && r.x < vw;
});
if (colorAreas.length === 0) {
var full = document.createElement('div');
full.className = 'mw-overlay-full';
overlayContainer.appendChild(full);
return;
}
var minX = Math.min.apply(null, rects.map(function (r) { return r.x; }));
var minY = Math.min.apply(null, rects.map(function (r) { return r.y; }));
var maxX = Math.max.apply(null, rects.map(function (r) { return r.x + r.width; }));
var maxY = Math.max.apply(null, rects.map(function (r) { return r.y + r.height; }));
minX = Math.max(0, minX);
minY = Math.max(0, minY);
maxX = Math.min(vw, maxX);
maxY = Math.min(vh, maxY);
if (minY > 0) {
createPartialOverlay(0, 0, vw, minY);
}
if (maxY < vh) {
createPartialOverlay(0, maxY, vw, vh - maxY);
}
if (minX > 0) {
createPartialOverlay(0, minY, minX, maxY - minY);
}
if (maxX < vw) {
createPartialOverlay(maxX, minY, vw - maxX, maxY - minY);
}
fillInternalGaps(rects, minX, minY, maxX, maxY);
}
function fillInternalGaps(rects, minX, minY, maxX, maxY) {
var xCoords = [minX, maxX];
var yCoords = [minY, maxY];
rects.forEach(function (r) {
xCoords.push(Math.max(minX, r.x));
xCoords.push(Math.min(maxX, r.x + r.width));
yCoords.push(Math.max(minY, r.y));
yCoords.push(Math.min(maxY, r.y + r.height));
});
xCoords = xCoords.filter(function (v, i, a) { return a.indexOf(v) === i; }).sort(function (a, b) { return a - b; });
yCoords = yCoords.filter(function (v, i, a) { return a.indexOf(v) === i; }).sort(function (a, b) { return a - b; });
for (var i = 0; i < xCoords.length - 1; i++) {
for (var j = 0; j < yCoords.length - 1; j++) {
var cellX = xCoords[i];
var cellY = yCoords[j];
var cellW = xCoords[i + 1] - cellX;
var cellH = yCoords[j + 1] - cellY;
var cellCenterX = cellX + cellW / 2;
var cellCenterY = cellY + cellH / 2;
var insideAny = rects.some(function (r) {
return cellCenterX >= r.x && cellCenterX <= r.x + r.width &&
cellCenterY >= r.y && cellCenterY <= r.y + r.height;
});
if (!insideAny && cellW > 0 && cellH > 0) {
createPartialOverlay(cellX, cellY, cellW, cellH);
}
}
}
}
function createPartialOverlay(left, top, width, height) {
if (width <= 0 || height <= 0) return;
var div = document.createElement('div');
div.className = 'mw-overlay-part';
div.style.left = left + 'px';
div.style.top = top + 'px';
div.style.width = width + 'px';
div.style.height = height + 'px';
overlayContainer.appendChild(div);
}
function deleteArea(areaId) {
colorAreas = colorAreas.filter(function (a) { return a.id !== areaId; });
saveAreas();
updateOverlay();
}
function toggleAreaType(areaId) {
var area = colorAreas.find(function (a) { return a.id === areaId; });
if (!area) return;
var scrollY = window.scrollY || window.pageYOffset;
if (area.type === 'fixed') {
area.type = 'floating';
area.y = area.originalY - scrollY;
} else {
area.type = 'fixed';
area.originalY = area.y + scrollY;
}
saveAreas();
updateOverlay();
}
function highlightAreaForEdit(areaId) {
var area = colorAreas.find(function (a) { return a.id === areaId; });
if (!area) return;
colorAreas = colorAreas.filter(function (a) { return a.id !== areaId; });
updateOverlay();
enterSelectionMode();
if (selectionBox) selectionBox.remove();
selectionBox = document.createElement('div');
selectionBox.className = 'mw-selection-box mw-editing';
selectionLayer.appendChild(selectionBox);
var displayY = area.type === 'fixed' ? area.originalY - (window.scrollY || 0) : area.y;
updateSelectionBox(area.x, displayY, area.width, area.height);
}
function loadSettings() {
// Load global settings + page-specific areas
chrome.storage.sync.get(['monochrome', 'hideEngagement'], function (result) {
isMonochrome = result.monochrome !== false;
isHidingEngagement = result.hideEngagement !== false;
// Load page-specific areas from local storage (per-page)
chrome.storage.local.get(['areas_' + pageKey], function (localResult) {
var storedAreas = localResult['areas_' + pageKey];
if (storedAreas && Array.isArray(storedAreas)) {
colorAreas = storedAreas;
nextAreaId = colorAreas.reduce(function (max, a) { return Math.max(max, a.id + 1); }, 1);
}
updateState();
});
});
}
function saveAreas() {
// Save page-specific areas to local storage
var data = {};
data['areas_' + pageKey] = colorAreas;
chrome.storage.local.set(data);
}
function enterSelectionMode() {
selectionMode = true;
if (selectionLayer) selectionLayer.style.display = 'block';
document.body.style.cursor = 'crosshair';
}
function exitSelectionMode() {
selectionMode = false;
if (selectionLayer) selectionLayer.style.display = 'none';
document.body.style.cursor = 'default';
if (selectionBox) {
selectionBox.remove();
selectionBox = null;
}
editingAreaId = null;
}
var isDragging = false;
var startX = 0, startY = 0;
function handleMouseDown(e) {
if (!selectionMode) return;
e.preventDefault();
isDragging = true;
startX = e.clientX;
startY = e.clientY;
if (!selectionBox) {
selectionBox = document.createElement('div');
selectionBox.className = 'mw-selection-box';
selectionLayer.appendChild(selectionBox);
}
updateSelectionBox(startX, startY, 0, 0);
}
function handleMouseMove(e) {
if (!selectionMode || !isDragging) return;
e.preventDefault();
var currentX = e.clientX;
var currentY = e.clientY;
var width = currentX - startX;
var height = currentY - startY;
var finalX = width < 0 ? currentX : startX;
var finalY = height < 0 ? currentY : startY;
var finalW = Math.abs(width);
var finalH = Math.abs(height);
updateSelectionBox(finalX, finalY, finalW, finalH);
}
function handleMouseUp(e) {
if (!selectionMode || !isDragging) return;
isDragging = false;
var rect = selectionBox.getBoundingClientRect();
var scrollY = window.scrollY || window.pageYOffset;
if (rect.width > 10 && rect.height > 10) {
var newArea = {
id: editingAreaId !== null ? editingAreaId : nextAreaId++,
x: rect.left,
y: rect.top,
width: rect.width,
height: rect.height,
type: newAreaType
};
if (newAreaType === 'fixed') {
newArea.originalY = rect.top + scrollY;
}
colorAreas.push(newArea);
saveAreas();
updateOverlay();
}
exitSelectionMode();
}
function updateSelectionBox(x, y, w, h) {
if (!selectionBox) return;
selectionBox.style.left = x + 'px';
selectionBox.style.top = y + 'px';
selectionBox.style.width = w + 'px';
selectionBox.style.height = h + 'px';
}
init();
})();