聲明:在發佈之前我不知道imgur上傳系統,但我希望它能幫助你。這篇文章只是通過查看代碼和一些HTML/JS知識來編寫:)
imgur似乎對Firefox(以及一般的Gecko瀏覽器)使用了不同的粘貼選項。確實,索引HTML中有一個upload-global-FF-paste-box
ID的div,其屬性爲contenteditable="true"
。在主JS,我們可以發現一個屬性$FFPasteBox
的初始化:
init: function (a) {
this._ = $.extend({
el: {
$computerButton: $("#gallery-upload-buttons #file-wrapper"),
$webButton: $("#gallery-upload-buttons #url"),
$computerButtonGlobal: $("#upload-global-file"),
$FFPasteBox: $("#upload-global-FF-paste-box") // <--- this line
}
}, a)
},
然後,通過在global.js
文件挖一點,我發現這些功能(見上文)。基本上,這個過程是:
- 等待用戶做出按Ctrl-V行動
- 當它的發射,使之前在焦點看過
contenteditable
格,檢索數據
- 等待數據完全發送(可以使用高分辨率的照片)
- 分析獲得的base64字符串。
下面是來自global.js
提取和註釋的代碼由自己:
// When a paste action is trigger, set the focus on this div and wait the data to be sent
initPasteUploadMozilla: function() {
$(document).on("paste", _.bind(function (a) {
$(a.target).is("input") || this.isInView() && (Imgur.Upload.Index && this.showColorBox(), this._.el.$FFPasteBox.focus(), this.waitForPasteData(this._.el.$FFPasteBox[0]))
}, this))
},
// Listen the user, and waiting that the node is created by the paste action
waitForPasteData: function (a) {
a.childNodes && a.childNodes.length > 0 ? this.processPaste(a) : setTimeout(this.waitForPasteData.bind(this, a), 20)
},
// Check data sent
processPaste: function (a) {
var b = a.innerHTML,
// Check if the thing pasted is a <img tag or a png or at least base64 stuff
c = {
image: -1 != b.indexOf("<img") && -1 != b.indexOf("src="),
base64: -1 != b.indexOf("base64,"),
png: -1 != b.indexOf("iVBORw0K")
};
if (c.image && c.base64 && c.png) {
var d = b.split('<img src="');
d = d[1].split('" alt="">');
var b = {
dataURL: d[0],
file: {
size: this.bytesizeBase64String(d[0].length)
}
};
this.addBase64(b)
} else {
var e = $(a).find("img"),
f = null;
if (e.length > 0 && e.attr("src").length > 0 ? f = e.attr("src") : b.length > 0 && (f = b), null !== f && f.match(this._.urlRegex)) this.queueUrl(f);
else if (null !== f) {
var g = $(f).filter("a");
g.length && g.attr("href").match(this._.urlRegex) ? this.queueUrl(g.attr("href")) : this.showError(this._.messages.urlInvalidError)
}
}
a.innerHTML = ""
},
哦,所以它到底是內容粘貼到一個CONTENTEDITABLE股利。謝謝 :) – Roman