Embed YouTube

This commit is contained in:
syuilo 2018-03-19 02:56:35 +09:00
parent 8164dd0c2b
commit e0ce00bacd

View file

@ -1,5 +1,9 @@
<template> <template>
<a class="mk-url-preview" :href="url" target="_blank" :title="url" v-if="!fetching"> <iframe v-if="youtubeId" type="text/html" height="250"
:src="`http://www.youtube.com/embed/${youtubeId}`"
frameborder="0"/>
<div v-else>
<a class="mk-url-preview" :href="url" target="_blank" :title="url" v-if="!fetching">
<div class="thumbnail" v-if="thumbnail" :style="`background-image: url(${thumbnail})`"></div> <div class="thumbnail" v-if="thumbnail" :style="`background-image: url(${thumbnail})`"></div>
<article> <article>
<header> <header>
@ -11,7 +15,8 @@
<p>{{ sitename }}</p> <p>{{ sitename }}</p>
</footer> </footer>
</article> </article>
</a> </a>
</div>
</template> </template>
<script lang="ts"> <script lang="ts">
@ -26,10 +31,18 @@ export default Vue.extend({
description: null, description: null,
thumbnail: null, thumbnail: null,
icon: null, icon: null,
sitename: null sitename: null,
youtubeId: null
}; };
}, },
created() { created() {
const url = new URL(this.url);
if (url.hostname == 'www.youtube.com') {
this.youtubeId = url.searchParams.get('v');
} else if (url.hostname == 'youtu.be') {
this.youtubeId = url.pathname;
} else {
fetch('/api:url?url=' + this.url).then(res => { fetch('/api:url?url=' + this.url).then(res => {
res.json().then(info => { res.json().then(info => {
this.title = info.title; this.title = info.title;
@ -42,10 +55,14 @@ export default Vue.extend({
}); });
}); });
} }
}
}); });
</script> </script>
<style lang="stylus" scoped> <style lang="stylus" scoped>
iframe
width 100%
.mk-url-preview .mk-url-preview
display block display block
font-size 16px font-size 16px