吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3677|回复: 14
收起左侧

[其他原创] 【油猴脚本】自用Alist快速添加云盘分享存储脚本

[复制链接]
jeege 发表于 2023-1-6 15:59
本帖最后由 jeege 于 2023-1-15 08:22 编辑

alist添加存储的时候,每次都要来回切换复制粘贴id,挺费劲的,写了个简易的脚本方便操作,分享一下完整的脚本。
添加脚本的时候,复制下面的代码,把前面四个变量替换成自己的就可以了。(手机上Via浏览器用起来还是很方便的)

// ==UserScript==
// @name         快速上传Alist
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  简单就完了
// @AuThor       52pojie
// @match       *://*.aliyundrive.com/*
// @Icon         https://www.google.com/s2/favicons?sz=64&domain=aliyundrive.com
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// @grant        GM_log
// @grant        GM_getValue
// @grant        GM_setValue
// @connect      *
// @run-at document-end
// ==/UserScript==

(function() {
    'use strict';
    const baseUrl = 'http://xxxxxxxx:5244'
    const adminUser = '管理员用户名'
    const adminPwd = '管理员密码'
    const refresh_token = '自行获取云盘的refresh_token(可以通过mt管理器查看日志)'
    const headerPost = async (url, data, headers, type) => {
        return new Promise((resolve, reject) => {
            let option = {
                method: "POST",
                url: url,
                headers: headers,
                data: data,
                responseType: type || 'json',
                onload: (res) => {
                    if (type === 'blob') {
                        resolve(res)
                    } else {
                        const _re = res.response || res
                        .responseText
                        typeof _re === 'string' ? resolve(JSON.parse(_re)) : resolve(_re)
                        }
                },
                onerror: (err) => {
                    reject(err);
                },
            }
            try {
                let req = GM_xmlhttpRequest(option);
            } catch (error) {
                console.error(error);
            }
        });
    };
    async function addStorage({mount_path, share_id, share_pwd = "", root_folder_id = "root", order_by = "", order_direction = ""}){
        return await headerPost(baseUrl + '/api/admin/storage/create',JSON.stringify({
            mount_path: mount_path,
            order: 0,
            remark: '',
            cache_expiration: 30, // 缓存过期时间(分钟)
            web_proxy: false, // 是否开启web代{过}{滤}理
            webdav_policy: "302_redirect", // Webdav策略: 302重定向
            down_proxy_url: "",
            extract_folder: "",
            driver: "AliyundriveShare",
            addition: JSON.stringify({
                refresh_token: refresh_token,
                share_id,
                share_pwd,
                root_folder_id,
                order_by,
                order_direction
            })
        }), {
            "Authorization": GM_getValue('token'),
            "Content-Type": "application/json;charset=UTF-8"
        })
    };
    async function login() {
        const res = await headerPost(baseUrl + '/api/auth/login', JSON.stringify({username: adminUser, password: adminPwd, "otp_code":""}), {"Content-Type": "application/json;charset=UTF-8"})
        if (res.code === 200) {
            GM_setValue('token', res.data.token)
        } else {
            toast("登录失败")
        }
    }
    function toast(msg) {
        const t = (() => {
            if (document.querySelector('#J_single_toast')) {
                return document.querySelector('#J_single_toast')
            } else {
                const _t = document.createElement('div')
                _t.id = 'J_single_toast'
                document.body.appendChild(_t)
                return _t
            }
        })()
        t.innerText = msg
        t.classList.toggle('show')
        setTimeout(() => {t.classList.toggle('show')}, 1500)
    }
    function createCustomDom() {
        let mount_path = `/xx云盘分享_${+new Date}`
        const cBtn = document.createElement('div')
        cBtn.innerText = "创建Alist存储"
        cBtn.className = "cBtn"
        document.body.appendChild(cBtn)
        const dialog = document.createElement('div')
        dialog.className = "mount-path-dialog"
        dialog.innerHTML = `
            <div class="mask"></div>
            <div class="input-wrap">
                <div class="input-item">
                    <p>请输入Alist挂载路径:</p>
                    <input name="mount_path" value=${mount_path}>
                </div>
                <div class="input-item">
                    <p>请输入分享密码:</p>
                    <input name="share_pwd">
                </div>
                <div class="btn-group">
                    <div class="cancel btn">取消</div>
                    <div class="confirm btn">确认</div>
                </div>
            </div>`
        document.body.appendChild(dialog)
        cBtn.addEventListener('click', () => {
            dialog.style.display = 'block';
            dialog.querySelector('[name="mount_path"]').focus();
            dialog.querySelector('[name="mount_path"]').setSelectionRange(1, 999);
        })
        dialog.addEventListener('click', function(e){
            if (Array.from(dialog.querySelectorAll('.mask, .cancel')).includes(e.target)) {
                closeDialog()
            }
            if (e.target === dialog.querySelector('.confirm')) {
                const arr = window.location.pathname.split('/')
                addStorage({
                    mount_path: dialog.querySelector('[name="mount_path"]').value,
                    share_pwd: dialog.querySelector('[name="share_pwd"]').value,
                    share_id: arr[2],
                    root_folder_id: arr[4]
                }).then(res => {
                    if (res.code === 200) {
                        dialog.style.display = 'none'
                        toast('创建成功')
                    } else {
                        toast(res.message)
                    }
                })
            }
        })
        function closeDialog() {
            dialog.style.display = 'none'
        }
    }
    async function init() {
        GM_addStyle(`
        #J_single_toast {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            z-index: 1001;
            display: none;
            background: rgba(0,0,0,0.8);
            color: var(--basic_white);
            padding: 10px;
            max-width: 300px;
            border-radius: 4px;
        }
         #J_single_toast.show {
            display: block;
        }
        .cBtn {
            position: fixed;
            left: 50%;
            transform: translateX(-50%);
            bottom: 80px;
            z-index: 999;
            bottom: calc(constant(safe-area-inset-bottom) + 80px);
            bottom: calc(env(safe-area-inset-bottom) + 80px);
            display: inline-flex;
            align-items: center;
            justify-content: center;
            color: var(--basic_white);
            background: var(--theme_primary);
            height: 44px;
            padding: 0 16px;
            font-size: 16px;
            border-radius: 22px;
        }
        .mount-path-dialog {
            position: fixed;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            z-index: 1000;
            display: none;
            margin: auto;
        }
        .mount-path-dialog .mask {
            position: absolute;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
            background: rgba(0,0,0,0.6)
        }
        .mount-path-dialog .input-wrap {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            width: 300px;
            height: 280px;
            background: #fff;
            border-radius: 10px;
            box-sizing: boder-box;
        }
        .mount-path-dialog .input-wrap .input-item {
            margin-bottom: 8px;
        }
        .mount-path-dialog .input-wrap p {
            font-size: 14px;
            margin-bottom: 5px;
        }
        .mount-path-dialog .input-wrap input{
            display: block;
            width: 240px;
            height: 40px;
            margin: 0 auto 15px;
            font-size: 12px;
            border: 1px solid #ccc;
            padding-left: 8px;
            border-radius: 4px;
        }
        .mount-path-dialog .input-wrap .btn-group {
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 260px;
            margin: 0 auto;
        }
        .mount-path-dialog .input-wrap .btn-group .btn {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            flex: 1;
            margin: 0 10px;
            height: 40px;
            background: var(--theme_primary);
            color: var(--basic_white);
            font-size: 16px;
            border-radius: 4px;
        }
        .mount-path-dialog .input-wrap .btn-group .cancel {
            background: #fff;
            color: #333;
            border: 1px solid #ccc;
        }
        `)
        await login()
        createCustomDom()
    }
    window.addEventListener ("load", init);

})();

效果如下:
5331672991787_.pic.jpg
5341672991787_.pic.jpg
5351672991787_.pic.jpg
5361672991788_.pic.jpg

免费评分

参与人数 5吾爱币 +5 热心值 +3 收起 理由
p00y + 1 非常好用,要是能把挂载名自动默认成分享路径名就更好了
极客玩家 + 1 + 1 很实用,谢谢
xucheng_1 + 1 谢谢@Thanks!
coverme + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
wanfon + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| jeege 发表于 2023-1-15 08:13
1655986984 发表于 2023-1-11 20:49
iOS不行吗
via alook 自带浏览器都不行

不显示应该是登录失败了,需要先登录alist获取token,你试试浏览器输入地址,看看能不能访问alist页面
 楼主| jeege 发表于 2023-1-15 08:14
魔术师_ 发表于 2023-1-12 11:04
为啥电脑版添加了脚本就显示不了呢

登录失败了吧,地址还有用户名密码确认是否正确
深海老鱼 发表于 2023-1-6 20:27
wushengli 发表于 2023-1-6 20:37
非常实用的工具感谢分享!
1655986984 发表于 2023-1-11 20:49
iOS不行吗
via alook 自带浏览器都不行


魔术师_ 发表于 2023-1-12 11:04
为啥电脑版添加了脚本就显示不了呢
1655986984 发表于 2023-1-12 12:00
魔术师_ 发表于 2023-1-12 11:04
为啥电脑版添加了脚本就显示不了呢

你手机显示吗
魔术师_ 发表于 2023-1-13 09:48

还没试手机。。想电脑用
1655986984 发表于 2023-1-16 11:27
jeege 发表于 2023-1-15 08:14
登录失败了吧,地址还有用户名密码确认是否正确

正确 PC端可以 手机不行
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-2 18:03

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表