Add method Browser.waitUntil

This commit is contained in:
Namhyeon Go 2024-03-20 11:13:58 +09:00
parent 05df3518ac
commit 533521239f
3 changed files with 108 additions and 72 deletions

View File

@ -1,6 +1,7 @@
// browser.js
// Namhyeon Go <abuse@catswords.net>
// https://github.com/gnh1201/welsonjs
////////////////////////////////////////////////////////////////////////
// only less than IE 9
////////////////////////////////////////////////////////////////////////
@ -110,9 +111,9 @@ if (!window.addEventListener) {
}
////////////////////////////////////////////////////////////////////////
// exports.getIEVersion()
// getIEVersion()
////////////////////////////////////////////////////////////////////////
exports.getIEVersion = function() {
function getIEVersion() {
var undef,
v = 3,
div = document.createElement('div'),
@ -127,50 +128,64 @@ exports.getIEVersion = function() {
};
////////////////////////////////////////////////////////////////////////
// exports.addScript()
// exports.waitUntil()
////////////////////////////////////////////////////////////////////////
exports.addScript = function(url, callback, test, ttl) {
var _callback = function(el, ttl) {
function waitUntil(f, test, ttl) {
if (typeof f !== "function") return;
if (typeof test !== "function") return;
var ttl = (typeof ttl === "number" ? ttl : 30000); // TTL(Time-To-Live)
var started_time = Date.now();
var _test = function(el, callback, ttl) {
var result = test(el);
if (typeof(result) !== "undefined") {
if (typeof result !== "undefined" && !!result) {
callback(el);
} else {
setTimeout(function() {
if (ttl > 0) {
_callback(el, ttl - 1);
var current_time = Date.now();
if (current_time - started_time < ttl) {
_test(el, callback, ttl);
} else {
console.log("failed load " + url);
console.warn("Failed to load:", url);
}
}, 1);
}
};
f(_test, ttl);
}
////////////////////////////////////////////////////////////////////////
// addScript()
////////////////////////////////////////////////////////////////////////
function addScript(url, callback, test, ttl) {
if (typeof test !== "function") {
test = function(el, callback, ttl) {};
}
var ttl = (typeof ttl === "number" ? ttl : 30000); // TTL(Time-To-Live)
var el = document.createElement("script");
el.src = url;
el.type = "text/javascript";
el.charset = "utf-8";
document.head.appendChild(el);
if (typeof(test) === "function" && typeof(callback) === "function") {
// Time-To-Live: default value is 30 seconds
ttl = (typeof(ttl) == "number" ? ttl : 30000);
el.onload = _callback(el, ttl);
} else if (typeof(callback) === "function") {
el.onload = callback(el);
if (typeof test === "function") {
test(el, callback, ttl);
}
return el;
};
////////////////////////////////////////////////////////////////////////
// exports.addResources()
// addResources()
////////////////////////////////////////////////////////////////////////
exports.addResources = function(resources, callback) {
function addResources(resources, callback) {
resources.forEach(function(resource) {
if (["javascript", "text/javascript"].indexOf(resource.type) > -1) {
exports.addScript(resource.url, callback);
addScript(resource.url, callback);
} else if (["stylesheet", "text/css"].indexOf(resource.type) > -1) {
exports.addStylesheet(resource.url, callback);
addStylesheet(resource.url, callback);
} else {
console.warn("Not supported resource type");
}
@ -178,9 +193,9 @@ exports.addResources = function(resources, callback) {
};
////////////////////////////////////////////////////////////////////////
// exports.addStylesheet()
// addStylesheet()
////////////////////////////////////////////////////////////////////////
exports.addStylesheet = function(url, callback) {
function addStylesheet(url, callback) {
var el = document.createElement("link");
el.href = url;
el.rel = "stylesheet";
@ -194,64 +209,79 @@ exports.addStylesheet = function(url, callback) {
};
////////////////////////////////////////////////////////////////////////
// exports.setContent()
// setContent()
////////////////////////////////////////////////////////////////////////
exports.setContent = function(content) {
function setContent(content) {
document.getElementById("app").innerHTML = content;
};
////////////////////////////////////////////////////////////////////////
// exports.start()
// start()
////////////////////////////////////////////////////////////////////////
exports.start = function(callback) {
var IEVersion = exports.getIEVersion();
function start(callback) {
var msie = getIEVersion();
// load jQuery and cross browsing libraries
if (IEVersion == 8) {
exports.addScript("app/assets/css/jquery/ie8-0.8.1.min.js");
if (msie == 8) {
addScript("app/assets/css/jquery/ie8-0.8.1.min.js");
}
exports.addScript("app/assets/js/html5shiv-printshiv-3.7.3.min.js");
if (IEVersion < 9) {
exports.addScript("app/assets/js/respond-1.4.2.edited.js");
exports.addScript("app/assets/js/selectivizr-1.0.2.edited.js");
exports.addScript("app/assets/js/excanvas.arv-565afad.js");
exports.addScript("app/assets/js/jquery-1.11.3.min.js", callback, function(el) {
addScript("app/assets/js/html5shiv-printshiv-3.7.3.min.js");
if (msie < 9) {
addScript("app/assets/js/respond-1.4.2.edited.js");
addScript("app/assets/js/selectivizr-1.0.2.edited.js");
addScript("app/assets/js/excanvas.arv-565afad.js");
waitUntil(function(test, ttl) {
addScript("app/assets/js/jquery-1.11.3.min.js", callback, test, ttl);
}, function(el) {
return window.jQuery;
});
} else {
exports.addScript("app/assets/js/jquery-3.5.1.min.js", callback, function(el) {
waitUntil(function(test, ttl) {
addScript("app/assets/js/jquery-3.5.1.min.js", callback, test, ttl);
}, function(el) {
return window.jQuery;
});
}
exports.addScript("app/assets/js/jquery.html5-placeholder-shim.jcampbell1-5a87f05.js");
addScript("app/assets/js/jquery.html5-placeholder-shim.jcampbell1-5a87f05.js");
// load Modernizr (2.8.3)
exports.addScript("app/assets/js/modernizr-2.8.3.min.js");
addScript("app/assets/js/modernizr-2.8.3.min.js");
// load jQuery UI (1.12.1)
exports.addScript("app/assets/js/jquery-ui-1.21.1.min.js");
addScript("app/assets/js/jquery-ui-1.21.1.min.js");
// load jsRender (1.0.8)
exports.addScript("app/assets/js/jsrender-1.0.8.min.js");
addScript("app/assets/js/jsrender-1.0.8.min.js");
};
////////////////////////////////////////////////////////////////////////
// exports.reload()
// reload()
////////////////////////////////////////////////////////////////////////
exports.reload = function() {
function reload() {
if (typeof window !== "undefined") {
window.location.reload();
}
};
////////////////////////////////////////////////////////////////////////
// exports.close()
// close()
////////////////////////////////////////////////////////////////////////
exports.close = function() {
function close() {
exit(0);
};
exports.VERSIONINFO = "Browser Library (browser.js) version 0.1.2";
exports.getIEVersion = getIEVersion;
exports.waitUntil = waitUntil;
exports.addScript = addScript;
exports.addResources = addResources;
exports.addStylesheet = addStylesheet;
exports.setContent = setContent;
exports.start = start;
exports.reload = reload;
exports.close = close;
exports.VERSIONINFO = "Browser Library (browser.js) version 0.1.3";
exports.AUTHOR = "abuse@catswords.net";
exports.global = global;
exports.require = global.require;

View File

@ -1,10 +1,6 @@
//////////////////////////////////////////////////////////////////////////////////
//
// file.js
//
// Common routines. Defines LIB object which contains the API, as well as
// a global console.log function.
// with the PIPE based IPC (lib/pipe-ipc.js)
//
/////////////////////////////////////////////////////////////////////////////////
@ -12,10 +8,6 @@
var LIB = require("lib/std");
var PipeIPC = require("lib/pipe-ipc");
/////////////////////////////////////////////////////////////////////////////////
// Private APIs / Utility functions
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// fileExists
/////////////////////////////////////////////////////////////////////////////////
@ -154,8 +146,16 @@ function deleteFile(FN) {
/////////////////////////////////////////////////////////////////////////////////
function includeFile(FN) {
try {
eval(readFile(FN));
} catch (e) {
console.error(e.message, "in", FN);
}
}
/////////////////////////////////////////////////////////////////////////////////
// appendFile
/////////////////////////////////////////////////////////////////////////////////
function appendFile(FN, content, charset) {
var result = false;
@ -168,6 +168,10 @@ function appendFile(FN, content, charset) {
return result;
}
/////////////////////////////////////////////////////////////////////////////////
// rotateFile
/////////////////////////////////////////////////////////////////////////////////
function rotateFile(FN, content, numOfLines, charset) {
var result = false;
var pipe = PipeIPC.connect(PipeIPC.CRC32(FN));
@ -196,6 +200,7 @@ exports.rotateFile = rotateFile;
exports.CdoCharset = PipeIPC.CdoCharset;
exports.VERSIONINFO = "File Library (file.js) version 0.2.10";
exports.VERSIONINFO = "File IO Library (file.js) version 0.2.11";
exports.AUTHOR = "abuse@catswords.net";
exports.global = global;
exports.require = global.require;

View File

@ -1,7 +1,6 @@
////////////////////////////////////////////////////////////////////////
// Webloader
////////////////////////////////////////////////////////////////////////
var CONFIG = require("lib/config");
var FILE = require("lib/file");
var Browser = require("lib/browser");
@ -39,7 +38,7 @@ global.console._echo = function(args, type) {
}
try {
if (typeof(window.jQuery.toast) !== "undefined") {
if (typeof window.jQuery.toast !== "undefined") {
window.jQuery.toast({
heading: heading,
text: message,
@ -127,6 +126,7 @@ exports.main = function(args) {
Browser.start(function(el) {
jQuery.support.cors = true;
Browser.waitUntil(function(test, ttl) {
Browser.addScript("app/assets/js/jquery.toast-1.3.2.min.js", function(el) {
var messages = global.console._messages;
if (messages.length > 0) {
@ -135,16 +135,17 @@ exports.main = function(args) {
console.log(messages[i]);
}
// start this app
// start the app
Browser.addScript("app/assets/js/jquery.form-4.3.0.min.js");
Browser.addScript("app/index.js");
// hide loading image
document.getElementById("loading").style.display = "none";
}
}, test, ttl);
}, function(el) {
return window.jQuery.toast;
});
}, 30000);
});
// hook drag event