Frodez / 清空知乎想法

// ==UserScript==
// @name         清空知乎想法
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  清空知乎想法
// @author       Frodez
// @license      MIT
// @match        *.zhihu.com/people/*
// @grant        GM_addStyle
// @grant        GM_xmlhttpRequest
// @run-at       document-start
// ==/UserScript==

/*
* 本脚本使用方法:允许本脚本使用,然后打开知乎个人主页动态页面,刷新即会弹出。
*
* 部分代码采用了https://openuserjs.org/scripts/leto/%E9%98%B2%E5%88%B7%E7%9F%A5%E4%B9%8E%E6%8F%90%E7%A4%BA
* 防刷知乎提示中的代码。在这里表示感谢。
*/

(function() {
    'use strict';

    // Your code here...
    // window.alert('stop');
    //删除速度和间隔
    let batch = 10;//每批删除数据的数量
    let batchInterval = 1500;//每批之间的间隔
    let interval = 120;//每批中删除每条数据的间隔,batch*interval必须小于batchInterval!!!
    let restTime = 100;//每次休息的间隔

    //初始化
    let inner = document.createElement('div');
    inner.id = 'leto-inner';
    let root = document.getElementById('root');
    root.classList.add('leto-blur');
    let myWindow = document.createElement('div');
    myWindow.id = 'leto-tip';
    myWindow.append(inner);
    document.body.appendChild(myWindow);

    //获取用户主页名字
    let userIndexName = window.location.href.split('/');
    userIndexName = userIndexName[userIndexName.length - 1];//用户主页名字

    //需要删除总数和当前完成删除的数量
    let totals = 0;
    let now = 0;
    let index = 0;
    //获取总数,启动流程
    GM_xmlhttpRequest({
        method: 'GET',
        url: 'https://zhihu.com/api/v4/members/' + userIndexName + '/pins?limit=1',//先获取一个看看总数
        onload: loadTotal
    });

    function loadTotal(response) {
        if(response.status !== 200){
            return;
        }
        let data = JSON.parse(response.response);
        totals = data.paging.totals;
        let tip = '你有' + totals + ' 条想法,需要把它们全部清除吗(由于知乎防脚本机制,可能会持续很长时间)';
        let yes = confirm(tip);
        if(yes) {
            //如果点击确定,创建屏幕开始删除流程
            createWindow();
            deletePins();
        }
    }

    //删除流程
    function deletePins() {
        if(index >= totals) {
            alert("删除完成!");
            myWindow.style.display = 'none';
            root.classList.remove('leto-blur');
            return;
        }
        batchDelete();
        let second = ((index / batch) * (batchInterval / 1000));
        if(second > restTime && (second % restTime) < (batchInterval / 1000)) {
            setTimeout(deletePins, batchInterval * 5);//休息一会,以免触发防脚本机制
        } else {
            setTimeout(deletePins, batchInterval);
        }
    }

    //调用API删除想法
    function batchDelete() {
        GM_xmlhttpRequest({
            method: 'GET',
            url: 'https://zhihu.com/api/v4/members/' + userIndexName + '/pins?limit=' + batch,
            onload: function(response) {
                if(response.status !== 200){
                    return;
                }
                index = index + batch;
                refreshText();
                let data = JSON.parse(response.response);
                let pins = data.data;
                for(let i = 0; i < pins.length; i++) {
                    let id = pins[i].id;//想法的id
                    setTimeout(()=>{
                        deleteOne(id);
                    }, i * interval);
                }
            }
        });
    }

    //更新删除进度
    function deleteOne(id) {
        GM_xmlhttpRequest({
            method: 'DELETE',
            url: 'https://zhihu.com/api/v4/pins/' + id,//删除
            onload: function(response) {
                if(response.status == 200){
                    now++;
                    refreshText();
                }
            }
        });
    }

    function refreshText() {
        inner.innerText = '总共' + totals + '条想法,遍历到第' + index + '条,现已删除' + now + '条';
    }

    //创建进度框体
    function createWindow() {
        let blurWindow = document.createElement('div');
        blurWindow.id = 'leto-blur';
        root.append(blurWindow);
        /*jshint multistr: true */
        GM_addStyle('\
                #leto-tip {\
                  position: fixed;\
                  width: 50%;\
                  height: 50%;\
                  top: 25%;\
                  left: 25%;\
                  z-index:1000;\
                  background: rgba(0,0,0,0.55);\
                  display: table;\
                }\
                #leto-close {\
                  background: #f6dd2a;\
                  color: #000;\
                  width: 40px;\
                  height: 40px;\
                  position: absolute;\
                  right: 2%;\
                }\
                .leto-blur {\
                  -webkit-filter: blur(10px);\
                  -moz-filter: blur(10px);\
                  -o-filter: blur(10px);\
                  -ms-filter: blur(10px);\
                }\
                #leto-inner {\
                  display: table-cell;\
                  vertical-align: middle;\
                  color: #fff;\
                  text-align: center;\
                  font-size: 60px;\
                }');
    }

})();