Export cookies to various formats in one click!
// ==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 includ###bDomain = !!domain?.startsWith('.'); const expiry = expirationDate?.toFixed() ?? '0'; const arr = [domain, includ###bDomain, 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 r###lt = 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 = r###lt ? '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); }); }); })();