-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
182 lines (157 loc) · 5.05 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
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
function attachLinkHandlers() {
const links = document.querySelectorAll("a[href]");
links.forEach((link) => {
const linkOrigin = new URL(link.href).origin;
if (!link.dataset.listenerAttached) {
if (linkOrigin !== window.location.origin) {
link.addEventListener("click", handleLinkClick);
}
link.dataset.listenerAttached = true;
}
});
}
function detachLinkHandlers() {
const links = document.querySelectorAll("a[href]");
links.forEach((link) => {
if (link.dataset.listenerAttached) {
link.removeEventListener("click", handleLinkClick);
link.dataset.listenerAttached = false;
}
});
}
function handleLinkClick(event) {
let linkElement = event.target;
while (linkElement && linkElement.tagName !== "A") {
linkElement = linkElement.parentElement;
}
if (!linkElement) {
return;
}
event.preventDefault();
const infoBox = createInfoBox(linkElement.href);
document.body.appendChild(infoBox);
const linkRect = linkElement.getBoundingClientRect();
const topPosition = linkRect.bottom + window.scrollY;
const leftPosition = (window.innerWidth - infoBox.offsetWidth) / 2;
infoBox.style.top = `${topPosition}px`;
infoBox.style.left = `${leftPosition}px`;
}
function createInfoBox(linkHref) {
const infoBox = document.createElement("div");
infoBox.id = "infoBox" + linkHref;
const addressBarContainer = document.createElement("div");
const addressBar = createAddressBar(linkHref);
const toolbar = createToolbar(infoBox, linkHref);
addressBarContainer.appendChild(addressBar);
addressBarContainer.appendChild(toolbar);
infoBox.appendChild(addressBarContainer);
const iframe = createIframe(linkHref);
infoBox.appendChild(iframe);
styleInfoBox(infoBox);
styleAddressBarContainer(addressBarContainer);
return infoBox;
}
function createIframe(href) {
const iframe = document.createElement("iframe");
iframe.src = href;
iframe.id = "infoBoxIframe" + href;
iframe.style.width = "100%";
iframe.style.height = "100%";
iframe.style.border = "none";
iframe.onload = () => {
try {
const iframeDocument =
iframe.contentDocument || iframe.contentWindow.document;
observeIframeContent(iframeDocument);
} catch {}
};
return iframe;
}
function observeIframeContent(iframeDocument) {
const iframeObserver = new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.type === "childList") {
attachLinkHandlers(iframeDocument);
}
}
});
iframeObserver.observe(iframeDocument.body, {
childList: true,
subtree: true,
});
}
function createAddressBar(href) {
const addressBar = document.createElement("input");
addressBar.type = "text";
addressBar.value = href;
addressBar.style.width = "80%";
addressBar.style.marginRight = "10px";
addressBar.style.padding = "5px";
addressBar.style.borderRadius = "5px";
addressBar.style.border = "1px solid #ccc";
return addressBar;
}
function createToolbar(infoBox, linkHref) {
const toolbar = document.createElement("div");
toolbar.style.display = "flex";
toolbar.style.gap = "10px";
const closeButton = document.createElement("button");
closeButton.textContent = "Close";
closeButton.addEventListener("click", function () {
infoBox.remove();
});
const openTabButton = document.createElement("button");
openTabButton.textContent = "Open in new tab";
openTabButton.addEventListener("click", function () {
window.open(this.parentNode.previousElementSibling.value, "_blank");
});
const toggleButton = document.createElement("button");
toggleButton.textContent = "Collapse";
toggleButton.addEventListener("click", function () {
const iframe = document.getElementById("infoBoxIframe" + linkHref);
if (iframe.style.display === "none") {
iframe.style.display = "block";
infoBox.style.height = "97vh";
toggleButton.textContent = "Collapse";
} else {
iframe.style.display = "none";
infoBox.style.height = "55px";
toggleButton.textContent = "Expand";
}
});
toolbar.appendChild(openTabButton);
toolbar.appendChild(toggleButton);
toolbar.appendChild(closeButton);
return toolbar;
}
function styleAddressBarContainer(container) {
container.style.display = "flex";
container.style.alignItems = "center";
container.style.padding = "10px";
}
function styleInfoBox(infoBox) {
infoBox.style.position = "absolute";
infoBox.style.width = "97vw";
infoBox.style.height = "97vh";
infoBox.style.backgroundColor = "white";
infoBox.style.border = "1px solid black";
infoBox.style.zIndex = "1000";
infoBox.style.overflow = "hidden";
infoBox.style.boxShadow = "0px 4px 8px rgba(0, 0, 0, 0.1)";
}
const observer = new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.type === "childList") {
attachLinkHandlers();
}
}
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
chrome.storage.local.get(["isActive"], function (result) {
if (result?.isActive || false) {
attachLinkHandlers();
}
});