LocalStorage Export/Import

Add Tampermonkey menu commands to export and import localStorage data from any website!

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         LocalStorage Export/Import
// @author       adityash4rma
// @version      2.0
// @namespace    https://greasyfork.org/en/users/1450540-adityash4rma
// @description  Add Tampermonkey menu commands to export and import localStorage data from any website!
// @icon         https://cdn-icons-png.flaticon.com/512/18091/18091224.png
// @match        *://*/*
// @grant        GM_registerMenuCommand
// @license   MIT
// ==/UserScript==

(function() {
    'use strict';
    // This project is inspired by: https://gist.github.com/shufengh/e331c3d9a91d142dc0786ba6ddc95872
    function exportLocalStorage() {
        var obj = JSON.stringify(localStorage, null, 4);
        var vLink = document.createElement('a');
        var vBlob = new Blob([obj], {type: "octet/stream"});
        var vUrl = window.URL.createObjectURL(vBlob);
        vLink.setAttribute('href', vUrl);
        vLink.setAttribute('download', location.hostname + '-export.json');
        vLink.click();
    }

    function importLocalStorage() {
        var fileInput = document.createElement('input');
        fileInput.type = 'file';
        fileInput.accept = '.json';
        fileInput.click();

        fileInput.addEventListener('change', function(event) {
            var file = event.target.files[0];
            if (file) {
                var reader = new FileReader();
                reader.readAsText(file);
                reader.onload = function(e) {
                    try {
                        var jsonData = JSON.parse(e.target.result);
                        for (var key in jsonData) {
                            if (jsonData.hasOwnProperty(key)) {
                                localStorage.setItem(key, jsonData[key]);
                            }
                        }
                        alert("LocalStorage has been updated from the JSON file.");
                    } catch (err) {
                        alert("Error parsing JSON: " + err);
                    }
                };
                reader.onerror = function() {
                    alert("Error reading file.");
                };
            } else {
                alert("No file selected.");
            }
        });
    }
    
    function clearLocalStorage() {
        localStorage.clear()
    }
    
    GM_registerMenuCommand("Export LocalStorage", exportLocalStorage);
    GM_registerMenuCommand("Import LocalStorage", importLocalStorage);
    GM_registerMenuCommand("Clear LocalStorage", clearLocalStorage);
})();