function zE(a, b) {
var c = new FormData;
c.append("via", "xhr2");
c.append("upload_file", b);
var d;
d = $.ajaxSettings.xhr();
d.withCredentials = i;
var f = $.ajax({url: a.Vz,data: c,processData: l,contentType: l,xhr: function() {
return d
},type: "POST"}).done(function(a) {
啊,找到了,這裡應該就是發送圖片數據的地方,而且用了 FormData 這個 HTML5 特性
簡單說就是 ajax 以前只能向伺服器發送文本,而 HTML5 提供的 XMLHttpRequest Level2 現在支持發送二進位數據了,這裡的 c.append("upload_file", b) 裡面 b 應該就是那個圖片的二進位數據
contenteditable 使相應元素在頁面呈現 可編輯/不可編輯 狀態see HTML 5 全局 contenteditable 屬性
&&
一個推測(尚未嚴格驗證,請知者指教):
複製一幅圖片到系統粘貼板中 會有以下內容在剪貼板中生成(可能缺少某些項):來源(類似 src ),標題(類似title),內容(可以是二進位的)根據這三項的有無和可訪問性,我們考慮以下三種剪貼板圖像(獲取方式下詳):type 1. public_uri/local_img(hvae src, title, content; local can access; )type 2. private_uri(have src, title, content; local can not access; )type 3. print_screen(have content; no src, title; local can access; )對於前面提到的&&
type 1, 將一個 IMG標籤寫入DIV,並設置相應的屬性,如 src, title。頁面上會顯示出這幅圖像,真實數據是從src指定的uri讀取的。
type 2, 同1,但是由於沒有URI的訪問許可權,頁面上會顯示為一個無法顯示的圖像。
type 3, 由於沒有 src, titile等內容,不會生成IMG元素,所以粘貼沒有任何效果 (注意:這只是IE的行為,其他瀏覽器可能有不同的行為,下詳)
FireFox24:
type 1, 同IE
type 2, 同IE type 3, 雖然沒有src, title。但是有content在。firefox還是會生成一個IMG標籤。 標籤的title等屬性為空,src屬性為圖片的數據內容,如 &
Chrome31:
type 1, 同IE type 2, 同IE
type 3, 同IE
再來看一下知乎編輯框的粘貼情況,同樣用上面三種粘貼板圖像來測試:IE: 同上(1 ok, with origion url; 2 not ok, with origion url; 3 not ok, no response)FF: 同上(1 ok, with origion url; 2 not ok, with origion url; 3 ok, with data in src)Chrome: type 1 (ok, with new url )!
type 2 (ok, with new url )!! (圖中是一張qq截圖)
type 3 (ok, with new url )!!
全都是new uri,並且是指向知乎的伺服器的,說明圖片已經從剪貼板上傳到伺服器上了。看來知乎的編輯框針對chrome 有特殊處理,或者說chrome提供了對粘貼板圖片進行特殊處理的途徑。參考了 Paste Image From Clipboard With Javascript and MooTools後,得到如下信息:chrome提供了訪問粘貼板中二進位數據的介面(clipboardData.items),並提供了將其轉換為文件的方法(getAsFile)。demo 如下(裡面用到的圖像請自行替換):
&
&
&test chrome paste image&
&
&
window.onload=function() {
function paste_img(e) {
if ( e.clipboardData.items ) {
// google-chrome
alert("support clipboardData.items(chrome ...)");
ele = e.clipboardData.items
for (var i = 0; i &< ele.length; ++i) {
if ( ele[i].kind == "file" ele[i].type.indexOf("image/") !== -1 ) {
var blob = ele[i].getAsFile();
window.URL = window.URL || window.webkitURL;
var blobUrl = window.URL.createObjectURL(blob);
console.log(blobUrl);
var new_img= document.createElement("img");
new_img.setAttribute("src", blobUrl);
var new_img_intro = document.createElement("p");
new_img_intro.innerHTML = "the pasted img url(open it in new tab): & &" + blobUrl + "&";