Skip to content

Commit fe718b0

Browse files
committed
Refactored base URL finding and used in mini IDE
1 parent 4b2edd1 commit fe718b0

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

web/mini_ide/webperl_mini_ide.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@
7070

7171
// This is a workaround for Emscripten only being able to call main() once per page load.
7272
// I wouldn't recommend this for "production" use.
73-
var baseurl = new URL(window.location);
74-
baseurl = baseurl.origin + baseurl.pathname.substring(0,baseurl.pathname.lastIndexOf('/'));
73+
var baseurl = Perl.Util.baseurl(window.location);
7574
function run_perl_iframe (argv, state_callback, done_callback) {
7675
var html = '<html><head><base href="'+baseurl+'"><script src="webperl.js"></scr'+'ipt></head><body></body></html>';
7776
var blob = new Blob([html], {type: "text/html;charset=utf-8"});

web/webperl.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var Module;
2727
var Perl = {
2828
trace: false, // user may enable this
2929
endAfterMain: false, // user may enable this (before Perl.init)
30+
Util: {},
3031
// internal variables:
3132
initStepsLeft: 2, // Must match number of Perl.initStepFinished() calls!
3233
state: "Uninitialized",
@@ -179,18 +180,21 @@ var getScriptURL = (function() { // with thanks to https://stackoverflow.com/a/2
179180
return function() { return myScript.src; };
180181
})();
181182

183+
Perl.Util.baseurl = function (urlstr) {
184+
var url = new URL(urlstr);
185+
if (url.protocol=='file:')
186+
return url.href.substring(0, url.href.lastIndexOf('/'));
187+
else
188+
return url.origin + url.pathname.substring(0, url.pathname.lastIndexOf('/'));
189+
};
190+
182191
Perl.init = function (readyCallback) {
183192
if (Perl.state != "Uninitialized")
184193
throw "Perl: can't call init in state "+Perl.state;
185194
Perl.changeState("Initializing");
186-
var baseurl = new URL(getScriptURL());
187-
if (baseurl.protocol=='file:')
188-
// Note that a lot of things still won't work for file:// URLs
189-
// because of the Same-Origin Policy.
190-
// see e.g. https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSRequestNotHttp
191-
baseurl = baseurl.href.substring(0,baseurl.href.lastIndexOf('/'));
192-
else
193-
baseurl = baseurl.origin + baseurl.pathname.substring(0,baseurl.pathname.lastIndexOf('/'));
195+
// Note that a lot of things still won't work for file:// URLs because of the Same-Origin Policy.
196+
// see e.g. https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSRequestNotHttp
197+
var baseurl = Perl.Util.baseurl(getScriptURL());
194198
Perl.readyCallback = readyCallback;
195199
Module = {
196200
noInitialRun: true,

0 commit comments

Comments
 (0)