Skip to content

Instantly share code, notes, and snippets.

@GoldenretriverYT
Created November 29, 2024 17:41
Show Gist options
  • Save GoldenretriverYT/d4d49bec9763b0882d337da187d11069 to your computer and use it in GitHub Desktop.
Save GoldenretriverYT/d4d49bec9763b0882d337da187d11069 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Allow IG Likes to be opened in new tab
// @namespace Violentmonkey Scripts
// @match https://www.instagram.com/*
// @grant none
// @version 1.0
// @author retrievaaaa
// @description Allows you to open your likes on the my activity page in a new tab by middle clicking / scroll wheel clicking
// ==/UserScript==
JSON.parse = ((parse) => function() {
const parsed = parse.apply(this, arguments);
setTimeout(() => {
handleJson(parsed)
}, 1000)
return parsed;
})(JSON.parse);
function handleJson(json) {
// recursively search for all attributes with key on_bind where the text includes `", "clips", `
function traverse(node) {
if (typeof node === 'object' && node !== null && node.on_bind && (node.on_bind.includes('", "clips", ') || node.on_bind.includes('", "feed", ') || node.on_bind.includes('", "carousel_container", '))) {
console.log(node)
handleFound(node)
}
if (Array.isArray(node)) {
node.forEach(traverse)
} else if (typeof node === 'object' && node !== null) {
Object.values(node).forEach(traverse)
}
}
traverse(json)
}
function handleFound(node) {
// Find all indices of `", "clips", ` and `", "feed", ` and `", "carousel_container", `
const indices = []
let index = node.on_bind.indexOf('", "clips", ')
const types = [
['clips', '", "clips", '],
['feed', '", "feed", '],
['carousel_container', '", "carousel_container", ']
];
for (const [type, searchStr] of types) {
index = node.on_bind.indexOf(searchStr);
while (index !== -1) {
indices.push([index, type]);
index = node.on_bind.indexOf(searchStr, index + 1);
}
}
console.log(indices)
const videos = [];
for (const [index, type] of indices) {
// get all text that comes BEFORE index until we reach the previous "
const before = node.on_bind.slice(0, index)
const videoId = before.slice(before.lastIndexOf('"') + 1)
const imageUrlIndex = index + 34 +type.length;
// get image url until next "
const imageUrl = node.on_bind.slice(imageUrlIndex, node.on_bind.indexOf('"', imageUrlIndex))
videos.push({
videoId,
imageUrl: imageUrl.replaceAll('\\', '')
})
}
handleVideos(videos)
}
function handleVideos(videos) {
// find all img tags with src attribute that contains the imageUrl
for (const video of videos) {
const imgTags = document.querySelectorAll(`img[src^="${video.imageUrl}"]`)
if (imgTags.length == 0) {
continue;
}
const imgTag = imgTags[0];
// add event handlers
imgTag.parentNode.parentNode.addEventListener('mouseup', (ev) => {
// open in new tab on scroll wheel click
if (ev.button === 1) {
window.open(`https://www.instagram.com/p/${video.videoId}/`, '_blank');
}
})
imgTag.parentNode.parentNode.addEventListener("mousedown", (ev) => {
if (ev.button === 1) { // dont make da scroll compass thingy show up
ev.preventDefault();
return false;
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment