Skip to content
This repository was archived by the owner on Nov 22, 2021. It is now read-only.

Commit 71a2e1d

Browse files
committed
fix jsbin resources bug
1 parent 48d87d7 commit 71a2e1d

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules/
1+
node_modules/
2+
.DS_Store

launcher.html

100644100755
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
if (config.page !== 'index') {
2929
$('head').append('<script src="msg/' + config.page + '/' + config.lang + '.js' + '"><\/script>');
3030
}
31+
3132
data.body = launcher.translate(data.body, window.MSG);
33+
data.body = launcher.injectMedia(data.body);
34+
data.head = launcher.injectDependencies(data.head);
3235

3336
$.extend(config.data, data);
3437
launcher.jsbin(config);

launcher.js

100644100755
Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
'use strict';
1616

17+
var parser = new window.DOMParser();
18+
1719
function loadTemplate(url, callback) {
1820
var link = document.createElement('link'),
1921
tag = document.getElementsByTagName('script')[0];
@@ -38,10 +40,7 @@
3840

3941
function assembleHtml(head, body, css, js) {
4042
var html = '<!doctype html>\n<html>\n\n';
41-
if(css==='jsbin'){
42-
head = head.replace(/\/lib\//g,'https://blockly.webduino.io/lib/');
43-
body = body.replace(/\/media\//g,'https://blockly.webduino.io/media/');
44-
}
43+
4544
html += ('<head>' +
4645
(head ? '\n ' + head + '\n' : '') +
4746
(css ? '\n <style>' + css + '</style>\n' : '') +
@@ -87,6 +86,39 @@
8786
return $wrap.wrap('<div/>').parent().html();
8887
}
8988

89+
function injectDependencies(headStr) {
90+
var head = parser.parseFromString(headStr, 'text/html').head;
91+
92+
head.querySelectorAll('script').forEach(function (sc) {
93+
sc.src = appendBaseURL(sc.getAttribute('src'));
94+
});
95+
head.querySelectorAll('link').forEach(function (link) {
96+
link.href = appendBaseURL(link.getAttribute('href'));
97+
});
98+
99+
return head.innerHTML;
100+
}
101+
102+
function injectMedia(bodyStr) {
103+
var body = parser.parseFromString(bodyStr, 'text/html').body;
104+
105+
body.querySelectorAll('img').forEach(function (img) {
106+
img.src = appendBaseURL(img.getAttribute('src'));
107+
});
108+
109+
return body.innerHTML;
110+
}
111+
112+
function appendBaseURL(url) {
113+
var urlc = url.toLowerCase();
114+
if (urlc.indexOf('//') !== 0 &&
115+
urlc.indexOf('http://') !== 0 &&
116+
urlc.indexOf('https://') !== 0) {
117+
url = location.origin + url;
118+
}
119+
return url;
120+
}
121+
90122
var launchers = {
91123
jsfiddle: function (config) {
92124
var data = config.data;
@@ -121,11 +153,12 @@
121153

122154
jsbin: function (config) {
123155
var data = config.data;
156+
124157
if (config.jsPreprocessor) {
125158
data[config.jsPreprocessor] = data.js;
126159
delete data.js;
127160
}
128-
data.html = assembleHtml(data.head,data.body,'jsbin');
161+
data.html = assembleHtml(data.head, data.body);
129162
config.modes = config.modes || 'html,css,js,output';
130163

131164
post('//bin.webduino.io?' + config.modes, data);
@@ -158,6 +191,8 @@
158191

159192
window.launcher = {
160193
loadTemplate: loadTemplate,
194+
injectDependencies: injectDependencies,
195+
injectMedia: injectMedia,
161196
translate: translate,
162197
jsfiddle: launchers.jsfiddle,
163198
codepen: launchers.codepen,

0 commit comments

Comments
 (0)