Lorem Ipsum Generator

When testing interfaces in Appian during the normal development cycle, I frequently find myself typing random text into form fields.

The nerd I am, I wanted something to make this more convenient. Browser support JavaScript in bookmarks. This is called a Bookmarklet. And it has direct access to the current web page.

So I needed some JavaScript that generates random text and adds it to the currently selected text field. I also wanted it to generate short text for text fields, and longer text for paragraph fields.

The Code

Just copy the following code into the address field of a bookmark.

javascript: (function () {
  const loremIpsumWords = [
    "lorem",
    "ipsum",
    "dolor",
    "sit",
    "amet",
    "consectetur",
    "adipiscing",
    "elit",
    "sed",
    "do",
    "eiusmod",
    "tempor",
    "incididunt",
    "ut",
    "labore",
    "et",
    "dolore",
    "magna",
    "aliqua",
    "ut",
    "enim",
    "ad",
    "minim",
    "veniam",
    "quis",
    "nostrud",
    "exercitation",
    "ullamco",
    "laboris",
    "nisi",
    "ut",
    "aliquip",
    "ex",
    "ea",
    "commodo",
    "consequat",
    "duis",
    "aute",
    "irure",
    "dolor",
    "in",
    "reprehenderit",
    "in",
    "voluptate",
    "velit",
    "esse",
    "cillum",
    "dolore",
    "eu",
    "fugiat",
    "nulla",
    "pariatur",
    "excepteur",
    "sint",
    "occaecat",
    "cupidatat",
    "non",
    "proident",
    "sunt",
    "in",
    "culpa",
    "qui",
    "officia",
    "deserunt",
    "mollit",
    "anim",
    "id",
    "est",
    "laborum",
  ];

  function generateLoremIpsum(wordsCount) {
    let result = [];
    for (let i = 0; i < wordsCount; i++) {
      result.push(
        loremIpsumWords[Math.floor(Math.random() * loremIpsumWords.length)],
      );
    }
    return result.join(" ");
  }

  function insertTextAtCursor(text) {
    var activeElement = document.activeElement;
    if (
      activeElement &&
      (activeElement.tagName === "TEXTAREA" ||
        (activeElement.tagName === "INPUT" && activeElement.type === "text"))
    ) {
      var start = activeElement.selectionStart;
      var end = activeElement.selectionEnd;
      activeElement.value =
        activeElement.value.substring(0, start) +
        " " +
        text +
        activeElement.value.substring(end);
      activeElement.selectionStart = activeElement.selectionEnd =
        start + text.length + 1;
    }
  }

  var activeElement = document.activeElement;
  var isTextarea = activeElement && activeElement.tagName === "TEXTAREA";
  if (isTextarea) {
    var loremText = generateLoremIpsum(15);
  } else {
    var loremText = generateLoremIpsum(3);
  }
  insertTextAtCursor(loremText);
})();

Just navigate to a text or paragraph field, preferably using the TAB key, then click the bookmark.

Summary

I was also toying around with the idea of iterating on all fields in a form, and enter data based on type, label and context, but this quickly became into a mess. If you have some good ideas on how to solve this, without calling an LLM, please let me know.

Keep rocking!

One thought on “Lorem Ipsum Generator

Leave a Reply