Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                body.bg-secondary
  - const BREAKPOINT = "lg"
  header.sticky-top: nav.navbar.navbar-dark.bg-dark.px-3.border-bottom.shadow-lg(class=`navbar-expand-${BREAKPOINT}`)
    a.navbar-brand(href="https://www.wikipedia.org/" target="_blank") Wikipedia Viewer
    button.navbar-toggler(type="button" data-bs-toggle="collapse" data-bs-target="#navbar-content" aria-controls="navbar-content" aria-expanded="true" aria-label="Toggle navigation"): span.navbar-toggler-icon
    #navbar-content.collapse.navbar-collapse.show
      ul.navbar-nav
        li.nav-item: a.nav-link(href="https://github.com/SSbit01" target="_blank") by #[i.text-decoration-underline SSbit01]
        li.nav-item: a.nav-link(href="https://en.wikipedia.org/wiki/Special:Random" target="_blank") Random#[i.bi.bi-shuffle.align-middle.mx-1]Article
      form#search-form.ms-auto.mt-2(class=`mt-${BREAKPOINT}-0` role="search")
        .input-group
          input.form-control(type="search" size="24" placeholder="Search Wikipedia" required autofocus)
          button.btn.btn-primary.bi.bi-search(type="submit" disabled)
        #autocomplete.position-absolute.list-group.shadow-lg.mt-2.me-3
    
  main#entries.d-grid.gap-3.container-xxl.mt-4.mb-5
    noscript #[h1.text-warning.text-center.lh-base PLEASE ENABLE JAVASCRIPT]
    h1.text-light.text-center.lh-base Search results will appear here#[i.bi.bi-arrow-down-circle-fill.text-white-50.ms-3]
              
            
!

CSS

              
                @import url(https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css);
@import url(https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css);

#search-form:not(:focus-within) #autocomplete {
  visibility: hidden;
}

#entries pre {
  white-space: pre-wrap;
}
              
            
!

JS

              
                const FORM = document.getElementById("search-form"),
      INPUT = FORM?.querySelector("input[type='search']"),
      SUBMIT = FORM?.querySelector("button[type='submit']"),
      AUTOCOMPLETE = document.getElementById("autocomplete"),
      ENTRIES = document.getElementById("entries")



function getURL(id = 13998098) {
  return `https://en.wikipedia.org/?curid=${id}`
}



const LIST_SPINNER = document.createElement("span")
LIST_SPINNER.className = "list-group-item list-group-item-info"
LIST_SPINNER.innerHTML = '<span class="spinner-grow" role="status"></span>'

const LIST_NO_RESULTS = document.createElement("span")
LIST_NO_RESULTS.className = "list-group-item list-group-item-danger"
LIST_NO_RESULTS.innerHTML = 'There are no results<i class="bi bi-emoji-frown-fill ms-2"></i>'

const LIST_EXAMPLE = document.createElement("span")
LIST_EXAMPLE.className = "list-group-item list-group-item-info"
LIST_EXAMPLE.innerHTML = 'e.g. "Cat"'



let api = "",
    autocompleteAbortController




INPUT?.addEventListener("input", async(event) => {
  
  SUBMIT.disabled = false
  
  const { value: SEARCH } = event.currentTarget
  
  api = `https://en.wikipedia.org/w/api.php?origin=*&format=json&action=query&generator=search&gsrsearch=${SEARCH}`
  
  
  
  if (SEARCH) {
    
    if (AUTOCOMPLETE?.firstChild == LIST_SPINNER) {
      autocompleteAbortController?.abort()
    } else {
      AUTOCOMPLETE?.replaceChildren(LIST_SPINNER)
    }
    
    autocompleteAbortController = new AbortController()
    
    try {
      
      const { query: { pages } = { pages: {} } } = await fetch(api, { signal: autocompleteAbortController.signal }).then(res => res.json()),
            arr = Object.values(pages)

      if (arr.length) {
        AUTOCOMPLETE?.replaceChildren(...arr.map(({pageid, title}) => {
          const A = document.createElement("a")
          A.href = getURL(pageid)
          A.target = "_blank"
          A.className = "list-group-item list-group-item-action list-group-item-primary"
          A.textContent = title
          return A
        }))
      } else {
        AUTOCOMPLETE?.replaceChildren(LIST_NO_RESULTS)
        SUBMIT.disabled = true
      }
      
    } catch(err) {
      if (err.name !== "AbortError") {
        console.error(err)
      }
    }
    
  } else {
    autocompleteAbortController?.abort()
    AUTOCOMPLETE?.replaceChildren(LIST_EXAMPLE)
    
    SUBMIT.disabled = true
  }
})




FORM?.addEventListener("keydown", event => {
  if (AUTOCOMPLETE?.hasChildNodes()) {
    switch(event.code) {
      case "ArrowUp":
        event.preventDefault();
        ((AUTOCOMPLETE.contains(document.activeElement) && document.activeElement?.previousElementSibling) || AUTOCOMPLETE.lastElementChild)?.focus()
        break
      
      case "ArrowDown":
        event.preventDefault();
        ((AUTOCOMPLETE.contains(document.activeElement) && document.activeElement?.nextElementSibling) || AUTOCOMPLETE.firstElementChild)?.focus()
        break
      
      case "Enter":
        break
      
      default:
        INPUT?.focus()
    }
  }
})




const LOADING_ENTRY = document.createElement("article")
LOADING_ENTRY.className = "bg-light bg-gradient p-2 border rounded-3 shadow"

const LOADING_ENTRY_TITLE = document.createElement("h2")
LOADING_ENTRY_TITLE.className = "placeholder-glow"
LOADING_ENTRY_TITLE.innerHTML = '<span class="placeholder col-3"></span>'
LOADING_ENTRY.appendChild(LOADING_ENTRY_TITLE)


const LOADING_ENTRY_P = document.createElement("p")
LOADING_ENTRY_P.className = "placeholder-glow"

for (let i = 0; i < 16; i++) {
  const SPAN = document.createElement("span")
  SPAN.className = `placeholder col-${Math.floor(Math.random() * 5) + 4}`
  LOADING_ENTRY_P.appendChild(SPAN)
}

LOADING_ENTRY.appendChild(LOADING_ENTRY_P)


const ENTRY_NO_RESULTS = document.createElement("h1")
ENTRY_NO_RESULTS.className = "text-light text-center lh-base"
ENTRY_NO_RESULTS.innerHTML = 'There are no results<i class="bi bi-emoji-frown-fill text-warning ms-3"></i>'




let entryAbortController



// FORM?.addEventListener("submit", async(event) => {  /* Strange Bug in CodePen, triggers "Referred From Pen" message */
SUBMIT?.addEventListener("click", async(event) => {
  
  event.preventDefault()
  
  autocompleteAbortController?.abort()
  
  SUBMIT.disabled = true
  
  if (ENTRIES?.firstChild == LOADING_ENTRY) {
    entryAbortController?.abort()
  } else {
    ENTRIES?.replaceChildren(LOADING_ENTRY)
  }
  
  entryAbortController = new AbortController()
  
  
  try {
    
    const { query: { pages } = { pages: {} } } = await fetch(api + "&prop=extracts|pageimages&exintro=1", { signal: entryAbortController.signal }).then(res => res.json())
  
    AUTOCOMPLETE?.replaceChildren()  // Remove all child nodes

    const arr = Object.values(pages)


    if (arr.length) {
      ENTRIES?.replaceChildren(...arr.map(({ title, thumbnail, pageimage, extract, pageid }) => {
        const ARTICLE = document.createElement("article"),
              A = document.createElement("a"),
              H2 = document.createElement("h2")

        ARTICLE.className = "bg-light bg-gradient pt-2 pt-md-3 px-2 px-md-3 pb-md-1 border rounded-3 shadow"

        A.className = "text-decoration-none"
        A.href = getURL(pageid)
        A.target = "_blank"

        H2.textContent = title
        A.appendChild(H2)

        if (pageimage && thumbnail) {
          const IMG = document.createElement("img")
          IMG.src = thumbnail.source
          IMG.title = pageimage
          IMG.className = "float-end img-thumbnail m-2"
          A.appendChild(IMG)
        }

        A.insertAdjacentHTML("beforeend", extract)

        ARTICLE.appendChild(A)

        return ARTICLE
      }))

      SUBMIT.disabled = false
    } else {
      ENTRIES?.replaceChildren(ENTRY_NO_RESULTS)
    }
    
  } catch(err) {
    if (err.name !== "AbortError") {
      console.error(err)
    }
  }
})
              
            
!
999px

Console