-
Notifications
You must be signed in to change notification settings - Fork 6
/
content.js
34 lines (32 loc) · 1.2 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
const pollingInterval = 500;
function replaceLightboxElementWithAnchor() {
// only disable lightbox for /r/*/comments/* pages
if (!(window.location.host === "www.reddit.com" && window.location.pathname.includes("/comments/"))) {
return;
}
browser.storage.local.get('disableLightbox', function(result) {
if (result.disableLightbox) {
const images = document.querySelectorAll('img.media-lightbox-img');
for (let i = 0; i < images.length; i++) {
const img = images[i];
if (img.parentNode.nodeName == "A") {
continue;
}
if (img.hasAttribute('src')) {
var src = img.getAttribute('src');
} else if (img.hasAttribute('data-lazy-src')) {
var src = img.getAttribute('data-lazy-src');
} else {
continue;
}
const a = document.createElement('a');
a.setAttribute('href', src);
img.parentNode.insertBefore(a, img);
a.appendChild(img);
}
}
});
}
replaceLightboxElementWithAnchor();
// regularly attempt to replace the lightbox as the window.location might have changed to a post page using history.pushState
setInterval(replaceLightboxElementWithAnchor, pollingInterval);