这个代码,可以复制后,粘贴到Chrome控制台中运行。
使用方法:
1. 打开Chrome,登录微信公众号,切换到用户管理,然后按F12,打开Chrome开发模式控制台
2. 复制下面代码,到Chrome,控制台中粘贴下面代码
/*
* url 目标url
* arg 需要替换的参数名称
* arg_val 替换后的参数的值
* return url 参数替换后的url
*/
function changeURLArg(url,arg,arg_val){
var pattern=arg+'=([^&]*)';
var replaceText=arg+'='+arg_val;
if(url.match(pattern)){
var tmp='/('+ arg+'=)([^&]*)/gi';
tmp=url.replace(eval(tmp),replaceText);
return tmp;
}else{
if(url.match('[\?]')){
return url+'&'+replaceText;
}else{
return url+'?'+replaceText;
}
}
return url+'\n'+arg+'\n'+arg_val;
}
function getQueryString(url, name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
var r = new URL(url).search.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
return null;
}
function copyToClipboard(text) {
if (window.clipboardData && window.clipboardData.setData) {
// IE specific code path to prevent textarea being shown while dialog is visible.
return clipboardData.setData("Text", text);
} else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
var textarea = document.createElement("textarea");
textarea.textContent = text;
textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in MS Edge.
document.body.appendChild(textarea);
textarea.select();
try {
return document.execCommand("copy"); // Security exception may be thrown by some browsers.
} catch (ex) {
console.warn("Copy to clipboard failed.", ex);
return false;
} finally {
document.body.removeChild(textarea);
}
}
}
function getlist()
{
var xhr = new XMLHttpRequest();
var next_id = getQueryString(url, "begin_openid");
var backfoward = 0;
var begin_create_time = getQueryString(url, "begin_create_time");
while (true) {
url2 = changeURLArg(url, "begin_openid", next_id);
url2 = changeURLArg(url2, "backfoward", backfoward);
url2 = changeURLArg(url2, "begin_create_time", begin_create_time);
xhr.open("GET", url2, false);
xhr.withCredentials = true;
xhr.send();
list = eval('('+xhr.responseText+')');
if (!list.user_list) break;
if (!list.user_list.user_info_list) break;
for (i=0;i<list.user_list.user_info_list.length;i++) {
ids.push(list.user_list.user_info_list[i].user_openid);
// console.log(list.user_list.user_info_list[i].user_openid);
}
if (list.length < 20) break;
next_id = list.user_list.user_info_list[list.user_list.user_info_list.length-1].user_openid;
begin_create_time = list.user_list.user_info_list[list.user_list.user_info_list.length-1].user_create_time;
backfoward = 1;
}
}
// token为当前的token,userlist 以%7C即|分隔的微信 openid 列表
// 例如 blockUsers("807440279", "oqpDhwg8kgC54CbUxMopdXvp4kv8%7CoqpDhwmMjDHI80RUZls9***********")
function blockUsers(userlist)
{
var xhr = new XMLHttpRequest();
var token = getQueryString(url, "token");
xhr.open("POST", "https://mp.weixin.qq.com/cgi-bin/user_tag?action=set_black&source=3", false);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=UTF-8');
xhr.withCredentials = true;
xhr.send('token=' + token + '&lang=zh_CN&f=json&ajax=1&random=0.3122558239872879&user_openid_list=' + userlist);
alert(xhr.responseText);
}
function handlePaste (e) {
var clipboardData, pastedData;
// Stop data actually being pasted into div
e.stopPropagation();
e.preventDefault();
// Get pasted data via clipboard API
clipboardData = e.clipboardData || window.clipboardData;
pastedData = clipboardData.getData('Text');
// Do whatever with pasteddata
all_ids = pastedData.split("\r\n");
console.log("所有历史用户数:" + all_ids.length.toString());
}
// 合并数组去重
// MergeArray(all_ids, ids);
function MergeArray(arr1,arr2){
var _arr = new Array();
for(var i=0;i<arr1.length;i++){
_arr.push(arr1[i]);
}
for(var i=0;i<arr2.length;i++){
var flag = true;
for(var j=0;j<arr1.length;j++){
if(arr2[i]==arr1[j]){
flag=false;
break;
}
}
if(flag){
_arr.push(arr2[i]);
}
}
return _arr;
}
// 扩充数组,增加diff指令
// [1,2].diff([1]);//[2]
Array.prototype.diff = function(a) {
return this.filter(function(i) {return a.indexOf(i) < 0;});
};
document.addEventListener('paste', handlePaste);
var ids = []; // 今日所有用户ID
var all_ids = []; // 历史所有ID,粘贴得到
var url = ""; // 请求的URL
var blocklist=[]; // 黑名单
var new_all_ids = []; // 新的合并今日ID后的所有ID
function go() {
if (all_ids.length < 2000) { alert("没有复制历史ID列表!"); return ;}
ids = [];
getlist();
console.log("今日用户数:" + ids.length);
new_all_ids = MergeArray(all_ids, ids);
blocklist = new_all_ids.diff(ids);
blocklist.pop(); // 去掉最后的空行
if (confirm("真的要把 " + blocklist.length + " 个用户加入黑名单吗?")) {
blockUsers(blocklist.join("%7C"));
}
all_ids = new_all_ids;
}
// wx.cgiData
// 1. 复制所有代码到Chrome控制台,确定
// 2. 复制所有历史ID并到网页中按Ctrl + V
// 3. 运行
url = "https://mp.weixin.qq.com/cgi-bin/user_tag?action=get_user_list&groupid=-2&begin_openid=oqpDhw***********************&begin_create_time=1507652253&limit=20&offset=0&backfoward=0&token=1732434362&lang=zh_CN&f=json&ajax=1&random=0.35365447441536824"
go();
// 返回新的所有用户到剪切板,到Excel中粘贴保存即可
copyToClipboard(all_ids.toString().replace(/,/g, "\r\n"), true);