app.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. function copyText() {
  2. var input = document.getElementById("shorturl");
  3. input.select(); // 选中文本
  4. document.execCommand("copy"); // 执行浏览器复制命令
  5. }
  6. var APP = (function(){
  7. var fn = {
  8. // 生成短地址
  9. setUrl: function(self) {
  10. var urlEl = document.getElementById('url'),
  11. tips = 'https://',
  12. request = {"url": urlEl.value};
  13. fn.getJson('api/set.php', true, JSON.stringify(request), function(res) {
  14. if(res.success == 'true') {
  15. //urlEl.className = 'focus';
  16. //urlEl.value = res.content.url;
  17. $res = document.getElementById('shorturl')
  18. $res.className = 'focus';
  19. $res.value = res.content.url;
  20. } else {
  21. urlEl.className = '';
  22. urlEl.value = '';
  23. urlEl.setAttribute('placeholder', res.content);
  24. setTimeout(function() {
  25. urlEl.setAttribute('placeholder', tips);
  26. }, 2000);
  27. }
  28. });
  29. },
  30. // 获取 JSON 数据
  31. getJson: function(url, post, data, callback) {
  32. var xhr = new XMLHttpRequest(),
  33. type = (post) ? 'POST' : 'GET';
  34. xhr.onreadystatechange = function() {
  35. if(xhr.readyState == 4 && xhr.status == 200) {
  36. var json = JSON.parse(xhr.responseText);
  37. callback(json);
  38. } else if(xhr.readyState == 4) {
  39. callback(false);
  40. }
  41. }
  42. xhr.open(type, url, true);
  43. xhr.send(data);
  44. }
  45. },
  46. init = function() {
  47. setTimeout(function() {
  48. var el = document.getElementsByTagName('html')[0];
  49. el.className = 'on';
  50. }, 10);
  51. };
  52. return {fn: fn, init: init}
  53. })();
  54. document.addEventListener('DOMContentLoaded', function() {APP.init();})
  55. console.log('\n %c Program By %c Baoshuo ( @renbaoshuo ) %c https://baoshuo.ren %c\n', 'color: #fadfa3; background: #000000; padding:5px 0;', 'background: #fadfa3; padding:5px 0;', 'background: #ffbf33; padding:5px 0;', '');
  56. console.log('\n GitHub Homepage: https://github.com/renbaoshuo/Shortlink \n \n%c ★ Please give us a star on GitHub! ★ %c \n', 'color: red;', '')