0

我正在编写一个利用本地存储的 web 应用程序。我现在将其设置为使用以下格式保存到本地存储:

键:四位数字 值:我的数据

我需要从 localStorage 中获取所有单独的数据,并将其输出到具有以下格式的单个文件:

XXXX <- 四位键
数据
-linebreak-

我该怎么做呢?此外,是否有可能以某种方式获取所有这些信息,并通过电子邮件发送。或者以某种方式将其从本地存储和剪贴板中取出,以便用户可以将其复制到他们的电子邮件中。

提前谢谢。

4

2 回答 2

1
var output = "";
for(var key in localStorage) { 
    output += key+"\n";
    output += localStorage[key]+"\n";
    output += "\n";
}

// output contains combined string

As for the email part you could try using a mailto: like this,

<a href='mailto:user@domain?subject=[subject here]&body=[email body here]'></a>

This could be combined into a function like this:

function sendLocalStorageByEmail(recipient) {
    // create localstorage string
    var output = "";
    for(var key in localStorage) { 
        output += key+"\n";
        output += localStorage[key]+"\n";
        output += "\n";
    }

    // create temporary anchor to emulate mailto click in new tab
    var anchor = document.createElement("a");
    anchor.href = "mailto:"+recipient+"?subject=Local+Storage+Data&body="+encodeURIComponent(output);
    anchor.style.display = "none"; 
    anchor.setAttribute("target","_blank");
    anchor.appendChild(document.createTextNode(""));
    document.body.appendChild(anchor);

    if (anchor.click) {
        return anchor.click();
    }

    // some browsers (chromium/linux) have trouble with anchor.click
    var clickEv = document.createEvent("HTMLEvents");
    clickEv.initEvent("click", true, true);
    anchor.dispatchEvent(clickEv)
}

Usage:

<a href='javascript:sendLocalStorageByEmail(prompt("Please enter your e-mail address"))'>
   Send Email
</a>
于 2012-12-02T19:13:42.380 回答
0

可以在代码中直接集成mail地址,在mailto之前:

anchor.href = "mailto:*youradress@ttt.com*"+recipient+"? 
subject=Local+Storage+Data&body="+encodeURIComponent(output);
于 2020-02-06T17:48:51.997 回答