Polaris
Polaris

Reputation: 712

Chrome Extension / Output from Javascript Value

I have a problem with creating a simple chrome extension.

Following should this program do on the options page:

I want to get these storaged values and print them out on options.html:

<html>
<head>
    <title>API-Konfiguration</title>
    <script type="text/javascript" src="options.js"></script>
</head>
<body onload="loadOptions()">
    <h1>Bitte API-Key und API-Secure hinterlegen:</h1>
    <form>
API-Key:<br>
<input type="text" id="v_apikey" name="apikey">
<br>
API-Secure:<br>
<input type="text" id="v_apisecure" name="apisecure">
</form>
    <br />
    <button onclick="saveOptions()">Speichern</button>
    <br />
    <button onclick="eraseOptions()">Daten loeschen</button>
</body>
</html>

The options.js contains following:

function loadOptions() {
    var apikey = localStorage["v_apikey"];
    var apisecure = localStorage["v_apisecure"];

}

function saveOptions() {
    var apikey = document.getElementById("v_apikey");
    var apisecure = document.getElementById("v_apisecure");
    localStorage["v_apikey"] = apikey;
    localStorage["v_apisecure"] = apisecure;
}

function eraseOptions() {
    localStorage.removeItem("v_apikey");
    localStorage.removeItem("v_apisecure");
    location.reload();
}

function getItems() {

document.write(localStorage.getItems("v_apikey"));
document.write(localStorage.getItems("v_apisecure"));

}

I don't know how to output the saved values in the input field (If they have already been stored).

Who can help me with this "little" problem?

Thanks in advance!

Upvotes: 0

Views: 53

Answers (1)

shrawan_lakhe
shrawan_lakhe

Reputation: 356

To save values to the localstorage, you can also use below:

window.localStorage.setItem("newsrefreshcomment", value);

Below is the sample code that i have used to retreive the value from local storage.Before retreving value from local storage,we have to check for web storage support.

To retreive the value stored,we can use:

if (typeof (Storage) != "undefined") {
        if (localStorage.getItem("newsrefreshcomment") != null) {
            commentValnews = localStorage.getItem("newsrefreshcomment").toString();

        } 
    }

Upvotes: 1

Related Questions