Greasy Fork

Greasy Fork is available in English.

导出 Cookies

一键导出多种格式的 Cookies!

当前为 2024-11-14 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Export Cookies
// @name:zh-CN   导出 Cookies
// @namespace    http://tampermonkey.net/
// @version      0.1.0
// @description  Export cookies to various formats in one click!
// @description:zh-CN 一键导出多种格式的 Cookies!
// @author       PRO-2684
// @match        *://*/*
// @run-at       context-menu
// @license      gpl-3.0
// @grant        GM_registerMenuCommand
// @grant        GM.cookie
// @grant        GM.download
// ==/UserScript==

(function () {
    "use strict";
    const log = () => { };
    // const log = console.log.bind(console, `[${GM.info.script.name}]`);
    // Adapted from https://github.com/kairi003/Get-cookies.txt-LOCALLY/blob/master/src/modules/cookie_format.mjs
    function jsonToNetscapeMapper(cookies) {
        return cookies.map(({ domain, expirationDate, path, secure, name, value }) => {
            const includeSubDomain = !!domain?.startsWith('.');
            const expiry = expirationDate?.toFixed() ?? '0';
            const arr = [domain, includeSubDomain, path, secure, expiry, name, value];
            return arr.map((v) => (typeof v === 'boolean' ? v.toString().toUpperCase() : v));
        });
    };
    const formats = {
        netscape: {
            ext: '.txt',
            mimeType: 'text/plain',
            serializer: (cookies) => {
                const netscapeTable = jsonToNetscapeMapper(cookies);
                const text = [
                    '# Netscape HTTP Cookie File',
                    '# http://curl.haxx.se/rfc/cookie_spec.html',
                    '# This file was generated by Export Cookies! Edit at your own risk.',
                    '',
                    ...netscapeTable.map((row) => row.join('\t')),
                    '' // Add a new line at the end
                ].join('\n');
                return text;
            }
        },
        json: {
            ext: '.json',
            mimeType: 'application/json',
            serializer: JSON.stringify
        }
    };
    async function blobCookies(format) {
        const { mimeType, serializer } = formats[format];
        const cookies = await GM.cookie.list({});
        log("Extracted cookies:", cookies);
        const text = serializer(cookies);
        log("Serialized cookies:", text);
        const blob = new Blob([text], { type: mimeType });
        return URL.createObjectURL(blob);
    }
    const result = confirm('Please select the format you want to export the cookies in.\n\nPress OK to export in Netscape format (.txt), or press Cancel to export in JSON format (.json).');
    const format = result ? 'netscape' : 'json';
    blobCookies(format).then((blob) => {
        GM.download(blob, `cookies${formats[format].ext}`).then(() => {
            URL.revokeObjectURL(blob);
            console.log(`Cookies exported in ${format.toUpperCase()} format.`);
        }).catch((err) => {
            console.error('Failed to download the cookies.', err);
        });
    });
})();