forked from jerrylususu/mainonly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainonly.js
83 lines (71 loc) · 2.73 KB
/
mainonly.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
// A JavaScript bookmarklet designed to isolate and highlight a specific element on a webpage, effectively hiding all other elements.
javascript: (function () {
var selectedElement = null;
var style = document.createElement("style");
style.textContent =
".mainonly-outline { outline: 2px solid red; } .mainonly-hidden { display: none; }";
document.head.appendChild(style);
function outlineElement(element) {
if (selectedElement) {
selectedElement.classList.remove("mainonly-outline");
}
selectedElement = element;
selectedElement.classList.add("mainonly-outline");
}
function removeOutline(element) {
element.classList.remove("mainonly-outline");
}
function onMouseOver(event) {
outlineElement(event.target);
}
function removeOtherElements(element) {
var allElements = document.body.getElementsByTagName("*");
for (var i = 0; i < allElements.length; i++) {
if (
element.contains(allElements[i]) ||
allElements[i].contains(element)
) {
continue;
}
allElements[i].classList.add("mainonly-hidden");
}
}
function onClick(event) {
event.preventDefault();
removeOtherElements(selectedElement);
cleanupEventListeners();
removeOutline(selectedElement);
}
function showHiddenElements(event) {
if (event.key === "Escape") {
var hiddenElements = document.querySelectorAll(".mainonly-hidden");
hiddenElements.forEach(function (element) {
element.classList.remove("mainonly-hidden");
});
}
document.removeEventListener("keydown", showHiddenElements, false);
}
function onWheel(event) {
event.preventDefault();
if (event.deltaY < 0) {
// Scrolling up, select parent element
if (selectedElement.parentElement) {
outlineElement(selectedElement.parentElement);
}
} else {
// Scrolling down, select first child element
if (selectedElement.children.length > 0) {
outlineElement(selectedElement.children[0]);
}
}
}
function cleanupEventListeners() {
document.removeEventListener("mouseover", onMouseOver, false);
document.removeEventListener("click", onClick, false);
document.removeEventListener("wheel", onWheel, { passive: false });
}
document.addEventListener("mouseover", onMouseOver, false);
document.addEventListener("click", onClick, false);
document.addEventListener("wheel", onWheel, { passive: false });
document.addEventListener("keydown", showHiddenElements, false);
})();