Advertisement
Guest User

main.js

a guest
Oct 18th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function init() {
  2.     var url = window.location.href;
  3.     extractUriParams(url, extractionSchema, 'extraction_form');
  4. }
  5.  
  6. function extractUriParams(uri, extractionSchema, parentElementId) {
  7.     var uriParts = getUriParts(uri);
  8.  
  9.     for (var i = 0; i < extractionSchema.length; i++) {
  10.         var part = extractionSchema[i];
  11.         if (isString(part)) {
  12.             var palceholder = placeholderDictionary[part] || part;
  13.             createExtractionField(part, uriParts[part], palceholder, parentElementId);
  14.         }
  15.         else if (isArray(part)) {
  16.             if (part.length === 1) {
  17.                 var placeholder = placeholderDictionary[part[0]] || part[0];
  18.                 createExtractionField(part[0], uriParts[part[0]], placeholder, parentElementId);
  19.             }
  20.             else {
  21.                 var keys = [];
  22.                 var values = [];
  23.                 var placeholderParts = [];
  24.                 for (var j = 0; j < part.length; j++) {
  25.                     if (isString(part[j])) {
  26.                         if (j + 1 < part.length && isArray(part[j + 1])) {
  27.                             var subkeys = [];
  28.                             var subvalues = [];
  29.                             for (var k = 0; k < part[j + 1].length; k++) {
  30.                                 subkeys.push(part[j + 1][k]);
  31.                                 subvalues.push(uriParts[part[j]][part[j + 1][k]]);
  32.                             }                  
  33.                             var parentPlaceholder = placeholderDictionary[part[j]] || [part[j]];
  34.                             var placeholderPart = parentPlaceholder + ' (' + subkeys.join(', ') + ')';
  35.                             placeholderParts.push(placeholderPart);
  36.                             keys.push(part[j] + keyLevelIndent + subkeys.join(bottomKeySplitter));
  37.                             values.push(subvalues.join(bottomValueSplitter));
  38.                         }
  39.                         else {
  40.                             var placeholderPart = placeholderDictionary[part[j]] || part[j];
  41.                             placeholderParts.push(placeholderPart);
  42.                             keys.push(part[j]);
  43.                             values.push(uriParts[part[j]]);
  44.                         }
  45.                     }
  46.                 }
  47.                 createExtractionField(keys, values, placeholderParts, parentElementId);
  48.             }
  49.         }
  50.     }
  51. }
  52.  
  53. function getPartPlaceholder(part) {
  54.     var placeholder = '';
  55.  
  56.     if (isString(part)) {
  57.         return placeholderDictionary[part] || part;
  58.     }
  59.     else if (isArray(part)) {
  60.        
  61.     }
  62.  
  63.     return placeholder;
  64. }
  65.  
  66. function createExtractionField(label, value, placeholder, parentElementId) {
  67.     var parentEl = document.getElementById(parentElementId);
  68.     var divEl = document.createElement('div');
  69.     label = formatLabel(label);
  70.     value = formatValue(value);
  71.     placeholder = formatPlaceholder(placeholder);
  72.  
  73.     var labelEl = document.createElement('label');
  74.     labelEl.innerHTML = label + ':';
  75.     labelEl.setAttribute('for', label);
  76.  
  77.     var inputEl = document.createElement('input');
  78.     inputEl.value = value;
  79.     inputEl.setAttribute('id', label);
  80.     inputEl.setAttribute('type', 'text');
  81.     inputEl.setAttribute('placeholder', placeholder);
  82.    
  83.     divEl.appendChild(labelEl);
  84.     divEl.appendChild(inputEl);
  85.     parentEl.appendChild(divEl);
  86. }
  87.  
  88. function getUriParts(uri) {
  89.     var link = document.createElement('a');
  90.     link.setAttribute('href', uri);
  91.    
  92.     return {
  93.         full: uri,
  94.         base: link.hostname,
  95.         protocol: link.protocol.replace(':', ''),
  96.         port: link.port,
  97.         path: link.pathname,
  98.         hash: link.hash.replace('#', ''),
  99.         param: getUriParamDictionary(uri)
  100.     }
  101. }
  102.  
  103. function getUriParamDictionary(uri) {
  104.     var paramRegex = new RegExp(/[&?;]([^&?#;]*?)=([^&?#;]*)/g);
  105.     var paramDictionary = {};
  106.  
  107.     while (match = paramRegex.exec(uri)) {
  108.         paramDictionary[match[1]] = match[2];
  109.     }
  110.  
  111.     return paramDictionary;
  112. }
  113.  
  114. function formatLabel(label) {
  115.     if (isString(label)) {
  116.         label = keyPrepend + label;
  117.     }
  118.     else if (isArray(label)) {
  119.         label = keyPrepend + label.join(topKeySplitter)
  120.     }
  121.  
  122.     return label;
  123. }
  124.  
  125. function formatValue(value) {
  126.     if (value == undefined) {
  127.         value = '';
  128.     }
  129.     else if (isArray(value)) {
  130.         value = value.join(topValueSplitter);
  131.     }
  132.  
  133.     return value;
  134. }
  135.  
  136. function formatPlaceholder(placeholder) {
  137.     if (isArray(placeholder)) {
  138.         placeholder = placeholder.join(' & ');
  139.     }
  140.  
  141.     return placeholder;
  142. }
  143.  
  144. function isString(object) {
  145.     return typeof object === 'string' || object instanceof String;
  146. }
  147.  
  148. function isArray(object) {
  149.     return object && typeof object === 'object' && object.constructor === Array;
  150. }
  151.  
  152. var placeholderDictionary = {
  153.     full: 'full url',
  154.     base: 'base url',
  155.     protocol: 'url protocol',
  156.     port: 'url port',
  157.     path: 'url path',
  158.     hash: 'url hash',
  159.     param: 'url parameters'
  160. }
  161.  
  162. var extractionSchema = [
  163.     "full",
  164.     "base",
  165.     "protocol",
  166.     ["param", ["utm_source"]],
  167.     ["param", ["utm_source", "utm_medium"]],
  168.     ["protocol", "base"]
  169. ];
  170.  
  171. var keyPrepend = 'URL-';
  172. var topKeySplitter = '-';
  173. var bottomKeySplitter = '__';
  174. var keyLevelIndent = ':';
  175. var topValueSplitter = ',';
  176. var bottomValueSplitter = ',';
  177.  
  178. window.onload = init;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement