下面代码加入当前接口请求完成在请求下一个 ==UserScript== name Get Weibo Fans ID namespace httptampermonkeynet version 10 description Get Weibo Fans ID by API and save to text file author Your name m
(function() { 'use strict';
// Set loop interval in milliseconds
const LOOP_INTERVAL = 2000;
// Set maximum page number to fetch fans
const MAX_PAGE_NUMBER = 10;
// Set file name to save fans ID
const FILE_NAME = 'weibo-fans-id.txt';
// Get user ID from current URL
const uid = window.location.pathname.match(/(\d+)/)[1];
// Create a function to fetch fans data from API
const fetchFansData = async (page) => {
try {
const response = await fetch(`https://weibo.com/ajax/friendships/friends?relate=fans&page=${page}&uid=${uid}&type=fans&newFollowerCount=0`);
const data = await response.json();
return data.users.map(user => user.id);
} catch (error) {
console.error(error);
return [];
}
};
// Create an async function to loop through pages and save fans ID
const loopFetchFansID = async () => {
let page = 1;
let fansID = [];
while (page <= MAX_PAGE_NUMBER) {
const data = await fetchFansData(page);
if (data.length === 0) {
break;
}
fansID = fansID.concat(data);
page++;
await new Promise(resolve => setTimeout(resolve, LOOP_INTERVAL));
}
const blob = new Blob([fansID.join('\n')], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = FILE_NAME;
link.click();
if (page <= MAX_PAGE_NUMBER) {
loopFetchFansID();
}
};
// Execute the function when a button is clicked
const addButton = () => {
const button = document.createElement('button');
button.textContent = 'Get Weibo Fans ID';
button.style.position = 'fixed';
button.style.bottom = '20px';
button.style.right = '20px';
button.style.zIndex = '9999';
button.onclick = loopFetchFansID;
document.body.appendChild(button);
};
addButton();
})()
原文地址: https://www.cveoy.top/t/topic/e9Fc 著作权归作者所有。请勿转载和采集!