Replace JS on form with a POST request

Closes #12
This commit is contained in:
Nikita Karamov 2021-08-27 15:03:24 +02:00
parent 4b545f4a14
commit 7839dfa6ff
No known key found for this signature in database
GPG key ID: 7FCADEDBB1AEBE44
3 changed files with 36 additions and 15 deletions

35
api/toot.js Normal file
View file

@ -0,0 +1,35 @@
/*!
toot - Cross-instance share page for Mastodon
Copyright (C) 2020-2021 Nikita Karamov <nick@karamoff.dev>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
module.exports = function (req, res) {
let text = "";
let instanceURL = "https://mastodon.social/";
if (req.body) {
if (req.body.text) {
text = req.body.text;
}
if (req.body.instance) {
instanceURL = req.body.instance;
}
}
return res.redirect(
303,
instanceURL + "share?text=" + encodeURIComponent(text)
);
};

View file

@ -61,7 +61,7 @@
</p> </p>
</header> </header>
<main> <main>
<form id="form"> <form action="/api/toot" method="POST">
<section> <section>
<label for="text">Post text</label> <label for="text">Post text</label>
<textarea <textarea

View file

@ -20,7 +20,6 @@ const INSTANCE_LIST_URL = "https://api.joinmastodon.org/servers";
const $instance = document.getElementById("instance"); const $instance = document.getElementById("instance");
const $instanceDatalist = document.getElementById("instanceDatalist"); const $instanceDatalist = document.getElementById("instanceDatalist");
const $form = document.getElementById("form");
/** /**
* Adds missing "https://" and ending slash to the URL * Adds missing "https://" and ending slash to the URL
@ -90,16 +89,3 @@ if (prefillInstance != null) {
} }
$instance.addEventListener("focus", loadInstances); $instance.addEventListener("focus", loadInstances);
$form.addEventListener("submit", function (e) {
e.preventDefault();
const text = e.target.elements["text"].value;
const instanceURL = normalizeUrl(e.target.elements["instance"].value);
const remember = e.target.elements["remember"].checked;
if (remember) {
window.localStorage.setItem("mastodon_instance", instanceURL);
}
window.location.href = instanceURL + "share?text=" + encodeURIComponent(text);
});