Created
March 6, 2024 13:50
-
-
Save Kenya-West/d2fc748958a88e8b7288bba6179f2b7f to your computer and use it in GitHub Desktop.
VK get feed sources you subscribed to
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is a script to get links from feed sources modal. | |
// How to use: | |
// 1. Go to your feed: https://vk.com/feed | |
// 2. Open the + button, list of feeds appears; | |
// 3. Do not add new source, select existing one; | |
// 4. Open the modal with sources you SUBSCRIBED to; | |
// 5. Apply this script in the browser's console; | |
// 6. It outputs a JSON you can copy. | |
const config = { | |
timeToLoad: 500, | |
timeToUnload: 250, | |
zeroResultLimit: 5, | |
}; | |
/** | |
* @type {Array<{title: string; link: string}>} | |
*/ | |
const linksList = new Array(); | |
let zeroResultCounter = 0; | |
// HERE WE GO | |
if (document.querySelector(".privacy_olist")) { | |
const timer = setInterval(async () => { | |
await work(); | |
if (zeroResultCounter >= config.zeroResultLimit) { | |
clearInterval(timer); | |
console.dir(Array.from(linksList)); | |
} | |
}, config.timeToLoad + zeroResultCounter * config.timeToUnload); | |
} else { | |
console.error("No links found"); | |
alert("No links found"); | |
} | |
// THESE ARE THE FUNCTIONS | |
async function work() { | |
let delta = linksList.length; | |
const links = await getLinks(); | |
extractDataFromLinks(links).forEach( | |
/** | |
* | |
* @param {{title: string; link: string}} link | |
*/ | |
(link) => { | |
if ( | |
linksList.findIndex( | |
/** | |
* | |
* @param {{title: string; link: string}} el | |
* @returns | |
*/ | |
(el) => el.link === link.link | |
) === -1 | |
) { | |
linksList.push(link); | |
} | |
} | |
); | |
delta = linksList.length - delta; | |
if (delta === 0) { | |
zeroResultCounter++; | |
} else { | |
zeroResultCounter = 0; | |
} | |
document.querySelector(".privacy_olist").scrollTo(0, 999999); | |
sleep(config.timeToLoad); | |
} | |
async function getLinks() { | |
const links = document.querySelectorAll(`.privacy_olist .olist > a[href]`); | |
const linksArr = Array.from(links); | |
console.log(`Found ${linksArr.length} links, proceed to process...`); | |
return linksArr; | |
} | |
/** | |
* | |
* @param {HTMLElement[]} links | |
* @returns {string[]} | |
*/ | |
function extractDataFromLinks(links) { | |
return links.map((link) => { | |
const data = { | |
title: getLinkTitle(link), | |
link: getLink(link), | |
}; | |
return data; | |
}); | |
} | |
/** | |
* | |
* @param {HTMLElement} link | |
* @returns {string} | |
*/ | |
function getLinkTitle(link) { | |
return link.querySelector(`.olist_item_name`)?.innerText ?? "Название"; | |
} | |
/** | |
* | |
* @param {HTMLElement} link | |
* @returns {string} | |
*/ | |
function getLink(link) { | |
const pageLink = link.getAttribute(`href`); | |
return `https://vk.com${pageLink || "id0"}`; | |
} | |
/** | |
* | |
* @param {number} ms | |
* @returns Promise | |
*/ | |
function sleep(ms) { | |
return new Promise((r) => setTimeout(r, ms)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment