Skip to content
This repository has been archived by the owner on Jun 20, 2021. It is now read-only.

Commit

Permalink
🐛fix #6
Browse files Browse the repository at this point in the history
  • Loading branch information
imgss committed Apr 2, 2019
1 parent abbe011 commit f5dffd5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 33 deletions.
4 changes: 4 additions & 0 deletions app/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"128": "images/icon-128.png"
},
"default_locale": "en",
"background": {
"scripts": ["scripts/background.js"],
"persistent": true
},
"content_scripts":
[
{
Expand Down
38 changes: 38 additions & 0 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
let ajaxUrl = "https://upload.cnblogs.com/imageuploader/CorsUpload";

// https://www.cnblogs.com/MainActivity/p/8550895.html
function dataURLtoFile(dataurl, filename) {//将base64转换为文件
var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, {type:mime});
}

chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {

if (request.contentScriptQuery == "uploadFile") {
let {file, filename, uploadType, fileOptions} = request;
console.log(file)
console.log(file instanceof ArrayBuffer)
let formData = new FormData();
formData.append("imageFile", dataURLtoFile(file, filename));
formData.append("host", "www.cnblogs.com");
formData.append("uploadType", uploadType);
console.log(formData);
fetch(ajaxUrl, {
credentials: 'include',
body: formData,
method: 'POST',
mode: 'cors'
})
.then(response => response.json())
.then(res => sendResponse(res))
.catch(error => {
sendResponse({success: false})
})
return true; // Will respond asynchronously.
}
});
60 changes: 27 additions & 33 deletions app/scripts/cnblogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,11 @@ getSetting().then(items => {
// 上传图片 author: cnblogs.com
(function($) {
let $this;
let $host;
let cursorPosi;
let $textarea = $("#Editor_Edit_EditorBody");
let $ajaxUrl = "https://upload.cnblogs.com/imageuploader/CorsUpload";

$.fn.pasteUploadImage = function(host) {
$.fn.pasteUploadImage = function() {
$this = $(this);
$host = host;
$this.on("paste", function(event) {
let filename, image, pasteEvent, text;
pasteEvent = event.originalEvent;
Expand Down Expand Up @@ -187,34 +184,31 @@ getSetting().then(items => {
};

let uploadFile = function(file, filename, uploadType) {
let formData = new FormData();
formData.append("imageFile", file);
formData.append("host", $host);
formData.append("uploadType", uploadType);

$.ajax({
url: $ajaxUrl,
data: formData,
type: "post",
timeout: 300000,
processData: false,
contentType: false,
dataType: "json",
xhrFields: {
withCredentials: true
},
success: function(data) {
if (data.success) {
return insertToTextArea(filename, data.message);
}
replaceLoadingTest(filename);
alert("上传失败! " + data.message);
},
error: function(xOptions, textStatus) {
replaceLoadingTest(filename);
alert("上传失败! " + xOptions.responseText);
}
});
console.log(file)
var reader = new FileReader();
reader.onload = function(e) {
chrome.runtime.sendMessage(
{
contentScriptQuery: "uploadFile",
file: e.target.result,
filename: filename,
fileOptions: {
type: file.type,
lastModified: file.lastModified
},
uploadType: uploadType
},
res => {
if (res.success) {
return insertToTextArea(filename, res.message);
}
replaceLoadingTest(filename);
alert("上传失败! " + res.message);

});
}
reader.readAsDataURL(file);

};

let insertToTextArea = function(filename, url) {
Expand Down Expand Up @@ -244,7 +238,7 @@ getSetting().then(items => {
};
})(jQuery);

$(".CodeMirror").pasteUploadImage("www.cnblogs.com");
$(".CodeMirror").pasteUploadImage();

// 初始化菜单
let menu = new Menu([
Expand Down

0 comments on commit f5dffd5

Please sign in to comment.