I've been using the InstagramPhotoLink extension for Opera for some time but instagram broke it recently. I thought I could try to fix it... and I did.
The original extension is rather limited and it supports only a link to the first image and no videos so while fixing it I completely rewrote it in order to extend it.
It works by looking for an image
or video
element with a specific class
and extracts the src
attribute from it and puts it in a new a
-element inside the media-container.
Switching images/videos is supported by the MutationObserver
that updates the links when either a div
or an attribute changes.
I came across a problem where the MutatorObserver
reacts too quickly and before the src
attribute of the new image is set. I solved it by adding a small delay by adding a call to setTimeout
to wait until everything is updated.
function createMediaLink() {
let mo = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.type == "attributes") {
//console.log(mutation.attributeName);
addOrUpdateVideoLink();
}
if (mutation.type == "childList") {
//console.log(mutation.childList);
addOrUpdateImageLink();
}
});
});
return (
addOrUpdateImageLink(mo) ||
addOrUpdateVideoLink(mo)
);
}
function addOrUpdateImageLink(mo) {
let image = document.getElementsByClassName('FFVAD');
if (image.length == 0) {
return false;
}
image = image[0];
if (mo) {
// the nearest div to the image that stays there when switching images
var div = document.getElementsByClassName("rQDP3");
mo.observe(div[0], {
childList: true
});
}
setTimeout(() => {
addOrUpdateMediaLink(image.src);
}, 50);
return true;
}
function addOrUpdateVideoLink(mo) {
let video = document.getElementsByClassName('tWeCl');
if (video.length == 0) {
return false;
}
video = video[0];
if (mo) {
mo.observe(video, {
attributes: true
});
}
setTimeout(() => {
addOrUpdateMediaLink(video.src);
}, 50);
return true;
}
function addOrUpdateMediaLink(src) {
console.log(`src: ${src}`);
let a = document.getElementsByClassName('_open_');
if (a.length == 0) {
a = document.createElement("a");
a.className = "_open_";
a.innerHTML = 'Open in new tab';
a.target = "_blank";
// media container
document.getElementsByClassName("_97aPb")[0].appendChild(a);
} else {
a = a[0];
}
a.href = src;
}
createMediaLink();
I don't write javascript too often so this is the best I can do but I'm pretty sure this can still be improved. What do you think?
var div = document.getElementsByClassName("rQDP3");
working... if I go to the album view cats_of_instagram I don't see any results there, then if I click on an item, it opens the modal but I still don't see any results there... if I refresh the item is displayed but not in a modal. When do you see results for that? \$\endgroup\$_97aPb
- this seems to be a more common one that works on any page. I see that the one I used in my question applies only to certain pages :-| \$\endgroup\$