Fix binary data, ./ relative path syntax, and IE
This commit is contained in:
@@ -2,39 +2,31 @@
|
|||||||
|
|
||||||
function compress(json) {
|
function compress(json) {
|
||||||
return LZString.compressToBase64(JSON.stringify(json))
|
return LZString.compressToBase64(JSON.stringify(json))
|
||||||
.replace(/\+/g, `-`)
|
.replace(/\+/g, '-')
|
||||||
.replace(/\//g, `_`)
|
.replace(/\//g, '_')
|
||||||
.replace(/=+$/, ``);
|
.replace(/=+$/, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
function fetchResource(resource) {
|
function fetchResource(resource) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise(function (resolve, reject) {
|
||||||
const isImage = /\.(png|jpe?g|gif|tiff)$/.test(resource);
|
const isImage = /\.(png|jpe?g|gif|tiff)$/.test(resource);
|
||||||
const xhr = new XMLHttpRequest();
|
|
||||||
xhr.open('GET', resource);
|
|
||||||
if (isImage) {
|
if (isImage) {
|
||||||
xhr.responseType = 'blob';
|
resolve ({
|
||||||
|
isBinary: true,
|
||||||
|
content: new URL(resource, window.location.href).href
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
|
const xhr = new XMLHttpRequest();
|
||||||
|
xhr.open('GET', resource);
|
||||||
xhr.responseType = 'text';
|
xhr.responseType = 'text';
|
||||||
}
|
xhr.addEventListener('load', function () {
|
||||||
xhr.addEventListener('load', () => {
|
|
||||||
if (isImage) {
|
|
||||||
const a = new FileReader();
|
|
||||||
a.addEventListener('load', e => {
|
|
||||||
resolve ({
|
|
||||||
isBinary: true,
|
|
||||||
content: e.target.result
|
|
||||||
})
|
|
||||||
});
|
|
||||||
a.readAsDataURL(xhr.response);
|
|
||||||
} else {
|
|
||||||
resolve ({
|
resolve ({
|
||||||
content: xhr.response
|
content: xhr.response
|
||||||
})
|
});
|
||||||
}
|
});
|
||||||
});
|
xhr.addEventListener('error', reject);
|
||||||
xhr.addEventListener('error', reject);
|
xhr.send();
|
||||||
xhr.send();
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,7 +34,7 @@
|
|||||||
if (codepenButton) {
|
if (codepenButton) {
|
||||||
const form = document.getElementById('codepen-form');
|
const form = document.getElementById('codepen-form');
|
||||||
codepenButton.href = form.action;
|
codepenButton.href = form.action;
|
||||||
codepenButton.addEventListener('click', function(event) {
|
codepenButton.addEventListener('click', function (event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
const html = document.getElementById('example-html-source').innerText;
|
const html = document.getElementById('example-html-source').innerText;
|
||||||
const js = document.getElementById('example-js-source').innerText;
|
const js = document.getElementById('example-js-source').innerText;
|
||||||
@@ -51,15 +43,27 @@
|
|||||||
const pkgJson = document.getElementById('example-pkg-source').innerText;
|
const pkgJson = document.getElementById('example-pkg-source').innerText;
|
||||||
|
|
||||||
const unique = new Set();
|
const unique = new Set();
|
||||||
const localResources = (js.match(/'data\/[^']*/g) || [])
|
const localResources = (js.match(/'(\.\/)?data\/[^']*/g) || [])
|
||||||
.concat(js.match(/'resources\/[^']*/g) || [])
|
.concat(js.match(/'(\.\/)?resources\/[^']*/g) || [])
|
||||||
.map(f => f.slice(1))
|
.map(
|
||||||
.filter(f => unique.has(f) ? false : unique.add(f));
|
function (f) {
|
||||||
|
return f.replace(/^'(\.\/)?/, '');
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.filter(
|
||||||
|
function (f) {
|
||||||
|
return unique.has(f) ? false : (unique.add(f) || unique);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const promises = localResources.map(resource => fetchResource(resource));
|
const promises = localResources.map(
|
||||||
|
function (resource) {
|
||||||
|
return fetchResource(resource);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
Promise.all(promises)
|
Promise.all(promises).then(
|
||||||
.then(results => {
|
function (results) {
|
||||||
const files = {
|
const files = {
|
||||||
'index.html': {
|
'index.html': {
|
||||||
content: html
|
content: html
|
||||||
@@ -67,7 +71,7 @@
|
|||||||
'main.js': {
|
'main.js': {
|
||||||
content: js
|
content: js
|
||||||
},
|
},
|
||||||
"package.json": {
|
'package.json': {
|
||||||
content: pkgJson
|
content: pkgJson
|
||||||
},
|
},
|
||||||
'sandbox.config.json': {
|
'sandbox.config.json': {
|
||||||
@@ -89,7 +93,8 @@
|
|||||||
|
|
||||||
form.parameters.value = compress(data);
|
form.parameters.value = compress(data);
|
||||||
form.submit();
|
form.submit();
|
||||||
});
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user