Drop Images into Tieba Editor 3

贴吧拖放粘贴上传图片 huhu修改

  1. // ==UserScript==
  2. // @name Drop Images into Tieba Editor 3
  3. // @description 贴吧拖放粘贴上传图片 huhu修改
  4. // @version 3.1.1
  5. // @grant none
  6. // @include http://tieba.baidu.com/f*
  7. // @include http://tieba.baidu.com/p/*
  8. // @include http://tieba.baidu.com.cn/f*
  9. // @include http://tieba.baidu.com.cn/p/*
  10. // @include http://tieba.baidu.cn/f*
  11. // @include http://tieba.baidu.cn/p/*
  12. // @run-at document-end
  13. // @copyright 5B4B huhu修改
  14. // @icon http://imgsrc.baidu.com/forum/pic/item/911e12087bf40ad13d03ebde552c11dfa8ecce89.jpeg
  15. // @namespace 5B4B
  16. // ==/UserScript==
  17.  
  18.  
  19. 'use strict';
  20.  
  21.  
  22. function init() {
  23. 'use strict';
  24. //////
  25. //// Basic Functions
  26. //
  27. function failedCallback() {
  28. if ( gFilesNumSum > 0 ) {
  29. --gFilesNumSum;
  30. }
  31. }
  32.  
  33. function progressCallback(e) {
  34. if ( !e.lengthComputable ) {
  35. progress.hide();
  36. return;
  37. }
  38. progress.show();
  39. progress.update(e.loaded / e.total / gFilesNumSum, gFilesNum, gFilesNumSum);
  40. }
  41.  
  42. function upload(blob, type, name) {
  43. var xhr = new XMLHttpRequest(),
  44. size = blob.size;
  45.  
  46. gUploading = true;
  47. xhr.open('GET', 'http://tieba.baidu.com/dc/common/imgtbs?t=' + new Date().getTime(), true);
  48. xhr.onload = function () {
  49. var res = $.json.decode(this.responseText);
  50.  
  51. if ( res.no !== 0 ) {
  52. failedCallback();
  53. }
  54.  
  55. var tbs = res.data.tbs,
  56. formData = new FormData();
  57.  
  58. xhr.abort();
  59. xhr.open('POST', 'http://upload.tieba.baidu.com/upload/pic?is_wm=1&tbs=' + tbs, true);
  60. xhr.withCredentials = true;
  61. xhr.upload.onprogress = progressCallback;
  62. xhr.onload = function () {
  63. var res = $.json.decode(this.responseText);
  64.  
  65. ++gFilesNum;
  66. if ( res.error_code !== 0 ) {
  67. showMsg(name + ' \u56FE\u7247\u4E0A\u4F20\u5931\u8D25\uFF01');
  68. } else {
  69. var pic_url = 'http://imgsrc.baidu.com/forum/pic/item/' + res.info.pic_id_encode + '.' + type;
  70. test_editor.execCommand('inserthtml', '<img class="BDE_Image" type="2" src="http://imgsrc.baidu.com/forum/pic/item/' + res.info.pic_id_encode + '.jpg">');
  71. //editor.execCommand('insertimage', [ pic_url ], '2');
  72. }
  73. if ( gFilesNum === gFilesNumSum ) {
  74. gUploading = false;
  75. progress.hide();
  76. }
  77. };
  78. formData.append('fid', PageData.forum.id);
  79. formData.append('Filename', name);
  80. formData.append('file', blob);
  81. xhr.send(formData);
  82. };
  83. xhr.onerror = failedCallback;
  84. xhr.send();
  85. }
  86.  
  87. function uploadImages(files) {
  88. Array.prototype.forEach.call(files, function (file) {
  89. if ( /^image\/(png|jpe?g|gif)$/i.test(file.type) ) {
  90. var type = RegExp.$1,
  91. r = new FileReader();
  92.  
  93. r.onload = function () {
  94. var blob = new Blob([ new Uint8Array(r.result) ], {
  95. type: file.type
  96. });
  97.  
  98. if ( !gUploading ) {
  99. gFilesNum = 0;
  100. }
  101. upload(blob, type, file.name);
  102. };
  103. r.readAsArrayBuffer(file);
  104. }
  105. });
  106. }
  107.  
  108. function createProgressBar(id) {
  109. var div = document.createElement('div'),
  110. label = document.createElement('label'),
  111. p = document.createElement('progress');
  112.  
  113. div.id = id;
  114. div.style.display = 'none';
  115. p.max = 1;
  116. p.value = 0;
  117. p.style.width = '200px';
  118. p.style.marginRight = '5px';
  119. div.appendChild(p);
  120. div.appendChild(label);
  121. div.update = function (percent, num, sum) {
  122. p.value = percent;
  123. label.textContent = '\u5DF2\u4E0A\u4F20 ' + num + '/' + sum + '\u7684\u56FE\u7247';
  124. };
  125. div.hide = function () {
  126. this.style.display = 'none';
  127. };
  128. div.show = function () {
  129. this.style.display = '';
  130. };
  131. return div;
  132. }
  133.  
  134.  
  135. //////
  136. //// Initialization
  137. //
  138. try {
  139. var isTarget = /^http:\/\/tieba\.baidu\.com\/(?:f\?.*?\b(?:kw|kz|z)=[^&]|p\/)/i;
  140.  
  141. if ( !isTarget.test(location.href) ) {
  142. return;
  143. }
  144.  
  145. var gFilesNumSum = 0,
  146. gFilesNum = 0,
  147. gUploading = false,
  148. intprogress = true;
  149. var usWin = window,
  150. $ = usWin.$,
  151. PageData = usWin.PageData,
  152. editor = usWin.test_editor,
  153. textarea = document.querySelector('#ueditor_replace'),
  154. ttp=document.querySelector('.tb_rich_poster_container'),pt='#ueditor_replace',
  155. progress = createProgressBar('DITE2_progress'),
  156. showMsg = function (msg) {
  157. var dialog = usWin.$.dialog.open(msg, {
  158. showTitle: false,
  159. modal: false,
  160. top: textarea.getClientRects().item(0).top + window.scrollY,
  161. left: textarea.getClientRects().item(0).left + window.scrollX,
  162. fixed: false
  163. });
  164. setTimeout(function (){
  165. dialog.element.fadeOut(400, function () {
  166. dialog.close();
  167. });
  168. }, 1000);
  169. };
  170.  
  171. //textarea.parentNode.insertBefore(progress, textarea.nextElementSibling);
  172.  
  173. window.addEventListener('dragover', function (e) {
  174. e.preventDefault();
  175. }, true);
  176.  
  177. ttp.addEventListener('dragover', function (e) {
  178. e.preventDefault();
  179. }, true);
  180. ttp.addEventListener('drop', function (e) {
  181. if(intprogress){textarea = document.querySelector('#ueditor_replace');textarea.parentNode.insertBefore(progress, textarea.nextElementSibling);intprogress=false;}
  182. var files = e.dataTransfer.files;
  183.  
  184. if ( files.length === 0 ) {
  185. return;
  186. }
  187.  
  188. e.preventDefault();
  189. if ( gUploading ) {
  190. gFilesNumSum += files.length;
  191. showMsg('\u51C6\u5907\u518D\u4E0A\u4F20 ' + files.length + ' \u5E45\u56FE\u7247\u2026\u2026');
  192. } else {
  193. gFilesNum = 0;
  194. gFilesNumSum = files.length;
  195. showMsg('\u51C6\u5907\u4E0A\u4F20 ' + files.length + ' \u5E45\u56FE\u7247\u2026\u2026');
  196. }
  197. uploadImages(files);
  198. },false);
  199. ttp.addEventListener('paste', function (e) {
  200. if(intprogress){textarea = document.querySelector('#ueditor_replace');textarea.parentNode.insertBefore(progress, textarea.nextElementSibling);intprogress=false;}
  201. if ( !e.clipboardData ) {
  202. showMsg('\u4F60\u7684\u706B\u72D0\u6D4F\u89C8\u5668\u6682\u65F6\u4E0D\u652F\u6301\u526A\u8D34\u677F\u7C98\u8D34\u4E0A\u4F20\u529F\u80FD\u3002');
  203. throw 'DITE2: e.clipboardData, Firefox not supported.';
  204. return;
  205. }
  206. var clipboard = e.clipboardData,
  207. items = clipboard.items;
  208.  
  209. Array.prototype.forEach.call(items, function (item, i) {
  210. if ( item.kind === 'file' ) {
  211. e.preventDefault();
  212. if ( gUploading ) {
  213. ++gFilesNumSum;
  214. showMsg('\u51C6\u5907\u518D\u4E0A\u4F20 1 \u5E45\u56FE\u7247\u2026\u2026');
  215. } else {
  216. gFilesNumSum = 1;
  217. showMsg('\u51C6\u5907\u4E0A\u4F20 1 \u5E45\u56FE\u7247\u2026\u2026');
  218. }
  219. uploadImages([ item.getAsFile() ]);
  220. }
  221. });
  222. });
  223. } catch (err) {
  224. throw new Error('DITE3: ' + err.message);
  225. }
  226. }
  227.  
  228.  
  229. var script = document.createElement('script');
  230. script.id = '__5B4B_DITE3__';
  231. script.charset = 'utf-8';
  232. script.type = 'text/javascript';
  233. script.innerHTML = 'try{(' + init.toString() + ')()}catch(e){}';
  234. document.body.appendChild(script);