Compare commits

...

4 Commits

Author SHA1 Message Date
3979d358d0 ignore npm tempfiles 2024-11-11 02:37:07 +01:00
422446c914 add minify configfile 2024-11-11 02:36:39 +01:00
bbb3d86e16 use minify 2024-11-11 02:36:26 +01:00
996dfb4029 various linter-related changes to formatting 2024-11-11 02:36:06 +01:00
5 changed files with 424 additions and 370 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
dist/js/*.js dist/js/*.js
dist/css/*.css dist/css/*.css
dist/css/*.svg dist/css/*.svg
package-lock.json
node_modules

8
.minify.json Normal file
View File

@ -0,0 +1,8 @@
{
"js": {
"type": "terser",
"terser": {
"mangle": false
}
}
}

18
package.json Normal file
View File

@ -0,0 +1,18 @@
{
"name": "myplayer",
"version": "1.0.0",
"description": "A Web-MP3-Player with Playlist Support",
"main": "src/player.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://tk-sls.de/git/tk-sls.de/myplayer.git"
},
"author": "tilt <tilt@linuxfoo.de>",
"license": "MIT",
"dependencies": {
"minify": "^11.4.1"
}
}

View File

@ -12,8 +12,8 @@ if ! cd "$dir" ; then
elif ! mkdir -p "$jsdir" ; then elif ! mkdir -p "$jsdir" ; then
echo "ERROR: Could not create directory $jsdir; aborted." >&2 ; echo "ERROR: Could not create directory $jsdir; aborted." >&2 ;
exit 1 ; exit 1 ;
elif ! type yui-compressor > /dev/null ; then elif ! type minify > /dev/null ; then
echo "ERROR: Executable \"yui-compressor\" not found in PATH; aborted." >&2 ; echo "ERROR: Executable \"minify\" not found in PATH; aborted." >&2 ;
exit 1 ; exit 1 ;
fi fi
@ -47,7 +47,7 @@ do
done done
echo "INFO: Compressing player.js ..." >&2 echo "INFO: Compressing player.js ..." >&2
yui-compressor player.js >> "$jsdir"/myplayer.min.js minify player.js >> "$jsdir"/myplayer.min.js
chmod 644 "$jsdir"/myplayer.min.js chmod 644 "$jsdir"/myplayer.min.js

View File

@ -6,7 +6,7 @@
* restriction, including without limitation the rights to use, copy, * restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies * modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is * of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be * The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software. * included in all copies or substantial portions of the Software.
@ -21,401 +21,427 @@
* THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE */ * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE */
var MyPlayer = function(_args) { var MyPlayer = function(_args) {
var failed = false; var failed = false,
var detached = false; detached = false,
var player = null; player = null,
args_default = {
autoplay: false,
css: { "height": "500px" },
detachable: true,
element: "body",
id3: false,
list: [],
loop: true,
mode: "mp3url",
shuffle: false,
waveform: false
},
args = (_args===undefined) ? args_default : $.extend({}, args_default, _args),
mimeTypeFromSrc = function(src) {
var extension = src.split(".").pop().toLowerCase();
var defaultArgs = { var types = {
detachable: true, "3g2": "video/3gpp2",
element: 'body', "3gp": "video/3gpp",
id3: false, "7z": "application/x-7z-compressed",
mode: 'mp3url', "aac": "audio/aac",
waveform: false, "abw": "application/x-abiword",
autoplay: false, "arc": "application/x-freearc",
loop: true, "avi": "video/x-msvideo",
shuffle: false, "azw": "application/vnd.amazon.ebook",
list: [] "bin": "application/octet-stream",
}; "bmp": "image/bmp",
"bz": "application/x-bzip",
"bz2": "application/x-bzip2",
"cda": "application/x-cdf",
"csh": "application/x-csh",
"css": "text/css",
"csv": "text/csv",
"doc": "application/msword",
"eot": "application/vnd.ms-fontobject",
"epub": "application/epub+zip",
"gif": "image/gif",
"gz": "application/gzip",
"htm": "text/html",
"html": "text/html",
"ico": "image/vnd.microsoft.icon",
"ics": "text/calendar",
"jar": "application/java-archive",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"js": "text/javascript",
"json": "application/json",
"jsonld": "application/ld+json",
"mid": "audio/midi audio/x-midi",
"midi": "audio/midi audio/x-midi",
"mjs": "text/javascript",
"mp3": "audio/mpeg",
"mp4": "video/mp4",
"mpeg": "video/mpeg",
"mpkg": "application/vnd.apple.installer+xml",
"odp": "application/vnd.oasis.opendocument.presentation",
"ods": "application/vnd.oasis.opendocument.spreadsheet",
"odt": "application/vnd.oasis.opendocument.text",
"oga": "audio/ogg",
"ogv": "video/ogg",
"ogx": "application/ogg",
"opus": "audio/opus",
"otf": "font/otf",
"pdf": "application/pdf",
"php": "application/x-httpd-php",
"png": "image/png",
"ppt": "application/vnd.ms-powerpoint",
"rar": "application/vnd.rar",
"rtf": "application/rtf",
"sh": "application/x-sh",
"svg": "image/svg+xml",
"swf": "application/x-shockwave-flash",
"tar": "application/x-tar",
"tif": "image/tiff",
"tiff": "image/tiff",
"ts": "video/mp2t",
"ttf": "font/ttf",
"txt": "text/plain",
"vsd": "application/vnd.visio",
"wav": "audio/wav",
"weba": "audio/webm",
"webm": "video/webm",
"webp": "image/webp",
"woff": "font/woff",
"woff2": "font/woff2",
"xhtml": "application/xhtml+xml",
"xls": "application/vnd.ms-excel",
"xml": "application/xml",
"xul": "application/vnd.mozilla.xul+xml",
"zip": "application/zip"
};
var mimeTypeFromSrc = function(src) { return types[extension] || "application/octet-stream";
var extension = src.split('.').pop().toLowerCase(); },
createAudio = function() {
var autoplay = args.autoplay ? 'autoplay="true"' : '';
var styles = [];
var style;
var key;
return { for (key in args.css) {
"aac": "audio/aac", var value = args.css[key];
"abw": "application/x-abiword", styles.push(`${key}: ${value}`);
"arc": "application/x-freearc",
"avi": "video/x-msvideo",
"azw": "application/vnd.amazon.ebook",
"bin": "application/octet-stream",
"bmp": "image/bmp",
"bz": "application/x-bzip",
"bz2": "application/x-bzip2",
"cda": "application/x-cdf",
"csh": "application/x-csh",
"css": "text/css",
"csv": "text/csv",
"doc": "application/msword",
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"eot": "application/vnd.ms-fontobject",
"epub": "application/epub+zip",
"gz": "application/gzip",
"gif": "image/gif",
"htm": "text/html",
"html": "text/html",
"ico": "image/vnd.microsoft.icon",
"ics": "text/calendar",
"jar": "application/java-archive",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"js": "text/javascript",
"json": "application/json",
"jsonld": "application/ld+json",
"mid": "audio/midi audio/x-midi",
"midi": "audio/midi audio/x-midi",
"mjs": "text/javascript",
"mp3": "audio/mpeg",
"mp4": "video/mp4",
"mpeg": "video/mpeg",
"mpkg": "application/vnd.apple.installer+xml",
"odp": "application/vnd.oasis.opendocument.presentation",
"ods": "application/vnd.oasis.opendocument.spreadsheet",
"odt": "application/vnd.oasis.opendocument.text",
"oga": "audio/ogg",
"ogv": "video/ogg",
"ogx": "application/ogg",
"opus": "audio/opus",
"otf": "font/otf",
"png": "image/png",
"pdf": "application/pdf",
"php": "application/x-httpd-php",
"ppt": "application/vnd.ms-powerpoint",
"pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
"rar": "application/vnd.rar",
"rtf": "application/rtf",
"sh": "application/x-sh",
"svg": "image/svg+xml",
"swf": "application/x-shockwave-flash",
"tar": "application/x-tar",
"tif": "image/tiff",
"tiff": "image/tiff",
"ts": "video/mp2t",
"ttf": "font/ttf",
"txt": "text/plain",
"vsd": "application/vnd.visio",
"wav": "audio/wav",
"weba": "audio/webm",
"webm": "video/webm",
"webp": "image/webp",
"woff": "font/woff",
"woff2": "font/woff2",
"xhtml": "application/xhtml+xml",
"xls": "application/vnd.ms-excel",
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"xml": "application/xml",
"xul": "application/vnd.mozilla.xul+xml",
"zip": "application/zip",
"3gp": "video/3gpp",
"3g2": "video/3gpp2",
"7z": "application/x-7z-compressed"
}[extension] || "application/octet-stream";
};
var args = (typeof(_args)==='undefined')
? defaultArgs
: $.extend({}, defaultArgs, _args);
var createAudio = function() {
var autoplay = args.autoplay ? 'autoplay="true"' : '';
$(args.element).append(
'<audio id="myplayer" controls="controls" ' + autoplay + '>'+
'<p>'+
'Your browser does not understand '+
'the &lt;audio&gt; tag. '+
'</p>'+
'</audio>'
);
args.audio = args.element + ' audio';
$(args.audio).css({
'height': '500px',
});
};
var readId3 = function() {
$(args.element).find('ul.mejs li').each(function(i, track){
var url = $(track).attr('data-url');
$(track).prepend('<span class="ajaxLoader">&nbsp;</span>&nbsp;');
jsmediatags.read(url, {
onSuccess: function(tag) {
var tags = tag.tags;
var track_title;
if(typeof(tags.title)!='undefined')
track_title = tags.title;
else
track_title = url;
if(typeof(tags.artist)!='undefined')
track_title = tags.artist + ' - ' + track_title;
$(args.element).find('ul.mejs li').each(function(j, _track){
if(i==j)
$(_track).html(track_title);
});
} }
});
});
};
var playerCreated = function() { style = styles.join(" ");
restoreParams(); args.audio = $(`<audio id="myplayer" controls="controls" style="${style}" ${autoplay}></audio>`);
console.log("DEBUG: appending audio=", args.audio, " to element=", args.element, " ...");
if(args.id3) $(args.element).append(args.audio);
readId3(); },
}; readId3 = function() {
$(args.element).find('ul.mejs li').each(function(i, track){
var url = $(track).attr('data-url');
var createPlayer = function() { $(track).prepend('<span class="ajaxLoader">&nbsp;</span>&nbsp;');
player = new MediaElementPlayer(
"myplayer", {
success: playerCreated,
features: ['playpause', 'current', 'progress', 'duration', 'volume', 'playlist', 'prevtrack', 'nexttrack', 'shuffle', 'loop']
});
$(args.element).append( jsmediatags.read(url, {
'<p class="playerDetached" style="display: none;">'+ onSuccess: function(tag) {
'The player is currently detached.'+ var tags = tag.tags;
'</p>' var track_title;
);
};
var param = function(name) { if(typeof(tags.title)!='undefined') {
var query = window.location.search.substring(1); track_title = tags.title;
var vars = query.split("&"); }
else {
track_title = url;
}
for(var i=0; i<vars.length; i++) { if(typeof(tags.artist)!='undefined') {
var pair = vars[i].split("="); track_title = tags.artist + ' - ' + track_title;
}
if(pair[0] == name) $(args.element).find('ul.mejs li').each(function(j, _track){
return pair[1]; if(i==j) {
} $(_track).html(track_title);
}
});
}
});
});
},
playerCreated = function(mediaElement, originalNode, instance) {
restoreParams();
return false; if(args.shuffle) {
}; instance.shuffleCallback();
}
var setupDetach = function() { if(args.loop) {
if(!args.detachable) instance.loopCallback();
return; }
var popup = function(url) { if(args.id3) {
player.pause(); readId3();
$('.mejs-audio').hide(); }
$('.playerDetached').show(); },
createPlayer = function() {
var playerWindow = window.open( player = new MediaElementPlayer(
url, 'myplayer', "myplayer", {
'height='+ Math.max($(args.element).height(), 298)+','+ success: playerCreated,
'width='+408+','+ features: [
'resizable=false' 'playpause',
); 'current',
'progress',
if(window.focus) 'duration',
playerWindow.focus(); 'volume',
'playlist',
$("iframe",top.document).height(20); 'prevtrack',
'nexttrack',
return false; 'shuffle',
}; 'loop'
]
if(param('popup')==false) {
$('.mejs-controls').append(
'<div id="popupButton" '+
'class="popupButton">'+
'<button '+
'title="Open in separate window" '+
'type="button">'+
'</button>'+
'</div>'
);
$('.popupButton button').click(function(){
var currentTime = player.getCurrentTime();
var volume = player.getVolume();
var paused = player.media.paused;
var trackNo = 0;
$(args.element).find('ul.mejs li').each(function(i){
if($(this).hasClass('current'))
trackNo = i;
}); });
popup( $(args.element).append(
$(location).attr('href')+'?'+ '<p class="playerDetached" style="display: none;">'+
'popup&'+ 'The player is currently detached.'+
'paused='+paused+'&'+ '</p>'
'currentTime='+currentTime+'&'+
'volume='+volume+'&'+
'trackNo='+trackNo
); );
}); },
} param = function(name) {
else { var query = window.location.search.substring(1);
var windowHeight = $(window).height(); var vars = query.split("&");
window.onbeforeunload = function(){ for(i=0; i<vars.length; i++) {
$("iframe", opener.top.document).height(windowHeight); var pair = vars[i].split("=");
$('.mejs-audio', opener.document).show();
$('.playerDetached', opener.document).hide();
};
}
};
var restoreParams = function() { if(pair[0] == name) {
if(!detached) return pair[1];
return;
var paused = (param('paused') && param('paused')=='true');
var currentTime = param('currentTime') ? parseFloat(param('currentTime')) : 0.0;
var volume = param('volume') ? parseFloat(param('volume')) : 1.0;
var trackNo = param('trackNo') ? parseInt(param('trackNo')) : 0;
player.setVolume(volume);
$(args.element).find('ul.mejs li').each(function(i){
if(i==trackNo) {
player.playTrack($(this));
player.setCurrentTime(currentTime);
}
});
if(paused)
player.pause();
};
if(param('popup')!==false) {
detached = true;
$('body').html(
'<div '+
'id="myplayer" '+
'style="'+
'width: '+$(args.element).width()+'px; '+
'height: '+$(args.element).height()+' px;'+
'"></div>'
);
args.element = '#myplayer';
}
if(args.mode=='mp3url') {
var mp3url = (typeof args.mp3url === "undefined")
? $(location).attr('href').replace(/\?.*/, '').replace(/\/[^\/]+\/?$/, '')
: args.mp3url;
$.ajax({
url: mp3url,
type: 'GET',
success: function(res) {
createAudio();
var doc = document.createElement('html');
doc.innerHTML = res;
var head = $(doc).find('a').each(function(idx,item) {
var src = $(item).attr('href');
if(src.match(/\.mp3$/)) {
var url = mp3url+'/'+src;
var title = item.innerHTML;
var type = "audio/mpeg";
$(args.audio).append('<source src="'+url+'" type="' + type + '" title="'+title+'"/>');
} }
});
createPlayer();
setupDetach();
}
});
}
else if(args.mode=='m3u') {
var m3u = (typeof args.m3u === "undefined")
? $(location).attr('href').replace(/\?.*/, '').replace(/\/[^\/]+\/?$/, '/play.m3u')
: args.m3u;
createAudio();
$.get(m3u, function(txt){
var lines = txt.split("\n");
for(var i = 0, len = lines.length; i < len; i++)
if(!lines[i].match(/^[ \t]*#/) && lines[i].match(/\.mp3$/)) {
var mp3 = lines[i];
var title = unescape(mp3.split('/').pop());
var type = "audio/mpeg";
$(args.audio).append('<source src="'+mp3+'" type="' + type + '" title="'+title+'"/>');
} }
console.log("DEBUG: mode==m3u audio=",$(args.audio)); return false;
},
setupDetach = function() {
if(!args.detachable) {
return;
}
createPlayer(); var popup = function(url) {
setupDetach(); player.pause();
}); $('.mejs-audio').hide();
} $('.playerDetached').show();
else if(args.mode=='list') {
createAudio();
for(var i = 0, len = args.list.length; i < len; i++) { var playerWindow = window.open(
var item = args.list[i]; url, 'myplayer',
var type = typeof item; 'height='+ Math.max($(args.element).height(), 298)+','+
var src = ''; 'width='+408+','+
var title = ''; 'resizable=false'
if(item !== null && type === 'object') {
if(item.url)
src = item.url;
else {
alert(
'MyPlayer: item[' + i + ']: '+
'property "url" is missing.'
); );
continue; if(window.focus) {
playerWindow.focus();
}
$("iframe",top.document).height(20);
return false;
};
if(param('popup')==false) {
$('.mejs-controls').append(
'<div id="popupButton" '+
'class="popupButton">'+
'<button '+
'title="Open in separate window" '+
'type="button">'+
'</button>'+
'</div>'
);
$('.popupButton button').click(function(){
var currentTime = player.getCurrentTime();
var volume = player.getVolume();
var paused = player.media.paused;
var trackNo = 0;
$(args.element).find('ul.mejs li').each(function(i){
if($(this).hasClass('current')) {
trackNo = i;
}
});
popup(
$(location).attr('href')+'?'+
'popup&'+
'paused='+paused+'&'+
'currentTime='+currentTime+'&'+
'volume='+volume+'&'+
'trackNo='+trackNo
);
});
}
else {
var windowHeight = $(window).height();
window.onbeforeunload = function(){
$("iframe", opener.top.document).height(windowHeight);
$('.mejs-audio', opener.document).show();
$('.playerDetached', opener.document).hide();
};
}
},
restoreParams = function() {
if(!detached) {
return;
}
var paused = (param('paused') && param('paused')=='true');
var currentTime = param('currentTime') ? parseFloat(param('currentTime')) : 0.0;
var volume = param('volume') ? parseFloat(param('volume')) : 1.0;
var trackNo = param('trackNo') ? parseInt(param('trackNo')) : 0;
player.setVolume(volume);
$(args.element).find('ul.mejs li').each(function(i){
if(i==trackNo) {
player.playTrack($(this));
player.setCurrentTime(currentTime);
}
});
if(paused) {
player.pause();
}
};
if(param('popup')!==false) {
detached = true;
$('body').html(
'<div ' +
'id="myplayer" ' +
'style="' +
'width: ' + $(args.element).width() + 'px; '+
'height: ' + $(args.element).height() + ' px;'+
'"></div>'
);
args.element = '#myplayer';
}
if(args.mode=='mp3url') {
console.log("args.mode=", args.mode);
var mp3url = (typeof args.mp3url === "undefined")
? $(location).attr('href').replace(/\?.*/, '').replace(/\/[^\/]+\/?$/, '')
: args.mp3url;
$.ajax({
url: mp3url,
type: 'GET',
success: function(res) {
var doc;
var head;
doc = document.createElement('html');
createAudio();
doc.innerHTML = res;
head = $(doc).find('a').each(function(idx,item) {
var src = $(item).attr('href');
if(src.match(/\.mp3$/)) {
var url = mp3url+'/'+src;
var title = item.innerHTML;
var type = "audio/mpeg";
args.audio.append(`<source src="${url}" type="${type}" title="${title}"/>`);
}
});
createPlayer();
setupDetach();
}
});
}
else if(args.mode=='m3u') {
var m3u = (typeof args.m3u === "undefined")
? $(location).attr('href').replace(/\?.*/, '').replace(/\/[^\/]+\/?$/, '/play.m3u')
: args.m3u;
createAudio();
$.get(m3u, function(txt){
var lines = txt.split("\n");
var len = lines.length;
var i = 0;
while(i < len) {
if(!lines[i].match(/^[ \t]*#/) && lines[i].match(/\.mp3$/)) {
var mp3 = lines[i];
var title = unescape(mp3.split('/').pop());
var type = "audio/mpeg";
args.audio.append(`<source src="${mp3}" type="${type}" title="${title}"/>`);
}
i++;
}
createPlayer();
setupDetach();
});
}
else if(args.mode=='list') {
var len = args.list.length;
var i = 0;
createAudio();
while(i < len) {
var item = args.list[i];
var type = typeof item;
var src = '';
var title = '';
var type;
if(item !== null && type === 'object') {
if(item.url) {
src = item.url;
}
else {
alert(`MyPlayer: item[${i}]: property "url" is missing.`);
i++;
continue;
}
if(item.title) {
title = item.title;
}
else {
title = url.replace(/(.*\/)?([^\/]+)/, '$2');
}
}
else if(type === 'string') {
src = item;
title = src;
}
else {
alert(`MyPlayer: item[${i}]: unsupported type=${type}.`);
i++;
continue;
} }
if(item.title) type = mimeTypeFromSrc(src);
title = item.title;
else
title = url.replace(/(.*\/)?([^\/]+)/, '$2');
}
else if(type === 'string') {
src = item;
title = src;
}
else {
alert(
'MyPlayer: item [' + i + ']: '+
'unsupported type "' + type + '"'
);
continue; args.audio.append(`<source src="${src}" type="${type}" title="${type}"/>`);
}
var type = mimeTypeFromSrc(src); i++;
}
$(args.audio).append('<source src="'+src+'" type="' + type + '" title="'+title+'"/>'); createPlayer();
} setupDetach();
}
createPlayer(); else {
setupDetach(); $(args.element).append(`<p>Unsupported mode ${args.mode}"</p>`);
} failed = true;
else { }
$(args.element).append('<p>Unsupported mode "'+args.mode+'"</p>');
failed = true;
}
}; };