115Rename

115改名称(根据现有的文件名<番号>查询并修改文件名),需要翻墙(查询需要)

当前为 2020-02-10 提交的版本,查看 最新版本

// ==UserScript==
// @name                115Rename
// @namespace           http://tampermonkey.net/
// @version             0.1
// @description         115改名称(根据现有的文件名<番号>查询并修改文件名),需要翻墙(查询需要)
// @author              db117
// @include             https://115.com/home/userhome
// @include             https://115.com/?*mode=wangpan*
// @domain              javbus.com
// @grant               GM_xmlhttpRequest
// ==/UserScript==

(function () {
    'use strict';
    // 数据list
    let item_list;
    let ifr;


    if (window.location.href === "https://115.com/home/userhome")
        window.location = "https://115.com/?mode=wangpan";
    else {
        ifr = $("iframe[style='position: absolute; top: 0px;']");
        ifr.load(
            function () {
                item_list = ifr.contents().find("body").find("div#js_data_list");
                item_list.mouseenter(
                    function () {
                        if ($("div.exph-loader").css("display") === "none" && !(item_list.find("div#isload").length)) {
                            // 添加按钮
                            addButton();
                        }
                    }
                );
            }
        );
    }

    // 添加按钮
    function addButton() {
        let rename_list = `
            <li id="rename_list">
                <a id="rename_a" class="mark" href="javascript:;">改名</a>
            </li>
        `;

        ifr.contents().find("body").mouseup(
            function (event) {
                if (event.button == 2) {
                    setTimeout(function () {
                        if ($("li#rename_list").length === 0) {
                            $("div#js_float_content").find("li[val='open_dir']").after(rename_list);
                            $("a#rename_a").click(
                                function () {
                                    rename();
                                }
                            );
                            ifr.contents().find("body").unbind("mouseup");
                            console.log("添加按钮")
                        }
                    }, 50);
                }
            }
        )
    }

    // 该名称
    function rename() {
        // 获取所有已选择的文件
        let list = item_list.find("li.selected");
        list.each(function (index, v) {
            let $item = $(v);
            // 原文件名称
            let file_name = $item.attr("title");
            // 文件类型
            let file_type = $item.attr("file_type");

            // 文件id
            let fid;
            if (file_type === "0") {
                // 文件夹
                fid = $item.attr("cate_id");
            } else {
                // 文件
                fid = $item.attr("file_id");
            }

            if (fid && file_name) {
                let fh = getVideoCode(file_name);
                if (fh) {
                    getVideoName(fid, fh);
                }
            }
        });
    }

    /**
     * 请求115接口
     * @param id 文件id
     * @param name 要修改的名称
     */
    function send_115(id, name) {
        let file_name = stringStandard(name);
        $.post("https://webapi.115.com/files/edit", {
                fid: id,
                file_name: file_name
            },
            function (data, status) {
                let result = JSON.parse(data);
                if (!result.state) {
                    console.log("请求115接口异常: " + unescape(result.error
                        .replace(/\\(u[0-9a-fA-F]{4})/gm, '%$1')));
                } else {
                    console.log("修改文件名称,id:" + id, "name:" + file_name);
                }
            }
        );
    }

    /**
     * 115名称不接受(\/:*?\"<>|)
     * @param name
     */
    function stringStandard(name) {
        return name.replace(/\\/g, "")
            .replace(/\//g, " ")
            .replace(/:/g, " ")
            .replace(/\?/g, " ")
            .replace(/"/g, " ")
            .replace(/</g, " ")
            .replace(/>/g, " ")
            .replace(/\|/g, "")
            .replace(/\*/g, " ");
    }

    /**
     * 获取番号
     * @param fid 文件id
     * @param fh 番号
     */
    function getVideoName(fid, fh) {
        GM_xmlhttpRequest({
            method: "GET",
            url: "https://www.javbus.com/search/" + fh,
            onload: xhr => {
                // 匹配标题
                let match = xhr.responseText.match("<span>(.*?)<br />");
                if (match) {
                    let title = match.pop();
                    if (title) {
                        send_115(fid, fh + " " + title);
                    }
                } else {
                    getUncensored(fid, fh);
                }
            }
        })
    }

    /**
     * 获取无码番号
     * @param fid 文件id
     * @param fh 番号
     */
    function getUncensored(fid, fh) {
        GM_xmlhttpRequest({
            method: "GET",
            url: "https://www.javbus.com/uncensored/search/" + fh,
            synchronous: true,
            onload: xhr => {
                // 匹配标题
                let match = xhr.responseText.match("<span>(.*?)<br />");
                if (match) {
                    let title = match.pop();
                    if (title) {
                        send_115(fid, fh + " " + title);
                    }
                }
            }
        })
    }

    // 获取番号
    function getVideoCode(title) {
        title = title.replace("SIS001", "");
        let t = title.match(/[A-Za-z]+-\d+/);
        if (!t) {
            t = title.match(/heyzo[\-_]?\d{4}/);
        }
        if (!t) {
            t = title.match(/\d{6}[\-_]\d{3}/);
        }
        if (!t) {
            t = title.match(/[A-Za-z]+\d+/);
        }
        if (!t) {
            t = title.match(/[A-Za-z]+_\d+/);
        }
        if (t) {
            t = t.toString().toUpperCase();
            console.log("找到番号:" + t);
        }
        return t;
    }
})();