[JavaScript] 匯出與匯入 HTML5 localStorage 的資訊
最近在寫 chrome extension 時,想要提供一個設定的匯出與匯入功能~
基本上因為我把設定都寫在 HTML5 localStorage 裡面,
因此只要可以匯出/匯入 localStorage 的東西,任務就算完成囉~
基本上程式也相當的簡單~
匯出 localStorage 的內容
function jsSettingExport()
{
// Get all key/values in localStorage
var arrayLocalStorageKeyValues = {};
for (var sKey in localStorage)
{
arrayLocalStorageKeyValues[sKey] = localStorage[sKey];
}
return JSON.stringify(arrayLocalStorageKeyValues);
}
{
// Get all key/values in localStorage
var arrayLocalStorageKeyValues = {};
for (var sKey in localStorage)
{
arrayLocalStorageKeyValues[sKey] = localStorage[sKey];
}
return JSON.stringify(arrayLocalStorageKeyValues);
}
匯入資訊至 localStorage
function jsSettingImport(var sExportedLocalStorage)
{
// Clear localStorage
localStorage.clear();
// Import settings
var objImport = JSON.parse(sExportedLocalStorage);
for (var sKey in objImport)
{
localStorage[sKey] = objImport[sKey];
}
}
{
// Clear localStorage
localStorage.clear();
// Import settings
var objImport = JSON.parse(sExportedLocalStorage);
for (var sKey in objImport)
{
localStorage[sKey] = objImport[sKey];
}
}
(本頁面已被瀏覽過 488 次)