Cookie/Localstorage Manager

Import or export localstorage and cookies of any site

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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);