-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
59 lines (51 loc) · 1.54 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
const observeImage = async (img) => {
if (img.classList.contains("overlay-applied")) {
return;
}
const newImg = document.createElement("img");
index = Math.floor(Math.random() * 10);
const randomSen = chrome.runtime.getURL(`assets/sen_bundle${index}.jpeg`);
newImg.src = randomSen;
newImg.style.position = "absolute";
newImg.classList.add("overlay-applied");
const rect = img.getBoundingClientRect();
newImg.style.left = `${rect.left + window.scrollX}px`;
newImg.style.top = `${rect.top + window.scrollY}px`;
newImg.style.width = `${rect.width}px`;
newImg.style.height = `${rect.height}px`;
document.body.appendChild(newImg);
img.style.opacity = "0";
img.classList.add("overlay-applied");
};
const imageObserver = new IntersectionObserver(
(entries, observer) => {
entries.forEach(async (entry) => {
if (entry.isIntersecting) {
observer.unobserve(entry.target);
await observeImage(entry.target);
}
});
},
{
rootMargin: "0px",
threshold: 0.1,
}
);
const setupObservers = () => {
document
.querySelectorAll('div[data-testid="tweetPhoto"] img')
.forEach(async (img) => {
imageObserver.observe(img);
});
const mutationObserver = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.tagName === "IMG") {
imageObserver.observe(node);
}
});
});
});
mutationObserver.observe(document.body, { childList: true, subtree: true });
};
setupObservers();