-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent_script.js
49 lines (44 loc) · 1.32 KB
/
content_script.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
// Put all the javascript code here, that you want to execute after page load.
(function () {
if (window.hasRun) {
return;
}
window.hasRun = true;
const div = document.createElement("div");
browser.runtime.onMessage.addListener((message) => {
if (message.command == "on") {
document.addEventListener("mouseover", handleMouseEnterImage);
}
if (message.command == "off") {
document.removeEventListener("mouseover", handleMouseEnterImage);
}
});
function handleMouseEnterImage(e) {
if (isImage(e)) {
const gparent = e.target.parentElement.parentElement;
div.textContent = e.target.alt == "" ? "Alt not found" : e.target.alt;
addClasses(div);
gparent.appendChild(div);
gparent.addEventListener("mouseout", handleMouseOutImage, {
once: true,
});
}
}
function handleMouseOutImage(e) {
if (isImage(e)) {
div.classList.add("twitter-alt-div--hidden");
}
}
function isImage(e) {
return (
e.target.tagName == "IMG" &&
!e.target.src.includes("https://abs-0.twimg.com/emoji/")
);
}
function addClasses(instertedDiv) {
instertedDiv.classList.add("twitter-alt-div--hidden", "twitter-alt-div");
requestAnimationFrame(() => {
instertedDiv.classList.remove("twitter-alt-div--hidden");
});
}
})();