Cookie/Localstorage Manager

Import or export localstorage and cookies of any site

// ==UserScript==
// @name         Cookie/Localstorage Manager
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Import or export localstorage and cookies of any site
// @grant        GM_registerMenuCommand
// @grant        GM_setClipboard
// @include      *
// @license      MIT
// ==/UserScript==
function exportAll() {
    let data = {
        localStorage: {},
        cookies: {}
    };

    for (let i = 0; i < localStorage.length; i++) {
        const key = localStorage.key(i);
        data.localStorage[key] = localStorage.getItem(key);
    }

    const cookies = document.cookie.split('; ');
    cookies.forEach(cookie => {
        const [name, value] = cookie.split('=');
        const cookieName = name.trim();
        const cookieValue = value.trim();

        const cookieAttributes = document.cookie.split('; ').find(c => c.startsWith(cookieName));
        const attributes = cookieAttributes ? cookieAttributes.split('; ').slice(1) : [];

        let expires = attributes.find(attr => attr.trim().toLowerCase().startsWith('expires='));

        data.cookies[cookieName] = {
            value: cookieValue,
            expires: expires ? expires.split('=')[1] : null
        };
    });

    const dataString = JSON.stringify(data);
    GM_setClipboard(dataString);
    alert('localStorage and cookies copied to clipboard!');
}

function importAll() {
    const dataString = prompt('Paste the stringified JSON data to import into localStorage and cookies:');
    if (dataString) {
        try {
            const data = JSON.parse(dataString);

            if (data.localStorage) {
                Object.entries(data.localStorage).forEach(([k, v]) => {
                    localStorage.setItem(k, v);
                });
            }

            if (data.cookies) {
                Object.entries(data.cookies).forEach(([k, { value, expires }]) => {
                    let cookieString = `${k}=${value}; path=/`;
                    if (expires) {
                        const expirationDate = new Date(expires);
                        cookieString += `; expires=${expirationDate.toUTCString()}`;
                    }
                    document.cookie = cookieString;
                });
            }

            alert('localStorage and cookies imported successfully!');
        } catch (e) {
            alert('Failed to import data. Please make sure the input is valid JSON. Error: ' + e.message);
            console.error(e);
        }
    } else {
        alert('No data was entered.');
    }
}

GM_registerMenuCommand("Copy localStorage and Cookies to Clipboard", exportAll);
GM_registerMenuCommand("Import localStorage and Cookies from Clipboard", importAll);