Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat]支持background的文件上传 #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion background.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,16 @@ chrome.runtime.onMessage.addListener(function (request, _, cb) {
}
})


function dataURLtoFile(dataUrl, filename) {
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 });
}

function sendAjax(req, successFn, errorFn) {
var formDatas;
var xhr = new XMLHttpRequest();
Expand All @@ -219,7 +229,18 @@ function sendAjax(req, successFn, errorFn) {
if (!req.headers['Content-Type'] || req.headers['Content-Type'] == 'application/x-www-form-urlencoded') {
req.headers['Content-Type'] = 'application/x-www-form-urlencoded';
req.data = formUrlencode(req.data);
} else if (typeof req.data === 'object' && req.data) {
} else if (req.headers['Content-Type'] === 'multipart/form-data') {
delete req.headers['Content-Type'];
let formDatas = new FormData();
for (var item of req.formDatas) {
if (item.is_file) {
formDatas.append(item.name, dataURLtoFile(item.value, item.fileName))
}else {
formDatas.append(item.name, item.value);
}
}
req.data = formDatas;
} else if (typeof req.data === 'object' && req.data) {
req.data = JSON.stringify(req.data);
}
}else{
Expand Down
77 changes: 26 additions & 51 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function (win) {

if(!document.getElementById('cross-request-sign')){
if (!document.getElementById('cross-request-sign')) {
return;
}

Expand Down Expand Up @@ -195,21 +195,25 @@
var interval;



var customEvent = document.createEvent('Event');
customEvent.initEvent('myCustomEvent', true, true);
function fireCustomEvent(data) {
hiddenDiv = document.getElementById(container);
hiddenDiv.innerText = data
hiddenDiv.dispatchEvent(customEvent);
}

function run(req) {
if (!req) return;
if (typeof req === 'string') req = { url: req }

var newId = getid();
data = {
id: newId,
res: null,
req: req
}
data = encode(data);
var newId = getid();
var div = createNode('div', {
_id: newId,
status: INITSTATUS
}, yRequestDom);
div.innerText = data;
yRequestMap[newId] = {
id: newId,
status: INITSTATUS,
Expand All @@ -224,53 +228,24 @@
}
}
}
monitor();
fireCustomEvent(data);
}



function monitor() {
if (interval) return;
interval = setInterval(function () {
var queueDom = yRequestDom.childNodes;
if (!queueDom || queueDom.length === 0) {
interval = clearInterval(interval);
yRequestDom.addEventListener('reponseEvent', function () {
var text = yRequestDom.innerText;
if (text) {
var data = decode(yRequestDom.innerText);
var id = data.id;
var res = data.res;
if (res.status === 200) {
yRequestMap[id].success(res.body, res.header, data);
} else {
yRequestMap[id].error(res.statusText, res.header, data);
}

try {
for (var i = 0; i < queueDom.length; i++) {
try {
var dom = queueDom[i];
if (+dom.getAttribute('status') === ENDSTATUS) {
var text = dom.innerText;
if (text) {
var data = decode(dom.innerText);
var id = dom.getAttribute('_id');
var res = data.res;
if (res.status === 200) {
yRequestMap[id].success(res.body, res.header, data);
} else {
yRequestMap[id].error(res.statusText, res.header, data);
}
dom.parentNode.removeChild(dom);
} else {
dom.parentNode.removeChild(dom);
}

}
} catch (err) {
console.error(err.message);
dom.parentNode.removeChild(dom);
}
}
} catch (err) {
console.error(err.message);
interval = clearInterval(interval);
}


}, 50)
}
} else {
}
});

win.crossRequest = run;
if (typeof define == 'function' && define.amd) {
Expand Down
Loading