SHARE
TWEET

Untitled

a guest Feb 27th, 2020 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var availabilityElem = document.querySelector('#availability');
  2. var errorsElem = document.querySelector('#errors');
  3. var resultElem = document.querySelector('#result');
  4. availabilityElem.textContent = '';
  5. errorsElem.textContent = '';
  6.  
  7. document.querySelector('#shorten-btn').addEventListener('click', function() {
  8.   let url = document.querySelector('#url-input').value.trim();
  9.   document.querySelector('#url-input').value = url;
  10.   let short = document.querySelector('#short-input').value;
  11.   if (!url) {
  12.     errorsElem.textContent = 'No URL provided!';
  13.     return;
  14.   }
  15.   if (!is_url(url)) {
  16.     errorsElem.textContent = 'Not a valid URL!';
  17.     return;
  18.   }
  19.   let xhr = new XMLHttpRequest();
  20.   let params = new URLSearchParams();
  21.   params.set('url', url);
  22.   if (short) params.set('short', short);
  23.   xhr.open('GET', 'api/create?' + params.toString());
  24.   xhr.onload = function() {
  25.     if (xhr.status < 500) {
  26.       let info = JSON.parse(xhr.responseText);
  27.       if (info['status'] === 'success') {
  28.         resultElem.style.color = '#90EE90';
  29.         let goturl = info['content']['url'];
  30.         resultElem.innerHTML = 'Created! ' + goturl + '';
  31.         errorsElem.textContent = '';
  32.         availabilityElem.textContent = 'Unavailable';
  33.       } else {
  34.         resultElem.style.color = '#f00';
  35.         resultElem.textContent = info['content'];
  36.       }
  37.     } else alert('Error in API!');
  38.   };
  39.   xhr.send();
  40. });
  41.  
  42. // One AJAX request is allowed to be made in one second
  43. // All other requests are ignored
  44. document
  45.   .querySelector('#short-input')
  46.   .addEventListener('input', e =>
  47.     throttleRequest(fetchData.bind(this, e), 1000)
  48.   );
  49.  
  50. // Rename this function to suit your style/convention
  51. function fetchData(e) {
  52.   let short = e.target.value;
  53.   if (!short) {
  54.     availabilityElem.textContent = '';
  55.     return;
  56.   }
  57.  
  58.   let xhr = new XMLHttpRequest();
  59.   xhr.open('GET', '/api/exists/' + encodeURIComponent(short));
  60.   xhr.onload = function() {
  61.     if (xhr.status < 500) {
  62.       let info = JSON.parse(xhr.responseText);
  63.       if (info['status'] === 'success') {
  64.         let ab = info['content']['exists'];
  65.         availabilityElem.textContent = ab ? 'Unavailable!' : 'Available!';
  66.         availabilityElem.style.color = ab ? '#f00' : '#90EE90';
  67.       } else {
  68.         resultElem.style.color = '#f00';
  69.         resultElem.textContent = info['content'];
  70.       }
  71.     } else alert('Error in API!');
  72.   };
  73.   xhr.send();
  74. }
  75.  
  76. function throttleRequest(requestFunction, interval) {
  77.   // This variable initializes only once because of closure
  78.   var lastCallTime = 0;
  79.  
  80.   // This function is executed every time input event is triggered
  81.   return function() {
  82.     var now = Date.now();
  83.  
  84.     // Compare last call time + interval with the current time to see if call can be made
  85.     if (lastCallTime + interval < now) {
  86.       // Set current time to be lastCallTime
  87.       lastCallTime = now;
  88.       // Call callback function(requestFunction) and pass any argument it may recieve
  89.       return requestFunction.apply(this, arguments);
  90.     }
  91.   };
  92. }
  93.  
  94. function is_url(str) {
  95.   regexp = /^(?:(?:https?|ftp):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/;
  96.   if (regexp.test(str)) return true;
  97.   return false;
  98. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top