diff options
| author | Melonai <einebeere@gmail.com> | 2020-05-07 04:29:30 +0200 |
|---|---|---|
| committer | Melonai <einebeere@gmail.com> | 2020-05-07 04:29:30 +0200 |
| commit | 1e06820480fb2d9cf9db1373817d5980a891d5b4 (patch) | |
| tree | 63d0d45190f95b40f20dfd8a718a912434286db3 /client/static/main.js | |
| parent | 385fcdd46b536d28094a96f3f6be0e87496277a1 (diff) | |
| download | shorest-1e06820480fb2d9cf9db1373817d5980a891d5b4.tar.zst shorest-1e06820480fb2d9cf9db1373817d5980a891d5b4.zip | |
interaction between API and client
Diffstat (limited to 'client/static/main.js')
| -rw-r--r-- | client/static/main.js | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/client/static/main.js b/client/static/main.js new file mode 100644 index 0000000..aa823c0 --- /dev/null +++ b/client/static/main.js @@ -0,0 +1,30 @@ +$(document).ready(function() { + $('#form').on('submit', onFormSubmit); + $('#url').on({'input': inputUpdate, 'paste': pasteTrim}); +}); + +function onFormSubmit() { + const urlField = document.getElementById('url'); + const data = JSON.stringify({'url': 'https://' + urlField.value}); + $.ajax('/', {method: 'POST', data: data, contentType: 'application/json'}).then(function (r) { + urlField.value = 'sho.rest/' + r.hash; + }) + return false; +} + +function inputUpdate() { + const userInput = document.getElementById('url').value; + if (!validate({website: 'https://' + userInput}, {website: {url: true}})) { + $('#form-group').css('border-color', '#E0E0E0'); + } else { + $('#form-group').css('border-color', '#FFBCBC'); + } +} + +function pasteTrim() { + const pattern = /^https?:\/\//; + setTimeout(() => { + const element = $('#url'); + element.value = element.value.replace(pattern, ''); + }, 0); +} \ No newline at end of file |
