]> git.ardis.lu - erc-165-calculator/commitdiff
Refactor to simplify element references
authorArdis Lu <ardisglu@gmail.com>
Sat, 13 Apr 2024 06:10:17 +0000 (23:10 -0700)
committerArdis Lu <ardisglu@gmail.com>
Sat, 13 Apr 2024 06:10:17 +0000 (23:10 -0700)
index.html

index 6e0be4ac078e9d43c237ee25cd11db34d3421f51..7a33a2cf625825c15071a80f6ceaf6632e36a8d0 100644 (file)
     import { keccak256 } from '/keccak256.js';
     const interfaces = await fetch('/interfaces.json').then(r => r.json());
 
-    let lastSelection = 'Custom';
+    const form = document.querySelector('form');
+    const select = document.querySelector('select');
+    const textarea = document.querySelector('textarea');
+    const output = document.querySelector('output');
+
     let customFunctions = '';
-    document.querySelector('select').addEventListener('change', e => {
+    select.addEventListener('change', e => {
       const selection = e.target.value;
-      const textarea = document.querySelector('textarea');
-      const output = document.querySelector('output');
-
-      if (lastSelection === 'Custom') {
-        customFunctions = textarea.value;
-        textarea.readOnly = true;
-      }
 
-      lastSelection = selection;
       if (selection === 'Custom') {
         textarea.value = customFunctions;
         textarea.readOnly = false;
-        document.querySelector('form').requestSubmit();
-      }
-      else if (typeof interfaces[selection] === 'string') {
-        textarea.value = 'No functions: the interface ID for this ERC is hardcoded.';
-        output.textContent = interfaces[selection];
+        form.requestSubmit();
       }
       else {
-        textarea.value = interfaces[selection].join('\n');
-        document.querySelector('form').requestSubmit();
+        customFunctions = textarea.value;
+        textarea.readOnly = true;
+
+        if (typeof interfaces[selection] === 'string') {
+          textarea.value = 'No functions: the interface ID for this ERC is hardcoded.';
+          output.textContent = interfaces[selection];
+        }
+        else {
+          textarea.value = interfaces[selection].join('\n');
+          form.requestSubmit();
+        }
       }
     });
 
-    document.querySelector('form').addEventListener('submit', e => {
+    form.addEventListener('submit', e => {
       e.preventDefault();
-      const functions = new FormData(e.target).get('functions');
+
       const hashes = [];
-      for (let line of functions.split('\n')) {
+      for (let line of textarea.value.split('\n')) {
         // Ignore comment lines
         line = line.trim();
         if (line.substring(0, 2) === '//') {
       }
 
       const xorHash = `0x${[...xor].map(b => b.toString(16).padStart(2, '0')).join('')}`;
-      e.target.elements['output'].textContent = xorHash;
+      output.textContent = xorHash;
     });
   </script>
 </body>
This page took 0.042491 seconds and 4 git commands to generate.