0

I am creating my first chrome extension and I am relatively very new to it.

My extension looks like this:

Manifest.json

{
    "name": "Screen Compare",
    "description": "Screen Compare",
    "version": "1.0", 
    "permissions": [
        "tabs", "http://*/*", "https://*/*"
    ],
    "browser_action": {      
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    },
    "manifest_version": 2
} 

popup.html

<html>
<head>
     <script src="popup.js"></script>
</head>
<body>
    <div id="loginConatiner">
        <table>
            <tr>
                <td>
                    Username:
                </td>
                <td>
                    <input id="txtUsername" type="text" placeholder="Username"/>
                </td>
            </tr>
            <tr>
                <td>
                    Password:
                </td>
                <td>
                    <input id="txtPassword" type="password" placeholder="Password"/>
                </td>
            </tr>
            <tr>
                <td>
                    <input id="btnLogin" type="button" value="Sign In"/>
                </td>
                <td>
                </td>
            </tr>
        </table>
    </div>
    <div id="applicationsContainer">
        <table>
            <tr>
                <td>
                    Applications:
                </td>
                <td>
                    <select id="currentApplications"></select>
                </td>
            </tr>
        </table>
    </div>
</body>
</html>

popup.js

var currentApplications = [];

function login(e) {
    var username = document.getElementById('txtUsername');
    var password = document.getElementById('txtPassword');

    console.log(username.value);
    console.log(password.value);

    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'url to load my applications', true);
    xhr.setRequestHeader("Authorization", "Basic " + btoa(username.value + ":" + password.value));
    xhr.onreadystatechange = function(){
        if(xhr.readyState == 4){
            console.log(xhr.responseText);
            currentApplications = JSON.parse(xhr.responseText);
            console.log(currentApplications.applications.length);
            var applicationsContainer = document.getElementById('applicationsContainer');
            if(applicationsContainer){
                applicationsContainer.style.display = 'block';
            }

            var loginConatiner = document.getElementById('loginConatiner');
            if(loginConatiner){
                loginConatiner.style.display = 'none';
            }

            var applicationsDropdown = document.getElementById('currentApplications');          
            if(applicationsDropdown){
                for(var i =0, count = currentApplications.applications.length; i < count; i++){
                    var option = document.createElement("option");
                    option.text = currentApplications.applications[i].name;
                    option.value = currentApplications.applications[i].id;
                    applicationsDropdown.add(option);
                }
            }

        }
    };
    xhr.send();
}

document.addEventListener('DOMContentLoaded', function () {
    var applicationsContainer = document.getElementById('applicationsContainer');
    if(applicationsContainer){
        applicationsContainer.style.display = 'none';
    }
    var loginButton = document.getElementById('btnLogin');
    loginButton.addEventListener('click', login);  
});

when user clicks on addon icon in browser, a login form will be displayed. On successful login I hide the loginContianer div and display the applicationsContainer div with loaded application.

When the popup is closed and open again, I again see the login page. I am not able to persist the ui.

I am feel some where I am not utilizing the architecture properly, Kindly guide me.

Priyank Thakkar
  • 4,752
  • 19
  • 57
  • 93
  • What do you mean by "not able to persist the ui"? – Dayton Wang Dec 22 '14 at 22:32
  • The popup pages gets torndown on close and rebuilt on load. You have to store persistent data in localStorage or chrome.storage. – abraham Dec 23 '14 at 03:23
  • @gui47: By persist, I mean it recreates the entire ui of popup.html each time popup closes and open, while i want to persist the state of my ui – Priyank Thakkar Dec 23 '14 at 06:53
  • possible duplicate of [How can I instruct Chrome Extension to NOT reload my HTML each time it is opened?](http://stackoverflow.com/questions/26398014/how-can-i-instruct-chrome-extension-to-not-reload-my-html-each-time-it-is-opened) – Xan Dec 23 '14 at 16:45

0 Answers0