Skip to content

Instantly share code, notes, and snippets.

@polosaty
Created April 7, 2020 16:33
Show Gist options
  • Save polosaty/b696d9b591e512699c17748e5ec569f8 to your computer and use it in GitHub Desktop.
Save polosaty/b696d9b591e512699c17748e5ec569f8 to your computer and use it in GitHub Desktop.
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// async function getAsyncSubelementsByXpath(path, el) {
// const snapshot = document.evaluate(path, el, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
// while (node = result.iterateNext()) {
// yield node;
// }
// // return document.evaluate(path, el, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
// }
function getSubelementsByXpath(path, el) {
el = typeof el !== 'undefined' ? el : document;
const snapshot = document.evaluate(path, el, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
let res = [];
// while (node = snapshot.iterateNext()) {
// res.push(node);
// }
for (let i = 0, len = snapshot.snapshotLength ; i < len ; i++){
res.push(snapshot.snapshotItem(i));
}
return res;
}
function getFirstSubelementByXpath(path, el) {
el = typeof el !== 'undefined' ? el : document;
const snapshot = document.evaluate(path, el, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
return snapshot.singleNodeValue;
}
async function test() {
console.log(new Date())
await sleep(2000);
// [1,2,3,4,5].forEach((i)=>{
// console.log('start:', new Date(), i);
// await sleep(2000);
// console.log('end:', new Date(), i);
// })
// Uncaught SyntaxError: await is only valid in async function
for (let i = 0; i < 5; i++) {
console.log('start:', new Date(), i);
await sleep(2000);
console.log('end:', new Date(), i);
}
}
let item;
// let XX = $x;
let XX = getFirstSubelementByXpath;
let XXs = getSubelementsByXpath;
let Q = $;
async function unsubscribe(){
var exclude = ['captain_os', 'aleksandr.aksarin', 'rimetmn', 'electronics_workshop', 'vitylio', 'olgakishenya', 'i.am.urfin', 'dead_monarch'];
var lilist = XXs('div/ul/div/li', XX("//div[div/div/h1/div[contains(.,'Ваши подписки')]]"));
var scrolldiv = XX('div[2]', XX("//div[div/div/h1/div[contains(.,'Ваши подписки')]]"));
// lilist.forEach((item, indx, all) => {
for (let i = 0; i < lilist.length ; i++){
item = lilist[i];
console.log('scroll to item', item.offsetTop);
scrolldiv.scroll(0, item.offsetTop)
await sleep(500);
// if (indx > 100) return;
// XX('div/div[1]/div[2]/div[1]/a', item)[0]
let links = XXs('.//a[not(img)]', item);
// иногда аватарка кликабельна
if (exclude.indexOf(links[links.length - 1].innerText) != -1) {
continue
}
let time_to_sleep = 15 * 60 * 1000 + 1000 * Math.random();
console.log('sleep ' + time_to_sleep + 'ms starting');
await sleep(time_to_sleep);
console.log('sleep ' + time_to_sleep + 'ms ended');
// console.log(item)
var button = XX("div/div/button[contains(.,'Подписки')]", item);
if (typeof button == 'undefined' || !button) {
console.log(item)
continue
}
// console.log('scroll to button', button.offsetTop);
// scrolldiv.scroll(0, button.offsetTop) // всегда 4
console.log('sleep 1000ms');
await sleep(1000);
button.click()
console.log('sleep 1000ms');
await sleep(1000);
let unsubscrib_button = XX("//button[contains(., 'Отменить подписку')]")
if (typeof unsubscrib_button == 'undefined' || !unsubscrib_button){
console.log(item)
continue
}
unsubscrib_button.click()
}
}
unsubscribe()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment