LocalStorage Export/Import

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

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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);
})();