mirkoczat wrzucanie obrazków

przeciągnij zdjęcie na okno mirkoczata, żeby je automatycznie wrzucić.

  1. // ==UserScript==
  2. // @name mirkoczat wrzucanie obrazków
  3. // @namespace https://mirkoczat.pl/t
  4. // @description przeciągnij zdjęcie na okno mirkoczata, żeby je automatycznie wrzucić.
  5. // @include http://mirkoczat.pl/t/*
  6. // @include https://mirkoczat.pl/t/*
  7. // @include https://api.imgur.com/*
  8. // @version 1.0
  9. // @icon https://mirkoczat.pl/images/icon.png
  10. // @locale pl
  11. // ==/UserScript==
  12.  
  13. function append (text) {
  14. var c = $('#message-input').val();
  15. if (c.length > 0 && c.substring(c.length - 1) !== ' ') c += ' ';
  16. c += text;
  17.  
  18. $('#message-input').val(c);
  19. }
  20.  
  21. function info (text) {
  22. $('#message-input').attr('placeholder', text);
  23. }
  24.  
  25. var uploading = 0;
  26. function upload (file) {
  27. uploading++;
  28. $("body").css("cursor", "progress");
  29.  
  30. var fd = new FormData();
  31. fd.append('image', file);
  32.  
  33. var xhr = new XMLHttpRequest();
  34.  
  35. xhr.onreadystatechange = function () {
  36. if (xhr.readyState !== 4)
  37. return;
  38.  
  39. uploading--;
  40. if (uploading === 0) {
  41. info('');
  42. $("body").css("cursor", "default");
  43. }
  44.  
  45. if (xhr.status !== 200)
  46. return append('[nie można wrzucić obrazka]');
  47.  
  48. var res = JSON.parse(xhr.responseText);
  49. if (!res.success)
  50. return append('[nie można wrzucić obrazka]');
  51.  
  52. append(res.data.link);
  53. info('');
  54. };
  55.  
  56. xhr.upload.onprogress = function (e) {
  57. var progress = e.loaded / e.total;
  58.  
  59. info('[' + (progress * 100).toFixed(0) + '%]');
  60. };
  61.  
  62. xhr.open('POST', 'https://api.imgur.com/3/image');
  63. xhr.setRequestHeader('Authorization', 'Client-ID ac0c5524d5f66a4');
  64. xhr.send(fd);
  65. }
  66.  
  67. document.body.addEventListener('dragover', function (e) {
  68. e.stopPropagation();
  69. e.preventDefault();
  70. e.dataTransfer.dropEffect = 'copy';
  71.  
  72. info('[upuść, aby wrzucić obrazek]');
  73. });
  74.  
  75. document.body.addEventListener('dragleave', function (e) {
  76. info('');
  77. });
  78.  
  79. document.body.addEventListener('drop', function (e) {
  80. info('');
  81.  
  82. e.stopPropagation();
  83. e.preventDefault();
  84.  
  85. var files = e.dataTransfer.files;
  86. for (var i = 0; i < files.length; i++) {
  87. upload(files[i]);
  88. }
  89. });

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址