Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, the localStorage of the background-page and that of the popup are <strike>not</strike> the same objects. <strike>Besides, each time the popup is shown, it is loaded anew, thus the localStorage is empty.</strike><br> <strong><em>UPDATE: Thx to rsanchez's comment, I correct my mistake: the popup shares the localStorage object of the extension (which is the same as the one of the background-page).</em></strong><br> You should use the localStorage of the background-page. (Keep in mind this works <strong>only</strong> because you have a persistent background-page !)</p> <p>The simplest (and most reliable) way is to have a single popup with two different divs (one for entering credentials and one for logging in) and display only one at a time.</p> <p>E.g.:</p> <p>1) Remove any <code>chrome.browserAction.onClicked...</code> listener from the background-page (it won't hurt leaving it there as it will never be triggered, but it will confuse you in the future).</p> <p>2) Declare a popup in your manifest (if you don't have already done do): </p> <pre><code>&lt;pre&gt; ... "browser_action": { ... "default_popup": "popup.html" }, ... &lt;/pre&gt; </code></pre> <p>3). Create a file named <code>popup.html</code> with the following code:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script type="text/javascript" src="popup.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="loadingContainer"&gt;&lt;h3&gt;Loading...&lt;/h3&gt;&lt;/div&gt; &lt;div id="settingsContainer" style="display:none;"&gt; &lt;b&gt;Enter your Email ID/Employee Code&lt;/b&gt; &lt;br /&gt; &lt;br /&gt; &lt;form id="userinfo"&gt; &lt;label for="user"&gt;Email/Employee Code:&lt;/label&gt; &lt;input type="text" id="user1" required /&gt; &lt;input type="submit" id="login" value="Log In" /&gt; &lt;/form&gt; &lt;/div&gt; &lt;div id="loggedInContainer" style="display:none;"&gt; &lt;label for="user"&gt;Email/Employee Code:&lt;/label&gt; &lt;!--&lt;input type="text" id="user2" readonly /&gt;--&gt; &lt;span id="user2"&gt;&lt;/span&gt; &lt;br /&gt; &lt;input type="button" id="calpine" value="Go to Calpinemate"/&gt; &lt;input type="button" id="change" value="Change Settings"/&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; &lt;/pre&gt; </code></pre> <p>4) Create a file named <code>popup.js</code> with the following code:</p> <pre><code>window.addEventListener('DOMContentLoaded', function() { /* Container divs */ var divLoading = document.querySelector('div#loadingContainer'); var divSettings = document.querySelector('div#settingsContainer'); var divLoggedIn = document.querySelector('div#loggedInContainer'); /* Settings-container fields */ var user1 = divSettings.querySelector('input#user1'); var form = divSettings.querySelector('form#userinfo'); /* LoggedIn-container fields */ //var user2 = divLoggedIn.querySelector('input#user2'); var user2 = divLoggedIn.querySelector('span#user2'); var change = divLoggedIn.querySelector('input#change'); var calpine = divLoggedIn.querySelector('input#calpine'); /* Query the extension's localStorage * in order to decide which DIV to show */ var user = localStorage.username; if (user) { /* 'Username' is set: Show the LoggedIn-container * (after updating the value of the (readonly) '#user' field) */ //user1.value = user2.value = user; user1.value = user2.textContent = user; divLoggedIn.style.display = 'block'; } else { /* 'Username' is not set: Show the Settings-container */ divSettings.style.display = 'block'; user1.focus(); user1.select(); } divLoading.style.display = 'none'; /* Listener for '#userinfo' form */ form.addEventListener('submit', function(evt) { evt.preventDefault(); var userStr = user1.value; chrome.runtime.getBackgroundPage(function(bgPage) { bgPage.login(userStr); }); window.close(); }); /* Listener for '#change' button */ change.addEventListener('click', function(evt) { divLoggedIn.style.display = 'none'; divSettings.style.display = 'block'; user1.focus(); user1.select(); }); /* Listener for '#calpine' button */ calpine.addEventListener('click', function() { chrome.tabs.create({ url: 'https://your.domain.goes/here' }); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload