Created
December 11, 2013 13:02
-
-
Save wenketel/7910047 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name 图片粘贴上传正式版 | |
// @namespace http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul | |
// @include http://tieba.baidu.com/* | |
// @version 1 | |
// @grant GM_xmlhttpRequest | |
// @grant unsafeWindow | |
// @grant GM_addStyle | |
// @run-at document-end | |
// ==/UserScript== | |
GM_addStyle("img[src='http://tieba.baidu.com']{display:none;}"); | |
if (!(unsafeWindow.FlashImageLoader)) { | |
var sc = document.createElement('script'); | |
sc.setAttribute('src', 'http://static.tieba.baidu.com/tb/static-frs/component/sign_shai/flash_image_loader.js'); | |
document.body.appendChild(sc); | |
}; | |
setTimeout(function () { | |
if(!document.querySelector('#ueditor_replace')) | |
setTimeout(arguments.callee,200); | |
else | |
document.querySelector('#ueditor_replace').addEventListener('paste', function (e) { | |
e.stopPropagation(); | |
setTimeout(function () { | |
pasteImg() | |
}, 200); | |
}, true); | |
},200); | |
function pasteImg() { | |
var txt = document.querySelector('#ueditor_replace').innerHTML; | |
if (/src\=\"data\:image\/[^"].*?\"/.test(txt)) { | |
var img = txt.match(/src\=\"(data\:image\/[^"].*?)\"/); | |
if (img) { | |
var src = img[1]; | |
var imgs = document.querySelectorAll('#ueditor_replace img'); | |
for(var i=0;i<imgs.length;i++){ | |
if(imgs[i].src.indexOf("data:image")==0){ | |
imgs[i].outerHTML = '<span class="waitimage" style="position:relative;display:inline-block;"><img src="'+src+'"><span style="position:absolute;top:0;left:0;display:block;height:100%;width:100%;background:rgba(255,255,255,.8) url(data:image/gif;base64,R0lGODlhEAAQAOUdAOvr69HR0cHBwby8vOzs7PHx8ff397W1tbOzs+Xl5ebm5vDw8PPz88PDw7e3t+3t7dvb2+7u7vX19eTk5OPj4+rq6tbW1unp6bu7u+fn5+jo6N/f3+/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Q22olUEcroJB66KD8FNCjUrlxWpTUEAIfkEBTAAOQAsAAAAABAAEAAABobAnDAnWRAIOULBMBwyHoqENCdVLBhOQnW6fWBznC13G8nZchXNllql5Bg2xA1cZQOwShwCMdDkLgk5GVgAUgAie3syVDkTbFIaiIkIJ0NiGnp7HiNonRVVAHEuFjlQFVQVAI0JCzYjrKCPZQWnf1unYkMVWrFbBLVoUIaPD8C6CwCnAMhNQQA7) center no-repeat;"></span></span>'; | |
break; | |
} | |
} | |
upload(src); | |
} | |
} | |
}; | |
function upload(f) { //f是base64数据 | |
function uploadedHandler(a, d) { | |
try { | |
var c = JSON.parse(d); //d是返回的text c是json对象 | |
if (c.error_code != 0) { | |
alert("图片化失败") | |
} else { | |
var b = c.info.pic_id_encode; //c是json数据;b是地址信息; | |
var e = 'http://imgsrc.baidu.com/forum/pic/item/' + b + '.png'; //e是img完整地址; | |
document.querySelector('#ueditor_replace').focus(); | |
unsafeWindow.FlashImageLoader.unbind('uploadComplete', arguments.callee) | |
var w = document.querySelector('.waitimage'), | |
width = Math.min(w.firstChild.width,560), | |
height = Math.round(w.firstChild.height*(width/w.firstChild.width)); | |
w.outerHTML = '<img class="BDE_Image" src="'+e+'" width="'+width+'" height="'+height+'" unselectable="on" pic_type="0"/>'; | |
} | |
} catch (err) { | |
alert(err); | |
} | |
} | |
function callback(a) { | |
try { | |
unsafeWindow.FlashImageLoader.bind('uploadComplete', uploadedHandler); | |
unsafeWindow.FlashImageLoader.uploadBase64('http://upload.tieba.baidu.com/upload/pic', f.replace(/^.*?base64,/, ''), { | |
tbs: a | |
}) | |
} catch (err) { | |
alert(err); | |
}; | |
} //事先返回一个图片ID再返回 | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url: 'http://tieba.baidu.com/dc/common/imgtbs', | |
onload: function (response) { | |
callback(JSON.parse(response.responseText).data.tbs); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment