diff --git a/app/server.js b/app/server.js index 005ab30..31ec187 100644 --- a/app/server.js +++ b/app/server.js @@ -1,7 +1,8 @@ // Main entry point for the server. Deploys background worker in "bg-worker.js" for handling networking. +spaHide("jsAlert"); + let myWorker = null; -let getFrom, postTo, TGchatID; let numReadMsgs = 0; let numTotalMsgs = 0; const logs = document.getElementById("logs"); @@ -70,8 +71,8 @@ function genUUID() { } const fetchChatID = async () => { - logThis("Fetching Telegram chat ID") - const apiEndpoint = 'https://api.telegram.org/bot' + document.getElementById("apiKey").value + '/getUpdates'; + logThis("Fetching Telegram chat ID"); + const apiEndpoint = 'https://api.telegram.org/bot' + document.getElementById("TGbotKey").value + '/getUpdates'; const response = await fetch(apiEndpoint); // Make request if (! response.ok) { logThis(`Telegram API status code: ${response.status}. Is Bot API Token ok?`); @@ -80,7 +81,9 @@ const fetchChatID = async () => { } const data = await response.json(); try { - TGchatID = data.result[0].message.chat.id; + const TGchatID = data.result[0].message.chat.id; + document.getElementById("chatID").value = TGchatID; + localStorage.setItem("TGchatID", TGchatID); } catch (e) { alert("Failed to fetch chat ID. Send any text to the Telegram Bot then try again."); } @@ -91,19 +94,18 @@ function config() { const uuid = document.getElementById("uuid").value; // Choose a random index in [0, relayList.length]. Use first two nibbles of uuid as random number in range [0,256]. const randomIdx = Math.floor(parseInt(uuid.substr(0,2),16)*relayList.length/256); - getFrom = relayList[randomIdx] + '/' + uuid; - postTo = 'https://api.telegram.org/bot' + document.getElementById("apiKey").value + '/sendMessage'; - document.getElementById("config").innerHTML = `

HTML Form Action URL: ${getFrom}

`; - document.getElementById("testFormBtn").setAttribute("formaction", getFrom); - spaShowHide("testForm"); - document.getElementById("config").scrollIntoView(); + const getFrom = relayList[randomIdx] + '/' + uuid; + localStorage.setItem("getFrom", getFrom); + const postTo = 'https://api.telegram.org/bot' + document.getElementById("TGbotKey").value + '/sendMessage'; + localStorage.setItem("postTo", postTo); + spaGoTo("server"); + localStorage.setItem("loggedIn", "true"); } function startWorker() { - if (getFrom === undefined) { - config(); + if (myWorker) { + return; } - myWorker = new Worker("app/bg-worker.js"); // Register handler for messages from the background worker @@ -122,17 +124,26 @@ function startWorker() { } } + const getFrom = localStorage.getItem("getFrom"); + // Communicate key data to the background worker - myWorker.postMessage([getFrom, postTo, TGchatID]); + myWorker.postMessage([getFrom, localStorage.getItem("postTo"), localStorage.getItem("TGchatID")]); toggleServer.value = "Kill Server"; toggleServer.disabled = false; logThis("Server started"); document.getElementById("serverStatus").innerHTML = 'Live '; + + document.getElementById("formActionURL").innerHTML = `

HTML Form Action URL: ${getFrom}

`; + document.getElementById("testFormBtn").setAttribute("formaction", getFrom); + spaShow("testForm"); } function stopWorker() { + if (! myWorker) { + return; + } myWorker.terminate(); myWorker = null; console.log("Worker terminated"); @@ -148,3 +159,22 @@ function toggleWorker() { startWorker(); } } + +function signout() { + stopWorker(); + localStorage.clear(); + location.reload(); +} + +function main() { + // Enable config if no prior settings found in localStorage + if (localStorage.getItem("loggedIn")) { + startWorker(); + spaGoTo("server"); + } else { + spaGoTo("setup"); + } + +} + +spaHide("jsAlert"); diff --git a/app/spa.js b/app/spa.js index 6c08bb6..a092d5e 100644 --- a/app/spa.js +++ b/app/spa.js @@ -5,20 +5,30 @@ const spaHomePageID = document.querySelector(".spa-page").id; // Assuming first spa-page class is the home / hero page let spaCurrentPageID = spaHomePageID; -{ - const jsAlerts = document.getElementsByClassName("spa-js"); - - for (let el of jsAlerts) { - el.style.display = 'none'; - } +function spaShow(id) { + document.getElementById(id).style.display = 'block'; +} - const pages = document.getElementsByClassName("spa-page"); +function spaHide(id) { + document.getElementById(id).style.display = 'none'; +} - for (let el of pages) { - el.style.display = 'none'; +function spaToggle(id) { + let x = document.getElementById(id); + if (x.style.display === "none") { + x.style.display = "block"; + } else { + x.style.display = "none"; } } +function spaGoTo(id) { + document.getElementById(spaCurrentPageID).style.display = 'none'; + spaShow(id); + spaCurrentPageID = id; + spaTop(); +} + function spaTop(){ document.getElementById(spaCurrentPageID).scrollIntoView(); } @@ -27,19 +37,11 @@ function spaBottom(){ document.getElementById(spaCurrentPageID).scrollIntoView(false); } -function spaGoTo(id) { - document.getElementById(spaCurrentPageID).style.display = 'none'; - document.getElementById(id).style.display = 'block'; - spaCurrentPageID = id; - spaTop(); -} +{ + const pages = document.getElementsByClassName("spa-page"); -function spaShowHide(id) { - let x = document.getElementById(id); - if (x.style.display === "none") { - x.style.display = "block"; - } else { - x.style.display = "none"; + for (let el of pages) { + el.style.display = 'none'; } } diff --git a/index.html b/index.html index 6a7c862..36ed78e 100755 --- a/index.html +++ b/index.html @@ -22,11 +22,9 @@

Welcome to EasyForm

-

Menu

- - + Source Donate Contact @@ -41,45 +39,31 @@

Menu

-

You don't have JavaScript enabled! This page cannot function without it.

+

You don't have JavaScript enabled! This page cannot function without it.

About

-

EasyForm gives you a free and easy, self-hosted form backend solution for adding (contact) forms to your static website(s). You don't, however, need - to configure any server or install anything. Your browser becomes your server! -

-

Your smartphone or PC is perhaps always connected to the internet, even on the move. So, if you keep your browser open there, and a very - light-weight JavaScript server runs in it, you are essentially "self-hosting" for free. EasyForm capitalizes on this. Also, the ability to run in a - browser makes EasyForm platform-independent! +

EasyForm gives you a free and easy, self-hosted form backend solution that runs in your browser! Just sign up and then embed the following HTML + form in your website. Therefrom, as long as this Tab is open in your browser, whenever your users submit the form in your website, your Inbox here + will be populated. You will also be notified via Telegram.

- Working with EasyForm is dead simple. All you need to do is the following: -
    -
  • Create a Telegram Bot and store its API token. This is easy. Just open a chat with @BotFather in Telegram - and send: /newbot -
  • -
  • Follow instructions in the Config: section below. You will be required to choose a Form Action URL there
  • -
  • Use your chosen Form Action URL in your (contact) form
  • -
- Whenever your users submit the form, you will get a Telegram text containing the users' form data from the Telegram Bot you created. -

Here is an HTML code snippet you can readily embed as a basic contact form in your website. Just replace FormActionURL with the actual URL.


-<form action="FormActionURL" method="POST" target="hidden_iframe" autocomplete="on">
+<!-- Replace XXXXX with the form action URL given by this app upon sign up` -->
+<form action="XXXXX" method="POST" target="hidden_iframe" autocomplete="on">
     <input type="hidden" name="From" value="sample">
     <input type="email" name="Email" placeholder="Your Email">
     <input type="text" name="Name" placeholder="Your Name">
     <input type="text" name="Message" placeholder="Your Message" autocomplete="off">
-    <input type="submit" value="Submit">
-    <input type="reset" value="Reset">
+    <button type="submit">Submit<button>
+    <input type="reset">Reset<button>
 </form>
 <!-- when the form is submitted, the server response will appear in this iframe, hidden from view -->
 <iframe name="hidden_iframe" src="about:blank" hidden></iframe>
 
- Tips: -
    -
  • Securely store your Bot API token and Form Action URL for future reference.
  • -
  • For reproducibility upon page reload/refresh, reuse the previous values, often suggested by the browser, during config.
  • -
+ +

+

Powered by

-
+
-

Server

-
-
+

Setup

+

Setup in just 3 easy steps.

- - + +
-

2. Send any text to the Bot. Then, click Fetch Chat ID. Or, enter your chat ID if you know what you're doing :-)

+

2. Send any text to the Bot. Then, click Fetch Chat ID. Do not enter your chat ID by hand.

+

3. Generate or enter your EasyForm API Key:



- +
-
-
+
+
+
+

Server

+
+

Test if everything is working properly with the following sample form. You should get a Telegram message upon clicking Post! Your posted data should also be logged below.

@@ -132,11 +121,14 @@

Server



Log

+ + + +

Server logs will be shown here



-