微博一键清空

微博一键清空[动态|关注|粉丝|收藏]

  1. // ==UserScript==
  2. // @name 微博一键清空
  3. // @namespace https://github.com/hbc007/weibo-cleaner
  4. // @version 1.0
  5. // @description 微博一键清空[动态|关注|粉丝|收藏]
  6. // @author hbc007
  7. // @license GNU GPLv3
  8. // @match https://weibo.com
  9. // @match https://weibo.com/*
  10. // @icon https://weibo.com/favicon.ico
  11. // @grant GM_xmlhttpRequest
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. 'use strict';
  16.  
  17. var GlobalInfo = null;
  18. var cookie = null;
  19. var wbVersion = null;
  20.  
  21. function updateInfo(info) {
  22. if (GlobalInfo == null) {
  23. return;
  24. }
  25. console.log(info);
  26. GlobalInfo.innerText = info;
  27. }
  28. function getXSRFToken(cookieStr) {
  29. var cookieItems = cookieStr.split('; ');
  30. for (var i = 0, len = cookieItems.length; i < len; i++) {
  31. var item = cookieItems[i];
  32. var itemArr = item.split('=');
  33. if (itemArr[0] === 'XSRF-TOKEN') {
  34. return itemArr[1];
  35. }
  36. }
  37. }
  38. function getWB(uid, callback, last_id = 0) {
  39. var xsrfToken = getXSRFToken(cookie);
  40. GM_xmlhttpRequest(
  41. {
  42. method: "get",
  43. url: "/ajax/statuses/mymblog?feature=0&uid=" + uid + "&since_id=" + last_id,
  44. headers: {
  45. "Accept": "application/json",
  46. "Cookie": cookie,
  47. "x-xsrf-token": xsrfToken
  48. },
  49. onload: (res) => {
  50. if (res.status === 200) {
  51. var data = JSON.parse(res.responseText);
  52. var wblist = data.data.list;
  53. if (wblist == null || wblist.length == 0) {
  54. location.reload();
  55. return;
  56. }
  57. for (var i = 0, len = wblist.length, id = 0; i < len; i++) {
  58. id = wblist[i].id;
  59. last_id = id;
  60. callback(id);
  61. }
  62. getWB(uid, callback, last_id);
  63. } else {
  64. updateInfo("请求失败:" + res.status + "请手动刷新页面,稍后再试");
  65. }
  66. },
  67. }
  68. );
  69. }
  70. function destroyWB(id) {
  71. var xsrfToken = getXSRFToken(cookie);
  72. GM_xmlhttpRequest(
  73. {
  74. method: "post",
  75. url: "/ajax/statuses/destroy",
  76. data: "{\"id\":\"" + id + "\"}",
  77. headers: {
  78. "content-type": "application/json; charset=utf-8",
  79. "Cookie": cookie,
  80. "accept": "application/json, text/plain, */*",
  81. "x-xsrf-token": xsrfToken
  82. },
  83. onload: (res) => {
  84. if (res.status === 200) {
  85. updateInfo("删除成功:" + id);
  86. } else {
  87. updateInfo("请求失败[" + res.status + "]:请手动刷新页面,稍后再试");
  88. }
  89. },
  90. }
  91. );
  92. }
  93. function getFollows(uid, callback, page = 1) {
  94. if (wbVersion != 6) {
  95. alert("由于微博限制,“清空关注”功能需切换到“旧版”微博,请在设置中切换");
  96. return -1;
  97. }
  98. var xsrfToken = getXSRFToken(cookie);
  99. GM_xmlhttpRequest(
  100. {
  101. method: "get",
  102. url: "https://weibo.com/p/1005053026435753/myfollow?Pl_Official_RelationMyfollow__88_page=" + page,
  103. headers: {
  104. "Accept": "application/json",
  105. "Cookie": cookie,
  106. "x-xsrf-token": xsrfToken
  107. },
  108. onload: (res) => {
  109. if (res.status === 200) {
  110. var data = res.responseText;
  111. var uidset = new Set(data.match(/(?<=uid=)([a-z0-9:]+)/g));
  112. uidset.delete(uid)
  113. if (uidset.size == 0) {
  114. location.reload();
  115. return;
  116. }
  117. for (var id of uidset) {
  118. callback(id);
  119. }
  120. page += 1;
  121. setTimeout(() => {
  122. getFollows(uid, callback, page);
  123. }, 100);
  124. } else {
  125. updateInfo("请求失败[" + res.status + "]:请手动刷新页面,稍后再试");
  126. }
  127. },
  128. }
  129. );
  130. }
  131.  
  132. function destroyFollow(id) {
  133. var xsrfToken = getXSRFToken(cookie);
  134. GM_xmlhttpRequest(
  135. {
  136. method: "post",
  137. url: "/aj/f/unfollow?ajwvr=6&__rnd=" + new Date().getTime(),
  138. data: "refer_sort=relationManage&location=page_100505_myfollow&refer_flag=unfollow&uid=" + encodeURIComponent(id),
  139. headers: {
  140. "content-type": "application/x-www-form-urlencoded",
  141. "Cookie": cookie,
  142. "accept": "*/*",
  143. "origin": "https://weibo.com",
  144. "sec-fetch-site": "same-origin",
  145. "x-xsrf-token": xsrfToken,
  146. "referer": window.location.href,
  147. },
  148. onload: (res) => {
  149. if (res.status === 200) {
  150. updateInfo("删除成功:" + id);
  151. } else {
  152. updateInfo("请求失败[" + res.status + "]:请手动刷新页面,稍后再试");
  153. }
  154. },
  155. }
  156. );
  157. }
  158.  
  159.  
  160. function getFans(uid, callback, page = 1) {
  161. var xsrfToken = getXSRFToken(cookie);
  162. GM_xmlhttpRequest(
  163. {
  164. method: "get",
  165. url: "https://weibo.com/ajax/friendships/friends?uid=" + uid + "&relate=fans&count=20&page=" + page + "&type=fans&fansSortType=fansCount",
  166. headers: {
  167. "Accept": "application/json",
  168. "Cookie": cookie,
  169. "x-xsrf-token": xsrfToken
  170. },
  171. onload: (res) => {
  172. if (res.status === 200) {
  173. var data = JSON.parse(res.responseText);
  174. var userlist = data.users;
  175. if (userlist == null || userlist.length == 0) {
  176. location.reload();
  177. return;
  178. }
  179. for (var i = 0, len = userlist.length, id = 0; i < len; i++) {
  180. id = userlist[i].id;
  181. callback(id);
  182. }
  183. page += 1;
  184. setTimeout(() => { getFans(uid, callback, page) }, 100);
  185. } else {
  186. updateInfo("请求失败[" + res.status + "]:请手动刷新页面,稍后再试");
  187. }
  188. },
  189. }
  190. );
  191. }
  192.  
  193. function destroyFan(id) {
  194. var xsrfToken = getXSRFToken(cookie);
  195. GM_xmlhttpRequest(
  196. {
  197. method: "post",
  198. url: "/ajax/profile/destroyFollowers",
  199. data: "{\"uid\":\"" + id + "\"}",
  200. headers: {
  201. "content-type": "application/json; charset=utf-8",
  202. "Cookie": cookie,
  203. "accept": "application/json, text/plain, */*",
  204. "x-xsrf-token": xsrfToken
  205. },
  206. onload: (res) => {
  207. if (res.status === 200) {
  208. updateInfo("删除成功:" + id);
  209. } else {
  210. updateInfo("请求失败[" + res.status + "]:请手动刷新页面,稍后再试");
  211. }
  212. },
  213. }
  214. );
  215. }
  216.  
  217. function getFavorite(uid, callback, page = 1) {
  218. var xsrfToken = getXSRFToken(cookie);
  219. GM_xmlhttpRequest(
  220. {
  221. method: "get",
  222. url: "/ajax/favorites/all_fav?uid=" + uid + "&page=" + page,
  223. headers: {
  224. "Accept": "application/json",
  225. "Cookie": cookie,
  226. "x-xsrf-token": xsrfToken
  227. },
  228. onload: (res) => {
  229. if (res.status === 200) {
  230. var data = JSON.parse(res.responseText);
  231. var favlist = data.data;
  232. if (favlist.length == 0) {
  233. location.reload();
  234. return;
  235. }
  236. for (var i = 0, len = favlist.length, id = 0; i < len; i++) {
  237. id = favlist[i].id;
  238. callback(id);
  239. }
  240. page += 1;
  241. setTimeout(() => { getFavorite(uid, callback, page) }, 100);
  242. } else {
  243. updateInfo("请求失败[" + res.status + "]:请手动刷新页面,稍后再试");
  244. }
  245. },
  246. }
  247. );
  248. }
  249.  
  250. function destroyFavorite(id) {
  251. var xsrfToken = getXSRFToken(cookie);
  252. GM_xmlhttpRequest(
  253. {
  254. method: "post",
  255. url: "/ajax/statuses/destoryFavorites",
  256. data: "{\"id\":\"" + id + "\"}",
  257. headers: {
  258. "content-type": "application/json; charset=utf-8",
  259. "Cookie": cookie,
  260. "accept": "application/json, text/plain, */*",
  261. "x-xsrf-token": xsrfToken
  262. },
  263. onload: (res) => {
  264. if (res.status === 200) {
  265. updateInfo("删除成功:" + id);
  266. } else {
  267. updateInfo("请求失败[" + res.status + "]:请手动刷新页面,稍后再试");
  268. }
  269. },
  270. }
  271. );
  272. }
  273.  
  274. function getUid() {
  275. var url, uid = null;
  276. try {
  277. url = document.getElementsByClassName('woo-box-flex woo-tab-nav')[0].lastElementChild.href;
  278. uid = url.match(/(?<=u\/)\d+/)[0];
  279. wbVersion = 7;
  280. } catch (e) {
  281. console.log('error: not new weibo')
  282. }
  283. try {
  284. url = document.getElementsByClassName('gn_nav_list')[0].lastElementChild.firstElementChild.href;
  285. uid = url.match(/(?<=weibo.com\/)\d+/)[0];
  286. wbVersion = 6;
  287. } catch (e) {
  288. console.log('error: not old weibo')
  289. }
  290. console.log('wbVersion', wbVersion);
  291. return uid;
  292. }
  293. function init() {
  294. cookie = document.cookie;
  295. var uid = getUid();
  296. let mask = document.createElement('div');
  297. mask.style.color = 'white';
  298. mask.style.position = 'fixed';
  299. mask.style.top = '0';
  300. mask.style.left = '0';
  301. mask.style.width = '100%';
  302. mask.style.height = '100%';
  303. mask.style.background = 'rgba(128,128,128,0.5)';
  304. mask.style.zIndex = '9999';
  305. mask.style.lineHeight = '100vh';
  306. mask.style.fontSize = '50px';
  307. mask.style.textAlign = 'center';
  308. mask.style.textShadow = '0 0 10px black';
  309. mask.style.display = 'none';
  310. mask.innerText = '正在清理中,请勿刷新页面';
  311. GlobalInfo = document.createElement('div');
  312. GlobalInfo.style.position = 'fixed';
  313. GlobalInfo.style.bottom = '10%';
  314. GlobalInfo.style.left = '0';
  315. GlobalInfo.style.width = '100%';
  316. GlobalInfo.style.textAlign = 'center';
  317. GlobalInfo.style.zIndex = '10000';
  318. GlobalInfo.style.textShadow = '0 0 15px blue';
  319. GlobalInfo.style.fontSize = '16px';
  320. GlobalInfo.style.color = 'white';
  321.  
  322. function showMask() {
  323. mask.style.display = 'block';
  324. }
  325.  
  326. let container = document.createElement("div");
  327. container.style.position = "fixed";
  328. container.style.bottom = "0";
  329. container.style.left = "0";
  330. container.style.zIndex = "9999";
  331.  
  332. let clrWBbtn = document.createElement("button");
  333. clrWBbtn.innerHTML = "清空微博";
  334. clrWBbtn.onclick = () => {
  335. getWB(uid, destroyWB);
  336. showMask();
  337. };
  338. container.append(clrWBbtn);
  339. container.append(document.createElement("br"));
  340.  
  341. let clrFollowBtn = document.createElement("button");
  342. clrFollowBtn.innerHTML = "清空关注";
  343. clrFollowBtn.onclick = () => {
  344. if (getFollows(uid, destroyFollow) != -1) {
  345. showMask();
  346. }
  347. };
  348. container.append(clrFollowBtn);
  349. container.append(document.createElement("br"));
  350.  
  351. let clrFanBtn = document.createElement("button");
  352. clrFanBtn.innerHTML = "清空粉丝";
  353. clrFanBtn.onclick = () => {
  354. getFans(uid, destroyFan);
  355. showMask();
  356. };
  357. container.append(clrFanBtn);
  358. container.append(document.createElement("br"));
  359.  
  360. let clrFavBtn = document.createElement("button");
  361. clrFavBtn.innerHTML = "清空收藏";
  362. clrFavBtn.onclick = () => {
  363. getFavorite(uid, destroyFavorite);
  364. showMask();
  365. };
  366. container.append(clrFavBtn);
  367.  
  368. document.body.append(container);
  369. document.body.append(mask);
  370. document.body.append(GlobalInfo);
  371. }
  372. window.addEventListener('load', () => { init(); });
  373. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址