0

I'm trying to have a form in my chrome extension popup that has its data processed by js when it's submitted. However, without any errors, nothing happens on submission.

manifest.json

"action":{
  "default_popup": "index.html"
},
"background": {
  "service_workers": ["app.js"]
}

index.html

<!doctype html>
<html>
<body>
  <form id="form">
    <input id="grade" type="text" placeholder="grade">
    <input id="score" type="text" placeholder="score">
    <input type="submit" value="Save">
  </form>
</body>
</html>

app.js


document.addEventListener('DOMContentLoaded', function () {
    var form = document.getElementById("form")
    form.addEventListener('submit', function(e){        
        var grade = document.getElementById("grade").value
        var score = document.getElementById("score").value

        alert(grade)

        chrome.storage.sync.set({grade: grade})

        chrome.storage.sync.get(['grade'], function(result) {
          console.log('Value currently is ' + result.key);
        });
    })
})

Is this happening because I don't include the <script src="app.js"> tag? Chrome gives an error if any script tags are inside the html of the popup.

document.addEventListener('DOMContentLoaded', function() {
  var form = document.getElementById("form");

  form.addEventListener('submit', function(e) {
    e.preventDefault();
    var grade = document.getElementById("grade").value;
    var score = document.getElementById("score").value;

    console.log(grade);

    // chrome.storage.sync.set({grade: grade});

    // chrome.storage.sync.get(['grade'], function(result) {
      // console.log('Value currently is ' + result.key);
    // });
  });
});
<form id="form">
  <input id="grade" type="text" placeholder="grade">
  <input id="score" type="text" placeholder="score">
  <input type="submit" value="Save">
</form>
isherwood
  • 58,414
  • 16
  • 114
  • 157
Walker
  • 121
  • 1
  • 9
  • 1
    Please revise your post to ask a clear, _specific_ question. See [ask]. – isherwood Jan 13 '23 at 19:30
  • I thought preventDefault() just prevents the page from reloading? – Walker Jan 13 '23 at 19:32
  • Even after removing it, same thing? – Walker Jan 13 '23 at 19:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/251128/discussion-between-walker-and-isherwood). – Walker Jan 13 '23 at 19:35
  • There's no server in an extension, the html files are just local files, so `form` won't work. Remove it and just use a normal `button` element to save the inputs. – wOxxOm Jan 13 '23 at 19:40
  • Alright, let me try that – Walker Jan 13 '23 at 19:41
  • Also remove `background` from manifest.json and add `` to the end of your html. – wOxxOm Jan 13 '23 at 19:41
  • It gives me an error when I add js into my html: Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution. Note that hashes do not apply to event handlers, style attributes and javascript: navigations unless the 'unsafe-hashes' keyword is present. – Walker Jan 13 '23 at 19:44
  • This happens for the onclick="function()" – Walker Jan 13 '23 at 19:44
  • Use a separate js file, see [onclick or inline script isn't working in extension](https://stackoverflow.com/q/13591983) – wOxxOm Jan 14 '23 at 08:19

0 Answers0