Export Cookies

Export cookies to various formats in one click!

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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);
        });
    });
})();