SMAM (short for Send Me A Mail) is a free (as in freedom) contact form embedding software.

form.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. var items = {
  2. name: 'form_name',
  3. addr: 'form_addr',
  4. subj: 'form_subj',
  5. text: 'form_text',
  6. };
  7. var server = getServer();
  8. var xhrSend = new XMLHttpRequest();
  9. xhrSend.onreadystatechange = function() {
  10. if (xhrSend.readyState == XMLHttpRequest.DONE) {
  11. console.log(xhrSend.response);
  12. }
  13. };
  14. // Returns the server's base URI based on the user's script tag
  15. // return: the SMAM server's base URI
  16. function getServer() {
  17. var scripts = document.getElementsByTagName('script');
  18. // Parsing all the <script> tags to find the URL to our file
  19. for(var i = 0; i < scripts.length; i++) {
  20. let script = scripts[i];
  21. if(script.src) {
  22. let url = script.src;
  23. // This should be our script
  24. if(url.match(/:[0-9]+\/form\.js$/)) {
  25. // Port has been found
  26. return url.match(/^(http:\/\/[^\/]+)/)[1];
  27. }
  28. }
  29. }
  30. }
  31. // Creates a form
  32. // id: HTML identifier of the document's block to create the form into
  33. // return: nothing
  34. function generateForm(id) {
  35. var el = document.getElementById(id);
  36. // Set the form's behaviour
  37. el.setAttribute('onsubmit', 'sendForm(); return false;');
  38. var input = {
  39. name: getField(items.name, 'Your name', false, 'input'), // TODO: configurable prefix
  40. addr: getField(items.addr, 'Your e-mail address', true, 'input'),
  41. subj: getField(items.subj, 'Your message\'s subject', false, 'input'),
  42. text: getField(items.text, 'Your message', false, 'textarea')
  43. };
  44. // Adding nodes to document
  45. el.appendChild(input.name);
  46. el.appendChild(input.addr);
  47. el.appendChild(input.subj);
  48. el.appendChild(input.text);
  49. // Adding submit button
  50. el.appendChild(getSubmitButton('form_subm', 'Send the mail'));
  51. }
  52. // Returns a form field
  53. // id: field HTML identifier
  54. // placeholder: placeholder text
  55. // email: boolean: is it an email field?
  56. // type: 'input' or 'textarea'
  57. // return: a div node containing a label and an input text field
  58. function getField(id, placeholder, email, type) {
  59. var field = document.createElement('div');
  60. field.setAttribute('id', id); // TODO: configurable prefix
  61. field.appendChild(getLabel(id, placeholder, type));
  62. field.appendChild(getInputField(id, placeholder, email, type));
  63. return field;
  64. }
  65. // Returns a label
  66. // id: field HTML identifier
  67. // content: label's inner content
  68. // type: 'input' or 'textarea'
  69. // return: a label node the field's description
  70. function getLabel(id, content, type) {
  71. var label = document.createElement('label');
  72. label.setAttribute('for', id + '_' + type);
  73. label.innerHTML = content;
  74. return label;
  75. }
  76. // Returns an input text field
  77. // id: field HTML identifier
  78. // placeholder: placeholder text, field description
  79. // email: boolean: is it an email field?
  80. // type: 'input' or 'textarea'
  81. // return: an input text or email field (depending on "email"'s value') with an
  82. // HTML id and a placeholder text
  83. function getInputField(id, placeholder, email, type) {
  84. var input = document.createElement(type);
  85. if(!type.localeCompare('input')) { // Set input type if input
  86. if(email) {
  87. input.setAttribute('type', 'email');
  88. } else {
  89. input.setAttribute('type', 'text');
  90. }
  91. }
  92. input.setAttribute('required', 'required');
  93. input.setAttribute('placeholder', placeholder);
  94. input.setAttribute('id', id + '_' + type);
  95. return input;
  96. }
  97. // Returns a submit button
  98. // id: button HTML identifier
  99. // text: button text
  100. // return: a div node containing the button
  101. function getSubmitButton(id, text) {
  102. var submit = document.createElement('div');
  103. submit.setAttribute('id', id);
  104. var button = document.createElement('button');
  105. button.setAttribute('type', 'submit');
  106. button.setAttribute('id', id);
  107. // Disable button's default action
  108. button.setAttribute('onSubmit', 'return false;');
  109. button.innerHTML = text;
  110. submit.appendChild(button);
  111. return submit;
  112. }
  113. // Send form data through the XHR object
  114. // return: nothing
  115. function sendForm() {
  116. xhrSend.open('POST', server + '/send');
  117. xhrSend.setRequestHeader('Content-Type', 'application/json');
  118. xhrSend.send(JSON.stringify(getFormData()));
  119. }
  120. // Fetch form inputs from HTML elements
  121. // return: an object containing all the user's input
  122. function getFormData() {
  123. return {
  124. name: document.getElementById(items.name + '_input').value,
  125. addr: document.getElementById(items.addr + '_input').value,
  126. subj: document.getElementById(items.subj + '_input').value,
  127. text: document.getElementById(items.text + '_textarea').value
  128. }
  129. }