update demo

This commit is contained in:
zhw2590582 committed 2021-11-30 20:27:08 +08:00
1 parent bc8f169927
commit 571eeef46a
4 files changed
+255 -41

No files matched your search

+45 -39
View File
@@ -1,4 +1,4 @@
(function() {
(function () {
var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
if (isMobile) {
window.location.href = './mobile.html';
@@ -11,11 +11,7 @@
var $popups = document.querySelector('.popups');
var loaddLib = [];
consola.creat({
target: '.console',
size: '100%',
zIndex: 99,
});
window.consoleLog(document.querySelector('.console'));
var mirror = CodeMirror($codeMirror, {
lineNumbers: true,
@@ -27,15 +23,16 @@
});
function initArt(art) {
Artplayer.config.events.forEach(function(item) {
art && art.on('video:' + item, function(event) {
console.log('video: ' + event.type);
});
Artplayer.config.events.forEach(function (item) {
art &&
art.on('video:' + item, function (event) {
console.log('video: ' + event.type);
});
});
}
function getURLParameters(url) {
return (url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce(function(a, v) {
return (url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce(function (a, v) {
return (a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a;
}, {});
}
@@ -49,22 +46,18 @@
return getExt(url.split('#')[0]);
}
return url
.trim()
.toLowerCase()
.split('.')
.pop();
return url.trim().toLowerCase().split('.').pop();
}
function loadScript(url) {
return new Promise(function(resolve, reject) {
return new Promise(function (resolve, reject) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.onload = function() {
script.onload = function () {
resolve(url);
};
script.onerror = function() {
script.onerror = function () {
reject(new Error('Loading script failed:' + url));
};
document.querySelector('head').appendChild(script);
@@ -72,14 +65,14 @@
}
function loadStyle(url) {
return new Promise(function(resolve, reject) {
return new Promise(function (resolve, reject) {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = url;
link.onload = function() {
link.onload = function () {
resolve(url);
};
link.onerror = function() {
link.onerror = function () {
reject(new Error('Loading style failed:' + url));
};
document.querySelector('head').appendChild(link);
@@ -91,10 +84,10 @@
var libsDecode = decodeURIComponent(libs || '');
libsDecode
.split(/\r?\n/)
.filter(function(url) {
.filter(function (url) {
return !loaddLib.includes(url);
})
.forEach(function(url) {
.forEach(function (url) {
var ext = getExt(url);
if (ext === 'js') {
libPromise.push(loadScript(url));
@@ -108,15 +101,15 @@
function runExample(name) {
fetch(`./assets/example/${name}.js`)
.then(function(response) {
.then(function (response) {
return response.text();
})
.then(function(text) {
.then(function (text) {
mirror.setValue(text);
runCode();
})
.catch(function(err) {
console.error(err.message);
.catch(function (err) {
console.error(err);
});
}
@@ -131,14 +124,27 @@
}
}
function formatDate(date) {
var date = new Date(Number(date));
var YY = date.getFullYear() + '-';
var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
var DD = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
var ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
return YY + MM + DD + ' ' + hh + mm + ss;
}
function runCode() {
Artplayer.instances.forEach(function(art) {
Artplayer.instances.forEach(function (art) {
art.destroy(true);
});
var code = mirror.getValue();
eval(code);
initArt(Artplayer.instances[0]);
console.info('Player initialization completed');
console.debug(`Artplayer@${Artplayer.version}`);
console.debug(`Env@${Artplayer.env}`);
console.debug(`Build@${formatDate(Artplayer.build)}`);
}
function initApp() {
@@ -148,24 +154,24 @@
example = _getURLParameters.example;
loadLib(libs)
.then(function(result) {
.then(function (result) {
loaddLib = loaddLib.concat(result);
loadCode(code, example);
})
.catch(function(err) {
console.error(err.message);
.catch(function (err) {
console.error(err);
});
}
window.addEventListener('error', function(err) {
console.error(err.message);
window.addEventListener('error', function (err) {
console.error(err);
});
window.addEventListener('unhandledrejection', function(err) {
console.error(err.message);
window.addEventListener('unhandledrejection', function (err) {
console.error(err);
});
$run.addEventListener('click', function(e) {
$run.addEventListener('click', function (e) {
var libs = encodeURIComponent($lib.value);
var code = encodeURIComponent(mirror.getValue());
var url = window.location.origin + window.location.pathname + '?libs=' + libs + '&code=' + code;
@@ -178,6 +184,6 @@
this.style.display = 'none';
}
});
initApp();
})();