PyCard Autofill (Save/Persist Form Data CodeInput)

Persist and restore textarea contents per problem page

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         PyCard Autofill (Save/Persist Form Data CodeInput)
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Persist and restore textarea contents per problem page
// @match        https://pycard.org/*
// @match        https://www.pycard.org/*
// @run-at       document-idle
// @grant        none
// ==/UserScript==

(function() {
  'use strict';

  const storageKey = 'pycard_autofill::' + location.href;

  function save(val) {
    try { localStorage.setItem(storageKey, val); } catch (e) {}
  }

  function restore(el) {
    try {
      const saved = localStorage.getItem(storageKey);
      if (saved && el.value.trim() === '') el.value = saved;
    } catch (e) {}
  }

  function init() {
    let textarea = document.getElementById('codeInput');
    if (!textarea) return;

    restore(textarea);

    textarea.addEventListener('input', () => save(textarea.value));

    const submitBtn = document.querySelector('#codeForm button[type="button"]');
    if (submitBtn) submitBtn.addEventListener('click', () => save(textarea.value));

    window.addEventListener('beforeunload', () => save(textarea.value));

    const obs = new MutationObserver(() => {
      const current = document.getElementById('codeInput');
      if (current && current !== textarea) {
        textarea = current;
        restore(textarea);
        textarea.addEventListener('input', () => save(textarea.value));
      }
    });
    obs.observe(document.body, { childList: true, subtree: true });
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
  } else {
    init();
  }
})();