2023-07-17 14:37:39 +00:00
|
|
|
#include utils.js
|
|
|
|
#include uiharu.js
|
|
|
|
#include aembed.js
|
|
|
|
#include iembed.js
|
|
|
|
#include vembed.js
|
|
|
|
|
2023-01-29 01:51:54 +00:00
|
|
|
var MszEmbed = (function() {
|
|
|
|
let uiharu = undefined;
|
|
|
|
|
|
|
|
return {
|
|
|
|
init: function(endPoint) {
|
|
|
|
uiharu = new Uiharu(endPoint);
|
|
|
|
},
|
|
|
|
handle: function(targets) {
|
|
|
|
if(!Array.isArray(targets))
|
|
|
|
targets = Array.from(targets);
|
|
|
|
|
|
|
|
const filtered = new Map;
|
|
|
|
for(const target of targets) {
|
|
|
|
if(!(target instanceof HTMLElement)
|
|
|
|
|| !('dataset' in target)
|
|
|
|
|| !('mszEmbedUrl' in target.dataset))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const cleanUrl = target.dataset.mszEmbedUrl.replace(/ /, '%20');
|
|
|
|
if(cleanUrl.indexOf('https://') !== 0
|
2023-03-09 16:18:52 +00:00
|
|
|
&& cleanUrl.indexOf('http://') !== 0
|
|
|
|
&& cleanUrl.indexOf('//') !== 0) {
|
2023-01-29 01:51:54 +00:00
|
|
|
target.textContent = target.dataset.mszEmbedUrl;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$rc(target);
|
|
|
|
target.appendChild($e({
|
|
|
|
tag: 'i',
|
|
|
|
attrs: {
|
|
|
|
className: 'fas fa-2x fa-spinner fa-pulse',
|
|
|
|
style: {
|
|
|
|
width: '32px',
|
|
|
|
height: '32px',
|
|
|
|
lineHeight: '32px',
|
|
|
|
textAlign: 'center',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
|
|
|
if(filtered.has(cleanUrl))
|
|
|
|
filtered.get(cleanUrl).push(target);
|
|
|
|
else
|
|
|
|
filtered.set(cleanUrl, [target]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const replaceWithUrl = function(targets, url) {
|
|
|
|
for(const target of targets) {
|
|
|
|
let body = $e({
|
|
|
|
tag: 'a',
|
|
|
|
attrs: {
|
|
|
|
className: 'link',
|
|
|
|
href: url,
|
|
|
|
target: '_blank',
|
|
|
|
rel: 'noopener noreferrer',
|
|
|
|
},
|
|
|
|
child: url
|
|
|
|
});
|
|
|
|
$ib(target, body);
|
|
|
|
$r(target);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
filtered.forEach(function(targets, url) {
|
|
|
|
uiharu.lookupOne(url, function(metadata) {
|
|
|
|
if(metadata.error) {
|
|
|
|
replaceWithUrl(targets, url);
|
|
|
|
console.error(metadata.error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(metadata.title === undefined) {
|
|
|
|
replaceWithUrl(targets, url);
|
|
|
|
console.warn('Media is no longer available.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let phc = undefined,
|
|
|
|
options = {
|
|
|
|
onembed: console.log,
|
|
|
|
};
|
|
|
|
|
|
|
|
if(metadata.type === 'youtube:video') {
|
|
|
|
phc = MszVideoEmbedPlaceholder;
|
|
|
|
options.type = 'youtube';
|
|
|
|
options.player = MszVideoEmbedYouTube;
|
|
|
|
} else if(metadata.type === 'niconico:video') {
|
|
|
|
phc = MszVideoEmbedPlaceholder;
|
|
|
|
options.type = 'nicovideo';
|
|
|
|
options.player = MszVideoEmbedNicoNico;
|
|
|
|
} else if(metadata.is_video) {
|
|
|
|
phc = MszVideoEmbedPlaceholder;
|
|
|
|
options.type = 'external';
|
|
|
|
options.player = MszVideoEmbedPlayer;
|
|
|
|
//options.frame = MszVideoEmbedFrame;
|
|
|
|
options.nativeControls = true;
|
|
|
|
options.autosize = false;
|
|
|
|
options.maxWidth = 640;
|
|
|
|
options.maxHeight = 360;
|
|
|
|
} else if(metadata.is_audio) {
|
2023-01-29 20:29:20 +00:00
|
|
|
phc = MszAudioEmbedPlaceholder;
|
2023-01-29 01:51:54 +00:00
|
|
|
options.type = 'external';
|
2023-01-29 20:29:20 +00:00
|
|
|
options.player = MszAudioEmbedPlayer;
|
|
|
|
options.nativeControls = true;
|
2023-01-29 01:51:54 +00:00
|
|
|
} else if(metadata.is_image) {
|
2023-01-29 21:06:03 +00:00
|
|
|
phc = MszImageEmbed;
|
2023-01-29 01:51:54 +00:00
|
|
|
options.type = 'external';
|
|
|
|
}
|
|
|
|
|
|
|
|
if(phc === undefined)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for(const target of targets) {
|
2023-01-29 21:06:03 +00:00
|
|
|
const placeholder = new phc(metadata, options, target);
|
2023-01-29 01:51:54 +00:00
|
|
|
if(placeholder !== undefined)
|
|
|
|
placeholder.replaceElement(target);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
})();
|