0

I'm trying to collect the input from popup.js to pass to background.js. I'm unsure why it keeps giving me an undefined error.

popup.js code -

window.addEventListener('DOMContentLoaded', (event) => {
  var bt = document.getElementById("btSubmit").onclick = function(){
    var userInput = document.getElementById("userID").value;
    chrome.storage.sync.get({userID}, function(temp) {
      if (!(userInput===temp.userID))
      {
        chrome.storage.sync.set({userID: userInput}, function(){
        })
      }
    })
  }
});

popup.html -

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <script type= "text/javascript" src="https://sdk.amazonaws.com/js/aws-sdk-2.624.0.min.js"></script>
    <script type= "text/javascript" src="background.js"></script>
      <label for="userID"> User ID: </label>
      <input type="textbox" id="userID" name="userID"><br><br>
      <button type="button" id="btSubmit" style="float: right">Submit</button><br>
  </body>
</html>

I'm trying to call this in my background.js code by calling the variable userID. However, it keeps coming up as a missing variable or undefined.

Chumbotxj9
  • 29
  • 7
  • 1
    The background script runs in a hidden separate background page so including it also in the popup (which is a separate page as well) is totally wrong and doesn't do what you want. Remove it and use [messaging](https://developer.chrome.com/extensions/messaging). – wOxxOm Mar 19 '20 at 04:27
  • 1
    [communication b/w background and popup](https://stackoverflow.com/questions/13546778/how-to-communicate-between-popup-js-and-background-js-in-chrome-extension) this will help you in your code – Rajesh Verma Mar 19 '20 at 04:28
  • 1
    consider using a modal div, instead of a popup window. https://www.w3schools.com/howto/howto_css_modals.asp – ariel Mar 19 '20 at 04:33

0 Answers0