Add poster name saving via cookie

When a user makes a new post with a name set, it will get saved via a
cookie to the user's client and it will automatically get set the next
time a user makes a post.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-07-12 20:28:21 -07:00
parent b096f70751
commit 5df21ccb95
3 changed files with 60 additions and 2 deletions

View File

@@ -1,3 +1,4 @@
const NAME_COOKIE = "posting_as";
const OPEN = "open"; const OPEN = "open";
const CLOSED = "closed"; const CLOSED = "closed";
const replyWindowName = "reply-window"; const replyWindowName = "reply-window";
@@ -5,6 +6,35 @@ const postWindowName = "post-window";
const reportWindowName = "report-window" const reportWindowName = "report-window"
const WINDOW_INNER_PADDING = 25; const WINDOW_INNER_PADDING = 25;
////////////////////////////////////////////////////////////////////////////////
// Cookie functions, stolen from W3Schools
////////////////////////////////////////////////////////////////////////////////
function setCookie(cname, cvalue, expseconds) {
const d = new Date();
d.setTime(d.getTime() + (expseconds * 1000));
let expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
let name = cname + "=";
let ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
////////////////////////////////////////////////////////////////////////////////
// Document-specific functions
////////////////////////////////////////////////////////////////////////////////
function documentClick(e) { function documentClick(e) {
let sender = e.target; let sender = e.target;
let id = sender.getAttribute("data-id"); let id = sender.getAttribute("data-id");
@@ -82,7 +112,13 @@ function openReplyWindow(replyUrl) {
}); });
$(replyWindow.iframe, "iframe").on("load", () => { $(replyWindow.iframe, "iframe").on("load", () => {
fitWindowToContent(replyWindow); fitWindowToContent(replyWindow);
replyWindow.$("#id_text").focus(); // the iframe load event gets fired on the success page as well as the
// new post page. We just want to target the text area on the new post
// page.
let textarea = replyWindow.$("#id_text");
if (textarea) {
textarea.focus();
}
}); });
replyWindow.show(); replyWindow.show();
return replyWindow; return replyWindow;

View File

@@ -55,4 +55,15 @@ input[type=text] {
<tr><td>&nbsp;</td><td><input type="submit" value="Submit" /></td></tr> <tr><td>&nbsp;</td><td><input type="submit" value="Submit" /></td></tr>
</table> </table>
</form> </form>
<script>
$("#submit").on("click", (e) => {
let name = $("#{{form.name.auto_id}}");
setCookie(NAME_COOKIE, name.val(), 3600 * 24 * 28);
});
$(window).on("load", (e) => {
$("#{{form.name.auto_id}}").val(getCookie(NAME_COOKIE));
});
</script>
{% endblock content %} {% endblock content %}

View File

@@ -67,7 +67,18 @@ input[type=text] {
{{ max_upload_size|measure_bytes }} {{ max_upload_size|measure_bytes }}
</td> </td>
</tr> </tr>
<tr><td>&nbsp;</td><td><input type="submit" value="Submit" /></td></tr> <tr><td>&nbsp;</td><td><input id="submit" type="submit" value="Submit" /></td></tr>
</table> </table>
</form> </form>
<script>
$("#submit").on("click", (e) => {
let name = $("#{{form.name.auto_id}}");
setCookie(NAME_COOKIE, name.val(), 3600 * 24 * 28);
});
$(window).on("load", (e) => {
$("#{{form.name.auto_id}}").val(getCookie(NAME_COOKIE));
});
</script>
{% endblock content %} {% endblock content %}