GreasyFork优化

自动登录账号、快捷寻找自己库被其他脚本引用、更新自己的脚本列表、库、优化图片浏览、美化页面、Markdown复制按钮

< Feedback on GreasyFork优化

Review: Good - script works

§
Posted: 25.09.2024.

好同志,求添加脚本的设置项同步到GitHub 。不然每次都需要自己复制到脚本管理器里面。

const panelData = GM_getValue("GM_Panel", {})
const jsonData = JSON.stringify(panelData, null, 2)
const GistManager = {
    githubToken: null,
    description: null,
    init: function (token, description) {
        this.githubToken = token
        this.description = description || "Default Gist Description"
    },
    // 查找 Gist 根据描述
    findGistByDescription: function (callback) {
        const url = 'https://api.github.com/gists'
        GM_xmlhttpRequest({
            method: "GET",
            url: url,
            headers: {
                "Authorization": `token ${this.githubToken}`,
                "Content-Type": "application/json"
            },
            onload: function (response) {
                if (response.status === 200) {
                    const gists = JSON.parse(response.responseText)
                    for (let gist of gists) {
                        if (gist.description === GistManager.description) {
                            console.log("找到匹配的 Gist:", gist.html_url)
                            return callback(gist.id)
                        }
                    }
                    callback(null)
                } else {
                    console.error("获取 Gist 列表失败:", response.responseText)
                    callback(null)
                }
            }
        })
    },
    // 上传或更新 Gist
    uploadToGist: function (filename, content) {
        this.findGistByDescription((gistId) => {
            if (gistId) {
                this.updateGist(gistId, filename, content)
            } else {
                this.createGist(filename, content)
            }
        })
    },
    // 创建新的 Gist
    createGist: function (filename, content) {
        const url = 'https://api.github.com/gists'
        const data = {
            "description": this.description,
            "public": true,
            "files": {
                [filename]: {
                    "content": content
                }
            }
        }
        GM_xmlhttpRequest({
            method: "POST",
            url: url,
            headers: {
                "Authorization": `token ${this.githubToken}`,
                "Content-Type": "application/json"
            },
            data: JSON.stringify(data),
            onload: function (response) {
                if (response.status === 201) {
                    const responseData = JSON.parse(response.responseText)
                    console.log("Gist 创建成功:", responseData.html_url)
                } else {
                    console.error("Gist 创建失败:", response.responseText)
                }
            }
        })
    },
    downloadGistAsJson: function (filename) {
        // 查找与描述匹配的 Gist
        this.findGistByDescription((gistId) => {
            if (!gistId) {
                console.error("未找到匹配的 Gist")
                return
            }
            const url = `https://api.github.com/gists/${gistId}`
            GM_xmlhttpRequest({
                method: "GET",
                url: url,
                headers: {
                    "Authorization": `token ${this.githubToken}`,
                    "Content-Type": "application/json"
                },
                onload: function (response) {
                    if (response.status === 200) {
                        const gistData = JSON.parse(response.responseText)
                        const fileContent = gistData.files[filename].content
                        const parsedJson = JSON.parse(fileContent)
                        GM_setValue("GM_Panel", parsedJson)
                    } else {
                        console.error("下载 Gist 失败:", response.responseText)
                    }
                }
            })
        })
    },
    // 更新已有的 Gist
    updateGist: function (gistId, filename, content) {
        const url = `https://api.github.com/gists/${gistId}`
        const data = {
            "files": {
                [filename]: {
                    "content": content
                }
            }
        }
        GM_xmlhttpRequest({
            method: "PATCH",
            url: url,
            headers: {
                "Authorization": `token ${this.githubToken}`,
                "Content-Type": "application/json"
            },
            data: JSON.stringify(data),
            onload: function (response) {
                if (response.status === 200) {
                } else {
                    console.error("Gist 更新失败:", response.responseText)
                }
            }
        })
    }
}
const githubToken = ''
const description = "GreasyFork优化设置备份"
GistManager.init(githubToken, description)
const filename = "GM_Panel.json"
GistManager.uploadToGist(filename, jsonData)
GistManager.downloadGistAsJson(filename)
WhiteSevsAuthor
§
Posted: 28.09.2024.

这种脚本数据同步功能建议给脚本管理器提建议,这不该是脚本做的事

§
Posted: 28.09.2024.

这种脚本数据同步功能建议给脚本管理器提建议,这不该是脚本做的事

好的

Post reply

Sign in to post a reply.