Login Register
Back to Help Center

Reading Input in a Plugin

Guide on how to read problem sample inputs and custom user test cases inside a plugin

The platform passes the currently selected sample input (or custom test case) to your plugin via the URL hash:

index.html#input={urlEncodedInput}

The iframe is recreated every time the input changes, so your plugin always receives fresh input on load — no event listeners or polling needed.

Using the monkeyCodeInput Library

The easiest way to read input is with the monkeyCodeInput.js helper library. It is automatically available in every plugin — you do not need to include it in your ZIP.

If you want to test your plugin locally, you can download the library here:

Download monkeyCodeInput.js

Setup

Include the script at the top of your <head>, before any other scripts:

Include monkeyCodeInput.js at the top of the <head>, before any plugin or application scripts. Otherwise your code may run before the library is available on first load.
Use a relative path (./monkeyCodeInput.js) so the script resolves correctly in all environments.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="./monkeyCodeInput.js"></script>
  <script src="./script.js"></script>
</head>
<body>
  <!-- your plugin UI -->
</body>
</html>

API Reference

Once the script is loaded, four global functions are available:

FunctionReturnsDescription
input()string | nullThe raw URL-decoded input string, or null if no input is present
inputArray()number[]Input split on all whitespace and newlines, each token converted to a number
inputLines()string[]Input split on newlines, each line returned as a string
getInputAt(index)number | undefinedA single number from inputArray() at the given zero-based index

Examples

Read the entire input as a string:

script.js
const raw = input()
console.log(raw) // "5\n1 2 3 4 5"

Read all tokens as numbers (e.g. a flat list of values):

script.js
const values = inputArray()
// Input: "3 1 4 1 5"
// values → [3, 1, 4, 1, 5]

Read line by line (e.g. a grid or multi-line input):

script.js
const lines = inputLines()
// Input: "3\n1 2\n3 4\n5 6"
// lines → ["3", "1 2", "3 4", "5 6"]

const n = parseInt(lines[0])
for (let i = 1; i <= n; i++) {
  const [a, b] = lines[i].split(' ').map(Number)
  // process row i
}

Read a specific token by index:

script.js
const n = getInputAt(0)  // first token
const k = getInputAt(1)  // second token

Reading Input Without the Library

If you prefer not to use monkeyCodeInput.js, you can read the hash directly:

index.html
const raw = decodeURIComponent(window.location.hash.replace('#input=', ''))
// `raw` is the full input string, e.g. "5\n1 2 3 4 5"

Or using URLSearchParams for a more robust parse:

index.html
const raw = new URLSearchParams(window.location.hash.slice(1)).get('input')
const decoded = raw ? decodeURIComponent(raw) : ''

Full Minimal Example

A complete plugin that reads n followed by n numbers and renders them as a bar chart:

index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <script src="./monkeyCodeInput.js"></script>
</head>
<body>
  <canvas id="chart" width="380" height="360"></canvas>
  <script>
    const lines = inputLines()
    const n = parseInt(lines[0])
    const values = lines[1].split(' ').map(Number)

    const canvas = document.getElementById('chart')
    const ctx = canvas.getContext('2d')
    const max = Math.max(...values)
    const barWidth = canvas.width / n

    values.forEach((v, i) => {
      const barHeight = (v / max) * canvas.height
      ctx.fillStyle = '#6366f1'
      ctx.fillRect(i * barWidth, canvas.height - barHeight, barWidth - 2, barHeight)
    })
  </script>
</body>
</html>