JS LocalStorage 导出导入工具

Posted by Yinode on Thursday, October 14, 2021

TOC

let genStorageSyncCode = () => {
  const kvList = []
  for (let i = 0; i < localStorage.length; i++) {
    const k = localStorage.key(i)
    const v = localStorage.getItem(k)
    kvList.push({
      k, v
    })
  }
  const code = `${kvList.map((item) => {
    return `localStorage.setItem('${item.k}','${item.v}')`
  }).join(';')}`
  return code
}

console.log(genStorageSyncCode())

直接导出代码,复制到另外一个页面控制台console粘贴回车即可进行storage同步