this post was submitted on 11 Jun 2023
7 points (100.0% liked)

Lemmy.World Announcements

29008 readers
1 users here now

This Community is intended for posts about the Lemmy.world server by the admins.

Follow us for server news ๐Ÿ˜

Outages ๐Ÿ”ฅ

https://status.lemmy.world

For support with issues at Lemmy.world, go to the Lemmy.world Support community.

Support e-mail

Any support requests are best sent to [email protected] e-mail.

Report contact

Donations ๐Ÿ’—

If you would like to make a donation to support the cost of running this platform, please do so at the following donation URLs.

If you can, please use / switch to Ko-Fi, it has the lowest fees for us

Ko-Fi (Donate)

Bunq (Donate)

Open Collective backers and sponsors

Patreon

Join the team

founded 1 year ago
MODERATORS
 

Decided to make a little script that listens to textareas in post & comments, and if you press Ctrl Enter while focusing on them, it submits them. I use this to post comments faster and with less bother. It's reminiscent of the RES feature to do the same thing.

If you use Greasemonkey or Tampermonkey, you can install this easily and instantly have CtrlEnter to submit. Let me know any improvements I can make.

// ==UserScript==
// @name         Lemmy Form Submit with Ctrl+Enter
// @version      1.0
// @description  Submit forms with Ctrl+Enter in Lemmy instances so you don't have to click the button every time you want to post something.
// @author       God (https://sh.itjust.works/u/god)
// @match        https://*/post/*
// @match        https://*/comment/*
// @icon         https://join-lemmy.org/static/assets/icons/favicon.svg
// ==/UserScript==

var isLemmy =
  document.head.querySelector("[name~=Description][content]").content ===
  "Lemmy";

if (isLemmy) {
  // Define a global variable to keep track of the currently focused textarea.
  var currentFocusedTextarea = null;

  // Function to attach focus and blur event handlers to all textareas.
  function attachEventHandlers() {
    document.querySelectorAll("textarea").forEach((textarea) => {
      if (!textarea.dataset.ctrlEnterHandled) {
        textarea.dataset.ctrlEnterHandled = true;

        // Check if this textarea is currently focused
        const wasFocused = document.activeElement === textarea;

        textarea.addEventListener("focus", function () {
          currentFocusedTextarea = this;
        });

        textarea.addEventListener("blur", function () {
          currentFocusedTextarea = null;
        });

        // If this textarea was focused, blur and re-focus it to ensure event handlers get triggered
        if (wasFocused) {
          textarea.blur();
          textarea.focus();
        }
      }
    });
  }

  // Attach a keydown event handler to the entire document.
  document.addEventListener("keydown", function (event) {
    // If Ctrl + Enter is pressed
    if (event.ctrlKey && event.key === "Enter") {
      // If a textarea is focused and contains text
      if (
        currentFocusedTextarea &&
        currentFocusedTextarea.value.trim() !== ""
      ) {
        // Your submit logic here
        handleSubmit(currentFocusedTextarea);
      }
    }
  });

  function handleSubmit(textarea) {
    // find the closest type="submit" button and press it.
    textarea.closest("form").querySelector('[type="submit"]').click();
  }

  // Call the function initially to cover textareas that exist when the page is first loaded.
  attachEventHandlers();

  // Observe the document for changes and reattach event handlers when new textareas are added.
  const observer = new MutationObserver(function (mutations) {
    mutations.forEach((mutation) => {
      if (mutation.type === "childList") {
        attachEventHandlers();
      }
    });
  });

  observer.observe(document.body, { childList: true, subtree: true });
}
you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 3 points 1 year ago* (last edited 1 year ago) (1 children)

You should share this with the "Lemmy Plugins & Userscripts" community at [email protected] :)

[โ€“] god 3 points 1 year ago (1 children)

I created that community and that was the first thing I did ๐Ÿ˜…

But I really appreciate the promo effort, and you should keep at it when you see similar things.

Protip, to link a community to anyone on any server, you can do it like this: [some text](/c/[email protected]), example: [[email protected]](/c/[email protected]) which would look like this: [email protected] and would allow anyone to just click it from their own instance and subscribe to it.

[โ€“] [email protected] 2 points 1 year ago

Small world! I didn't realize! ๐Ÿ˜‚ Thank you for the tips, though! I'm still definitely getting up to speed with finding/linking communities.