-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentScript.js
244 lines (204 loc) · 6.63 KB
/
contentScript.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
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
let selector = "a[href*='instagram.']:not(:has(.hoverable)), a[href*='tiktok.']:not(:has(.hoverable)), a[href*='youtube.']:not(:has(.hoverable)), a[href*='youtu.be']:not(:has(.hoverable))"
let links = Array.from(document.querySelectorAll(selector));
//links = document.querySelectorAll('a')
links.forEach(node => {
addmarker(node);
addlister(node);
});
const observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (mutation.addedNodes.length > 0) {
let new_nodes = Array.from(document.querySelectorAll(selector));
//new_nodes = Array.from(document.querySelectorAll('a'))
//filter out the nodes that already have a marker
new_nodes = new_nodes.filter(node => node.getAttribute('data-hoverable') !== 'true');
for (let node of new_nodes) {
addmarker(node);
addlister(node);
}
}
});
});
// Start observing the document
observer.observe(document, { childList: true, subtree: true });
let div = document.createElement('div');
div.className = 'move';
document.body.appendChild(div);
let elem = document.querySelector('.mover')
let over = false
let id = 187
let dimens = { width: 80, height: 40 }
let pos = { x: 0, y: 0 }
// element mousemove to stop
document.body.addEventListener('mousemove', function (e) {
if (over) {
pos.x = e.pageX;
pos.y = e.pageY;
positionOverlay()
}
}, true);
function positionOverlay() {
let left = pos.x + 5
let top = pos.y + 15
if (left > window.innerWidth - dimens.width - 15) {
//right
left = window.innerWidth - dimens.width - 15
}
if (top > window.innerHeight - dimens.height - 15) {
//bottom
top = pos.y - dimens.height - 15
}
div.style.left = `${left}px`;
div.style.top = `${top}px`;
}
function addlister(element) {
element.addEventListener("mouseleave", function (event) {
over = false;
removeOverlay()
}, false);
element.addEventListener("mouseover", function (event) {
over = true;
id = Math.random() * 10000
addOverlay(element, id)
}, false);
}
function addmarker(node) {
//add a marker to the link to prevent it from being processed again
node.setAttribute('data-hoverable', 'true');
}
/**
* adds an overlay to the link
* @param {*} elem
* @param {*} pid
*/
async function addOverlay(elem, pid) {
let href = elem.href;
let parsed = parselinks(href);
//just here for testing should be below the parsed check
//show loading animation
if (!parsed)
return;
div.innerHTML = '<div class="lds-ellipsis"><div></div><div></div><div></div><div></div></div>'
dimens = { width: 80, height: 40 }
switch (parsed.type) {
case "yt":
await showyt(parsed, pid);
break;
case "tt":
await showtt(parsed, pid);
break;
case "ig":
showig(parsed, pid);
break;
}
}
/**
* parses a link and returns the type and the data
* @param {*} link
* @returns {type: string, data: string}
*/
function parselinks(link) {
let ytregex = /^.*(youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/; //maybe add support for yt profiles later
let ttregex = /^.*(tiktok\.com\/)([^#\&\?]*).*/; //diferentiate between profile and video
let igprofileregex = /(instagram\.com\/[^/]*)\/?(\?hl=\w{2})?$/;
let ytmatch = link.match(ytregex);
let ttmatch = link.match(ttregex);
let igprofilematch = link.match(igprofileregex);
if (!ytmatch && !ttmatch && !igprofilematch)
return undefined;
if (ytmatch)
return { type: "yt", data: ytmatch?.[2] };
if (ttmatch)
return { type: "tt", data: link };
if (igprofilematch)
return { type: "ig", data: igprofilematch };
}
/**
* embeds a youtube video
* @param {*} data
* @param {*} pid
*/
async function showyt(data, pid) {
let videoId = data.data;
let ytdata = await fetch(`https://youtube.com/oembed?url=https://www.youtube.com/watch?v=${videoId}&format=json`).then((response) => response.json())
if (over && id == pid)
div.innerHTML = `<div> <img src="https://img.youtube.com/vi/${videoId}/maxresdefault.jpg" alt="Youtube Video" class=hoverimage> <div class="overlay-text"> ${ytdata.title} </div> </div>`;
changedimensions(pid)
}
/**
* embeds a tiktok video or profile
* TODO: richtiger video embed und nicht nur thumbnail
* @param {*} data
* @param {*} pid
*/
async function showtt(data, pid) {
let ttdata;
try {
ttdata = await fetch(`https://www.tiktok.com/oembed?url=${data.data}`).then((response) => response.json())
}
catch (e) {
console.log(e)
showerror(pid)
return;
}
if (ttdata?.code == 400) {
showerror(pid)
return;
}
if (over && id == pid)
if (ttdata?.embed_type == "video")
div.innerHTML = `<div> <img src="${ttdata.thumbnail_url}" alt="Tiktok Video" class=hoverimage style="width: 200px;"> <div class="overlay-text" style="width: 170px;"> ${ttdata.title} </div> </div>`;
else if (ttdata?.embed_type == "profile") {
let name = ttdata.author_url.split("/")[3]
div.innerHTML = `<iframe class="tt" src = "https://www.tiktok.com/embed/${name}" frameborder="0" seamless="seamless" scrolling="no"></iframe >`
}
changedimensions(pid)
}
/**
* embeds an instagram profile
* @param {*} data
* @param {*} pid
*/
function showig(data, pid) {
let link = `https://www.${data.data[1]}/embed/`
if (over && id == pid) {
div.innerHTML = `<iframe class="insta" src="${link}" width="100%" height="100%" frameborder="0" scrolling="no" allowtransparency="true"></iframe>`
dimens.width = div.offsetWidth;
dimens.height = div.offsetHeight;
positionOverlay()
}
}
/**
* embeds an imgur image (TODO)
* @param {*} data
* @param {*} pid
*/
async function showimgur(data, pid) {
}
/**
* changes the dimensions of the overlay to the dimensions of the image
* @param {*} pid
*/
function changedimensions(pid) {
let img = document.querySelector('.hoverimage')
if (!img)
return;
img.addEventListener('load', function () {
if (id = pid) {
dimens.width = div.offsetWidth;
dimens.height = div.offsetHeight;
positionOverlay()
}
})
}
/**
* shows Error message in case of an error
* @param {*} pid
*/
async function showerror(pid) {
if (over && id == pid)
div.innerHTML = `<div class="errordiv"> ERROR</div>`;
}
function removeOverlay() {
div.innerHTML = ""
}