mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-29 19:41:25 -08:00
修改动态加载lib
This commit is contained in:
1 parent
4ffc46a2c4
commit
24e9c67718
5 files changed
+157
-82
No files matched your search
+15
-4
@@ -21,7 +21,7 @@ html, body {
|
|||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
}
|
}
|
||||||
|
|
||||||
.part-left .controls {
|
.part-left .header {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
height: 40px;
|
height: 40px;
|
||||||
@@ -31,17 +31,28 @@ html, body {
|
|||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
}
|
}
|
||||||
|
|
||||||
.part-left .controls .left {
|
.part-left .header .left {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
flex: 1;
|
flex: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
.part-left .controls .left .logo {
|
.part-left .header .left .logo {
|
||||||
padding-right: 30px;
|
padding-right: 30px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.part-left .controls .run {
|
.part-left .lib {
|
||||||
|
height: 50px;
|
||||||
|
padding: 5px;
|
||||||
|
outline: none;
|
||||||
|
resize: none;
|
||||||
|
border: none;
|
||||||
|
font-size: 13px;
|
||||||
|
color: #666;
|
||||||
|
border-bottom: 1px solid #ddd;
|
||||||
|
}
|
||||||
|
|
||||||
|
.part-left .header .run {
|
||||||
height: 30px;
|
height: 30px;
|
||||||
width: 100px;
|
width: 100px;
|
||||||
}
|
}
|
||||||
|
|||||||
Whitespace-only changes.
+3
-7
@@ -19,15 +19,15 @@
|
|||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="part-left">
|
<div class="part-left">
|
||||||
<div class="controls">
|
<div class="header">
|
||||||
<div class="left">
|
<div class="left">
|
||||||
<a href="./" class="logo">ArtPlayer.js Lab</a>
|
<a href="./" class="logo">ArtPlayer.js Lab</a>
|
||||||
|
<a href="../docs" class="logo">Github</a>
|
||||||
<a href="../docs" class="logo">Document</a>
|
<a href="../docs" class="logo">Document</a>
|
||||||
<iframe src="https://ghbtns.com/github-btn.html?user=zhw2590582&repo=ArtPlayer&type=star&count=true" frameborder="0" scrolling="0" width="100px" height="20px"></iframe>
|
|
||||||
<iframe src="https://ghbtns.com/github-btn.html?user=zhw2590582&repo=ArtPlayer&type=fork&count=true" frameborder="0" scrolling="0" width="100px" height="20px"></iframe>
|
|
||||||
</div>
|
</div>
|
||||||
<input class="run" type="button" value="Run Code">
|
<input class="run" type="button" value="Run Code">
|
||||||
</div>
|
</div>
|
||||||
|
<textarea class="lib" cols="30" rows="10" placeholder="Other dependencies, multiple line breaks" spellcheck="false"></textarea>
|
||||||
<div class="code"></div>
|
<div class="code"></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="part-right">
|
<div class="part-right">
|
||||||
@@ -41,7 +41,6 @@
|
|||||||
<script src="./plugins/codemirror.js"></script>
|
<script src="./plugins/codemirror.js"></script>
|
||||||
<script src="./plugins/javascript.js"></script>
|
<script src="./plugins/javascript.js"></script>
|
||||||
<script src="./js/artplayer.js"></script>
|
<script src="./js/artplayer.js"></script>
|
||||||
<script src="./js/artplayer-plugin-subtitle.js"></script>
|
|
||||||
<script src="./js/common.js"></script>
|
<script src="./js/common.js"></script>
|
||||||
<script>
|
<script>
|
||||||
var _hmt = _hmt || [];
|
var _hmt = _hmt || [];
|
||||||
@@ -52,9 +51,6 @@
|
|||||||
s.parentNode.insertBefore(hm, s);
|
s.parentNode.insertBefore(hm, s);
|
||||||
})();
|
})();
|
||||||
</script>
|
</script>
|
||||||
<script>
|
|
||||||
loadCode('./js/index.js');
|
|
||||||
</script>
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
+139
-71
@@ -1,83 +1,151 @@
|
|||||||
(function(win) {
|
(function(win) {
|
||||||
var $code = document.querySelector('.code');
|
var $code = document.querySelector('.code');
|
||||||
var $run = document.querySelector('.run');
|
var $run = document.querySelector('.run');
|
||||||
var fileCode = '';
|
var $lib = document.querySelector('.lib');
|
||||||
|
var defaultLib = './js/artplayer-plugin-subtitle.js';
|
||||||
|
var loaddLib = [];
|
||||||
|
|
||||||
consola.creat({
|
consola.creat({
|
||||||
target: '.console',
|
target: '.console',
|
||||||
size: '100%',
|
size: '100%',
|
||||||
zIndex: 99
|
zIndex: 99,
|
||||||
});
|
|
||||||
|
|
||||||
var mirror = CodeMirror($code, {
|
|
||||||
lineNumbers: true,
|
|
||||||
mode: 'javascript',
|
|
||||||
matchBrackets: true,
|
|
||||||
value: ''
|
|
||||||
});
|
|
||||||
|
|
||||||
function initApp(app) {
|
|
||||||
consola.clean();
|
|
||||||
Artplayer.config.video.events.forEach(item => {
|
|
||||||
app.on('video:' + item, event => {
|
|
||||||
console.log('video: ' + event.type);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
function getURLParameters(url) {
|
var mirror = CodeMirror($code, {
|
||||||
return (url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce(
|
lineNumbers: true,
|
||||||
(a, v) => (
|
mode: 'javascript',
|
||||||
(a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a
|
matchBrackets: true,
|
||||||
),
|
value: '',
|
||||||
{}
|
});
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadCode(url) {
|
function initArt(art) {
|
||||||
const { code } = getURLParameters(window.location.href);
|
Artplayer.config.video.events.forEach(item => {
|
||||||
if (code) {
|
art.on('video:' + item, event => {
|
||||||
mirror.setValue(decodeURIComponent(code).trim());
|
console.log('video: ' + event.type);
|
||||||
runCode();
|
});
|
||||||
} else {
|
|
||||||
fetch(url)
|
|
||||||
.then(response => {
|
|
||||||
return response.text();
|
|
||||||
})
|
|
||||||
.then(text => {
|
|
||||||
var code = text.trim();
|
|
||||||
mirror.setValue(code);
|
|
||||||
runCode();
|
|
||||||
fileCode = code;
|
|
||||||
return text;
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
console.error(err.message);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function runCode() {
|
function getURLParameters(url) {
|
||||||
Artplayer.instances.forEach(ins => {
|
return (url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce(
|
||||||
ins.destroy(true);
|
(a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a),
|
||||||
});
|
{},
|
||||||
var code = mirror.getValue();
|
);
|
||||||
eval(code);
|
|
||||||
initApp(Artplayer.instances[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
$run.addEventListener('click', function(e) {
|
|
||||||
runCode();
|
|
||||||
if (fileCode !== mirror.getValue()) {
|
|
||||||
const code = encodeURIComponent(mirror.getValue());
|
|
||||||
const url = window.location.origin + window.location.pathname + '?code=' + code;
|
|
||||||
history.pushState(null, null, url);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
win.addEventListener('error', err => {
|
function getExt(url) {
|
||||||
console.error(err.message);
|
if (url.includes('?')) {
|
||||||
});
|
return getExt(url.split('?')[0]);
|
||||||
|
}
|
||||||
|
|
||||||
win.loadCode = loadCode;
|
if (url.includes('#')) {
|
||||||
|
return getExt(url.split('#')[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return url
|
||||||
|
.trim()
|
||||||
|
.toLowerCase()
|
||||||
|
.split('.')
|
||||||
|
.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadScript(url) {
|
||||||
|
return new Promise(function(resolve, reject) {
|
||||||
|
var script = document.createElement('script');
|
||||||
|
script.type = 'text/javascript';
|
||||||
|
script.src = url;
|
||||||
|
script.onload = function() {
|
||||||
|
resolve(url);
|
||||||
|
};
|
||||||
|
document.querySelector('head').appendChild(script);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadStyle(url) {
|
||||||
|
return new Promise(function(resolve) {
|
||||||
|
var style = document.createElement('style');
|
||||||
|
style.type = 'text/css';
|
||||||
|
style.src = url;
|
||||||
|
document.querySelector('head').appendChild(style);
|
||||||
|
resolve(url);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadLib(libs = defaultLib) {
|
||||||
|
var libPromise = [];
|
||||||
|
libs.split(',')
|
||||||
|
.filter(function(url) {
|
||||||
|
return !loaddLib.includes(url);
|
||||||
|
})
|
||||||
|
.forEach(function(url) {
|
||||||
|
var ext = getExt(url);
|
||||||
|
if (ext === 'js') {
|
||||||
|
libPromise.push(loadScript(url));
|
||||||
|
} else if (ext === 'css') {
|
||||||
|
libPromise.push(loadStyle(url));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
$lib.value = decodeURIComponent(libs);
|
||||||
|
return Promise.all(libPromise);
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadCode(code) {
|
||||||
|
return new Promise(function(resolve, reject) {
|
||||||
|
if (code) {
|
||||||
|
mirror.setValue(decodeURIComponent(code).trim());
|
||||||
|
runCode();
|
||||||
|
resolve(code);
|
||||||
|
} else {
|
||||||
|
fetch('./js/example.js')
|
||||||
|
.then(response => {
|
||||||
|
return response.text();
|
||||||
|
})
|
||||||
|
.then(text => {
|
||||||
|
mirror.setValue(text);
|
||||||
|
runCode();
|
||||||
|
resolve(text);
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.error(err.message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function initApp() {
|
||||||
|
const { code, libs } = getURLParameters(window.location.href);
|
||||||
|
loadLib(libs)
|
||||||
|
.then(function(result) {
|
||||||
|
loaddLib = loaddLib.concat(result);
|
||||||
|
loadCode(code).then(() => {
|
||||||
|
console.info('App initialization completed');
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.error(err.message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function runCode() {
|
||||||
|
Artplayer.instances.forEach(art => {
|
||||||
|
art.destroy(true);
|
||||||
|
});
|
||||||
|
var code = mirror.getValue();
|
||||||
|
eval(code);
|
||||||
|
initArt(Artplayer.instances[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$run.addEventListener('click', function(e) {
|
||||||
|
runCode();
|
||||||
|
const libs = encodeURIComponent($lib.value);
|
||||||
|
const code = encodeURIComponent(mirror.getValue());
|
||||||
|
const url = window.location.origin + window.location.pathname + '?libs=' + libs + '&code=' + code;
|
||||||
|
history.pushState(null, null, url);
|
||||||
|
});
|
||||||
|
|
||||||
|
win.addEventListener('error', err => {
|
||||||
|
console.error(err.message);
|
||||||
|
});
|
||||||
|
|
||||||
|
initApp();
|
||||||
})(window);
|
})(window);
|
||||||
File renamed without changes.
Reference in new issue
Block a user