You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.js 750B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. async function api(method, path, body = null) {
  2. let options = {method};
  3. if (body != null) {
  4. options.body = JSON.stringify(body);
  5. }
  6. let resp = await fetch("/api/" + path, options).then(r => r.text());
  7. let json = JSON.parse(resp);
  8. if (json.error != null) {
  9. throw new Error(json.error);
  10. }
  11. return json;
  12. }
  13. function html(name, attrs, children) {
  14. if (name == "text") {
  15. return document.createTextNode(attrs);
  16. }
  17. let el = document.createElement(name);
  18. for (let key in attrs) {
  19. if (!attrs.hasOwnProperty(key)) {
  20. continue;
  21. }
  22. el.setAttribute(key, attrs[key]);
  23. }
  24. for (let child of children) {
  25. el.appendChild(child);
  26. }
  27. return el;
  28. }
  29. function clearElement(el) {
  30. while (el.firstChild) {
  31. el.removeChild(el.firstChild);
  32. }
  33. }