2020-01-29 21:37:25 +02:00
|
|
|
<template>
|
2022-12-27 11:29:39 +02:00
|
|
|
<div v-if="playerEnabled" class="player" :style="`padding: ${(player.height || 0) / (player.width || 1) * 100}% 0 0`">
|
2022-12-19 12:01:30 +02:00
|
|
|
<button class="disablePlayer" :title="i18n.ts.disablePlayer" @click="playerEnabled = false"><i class="ti ti-x"></i></button>
|
2022-07-05 17:13:28 +03:00
|
|
|
<iframe :src="player.url + (player.url.match(/\?/) ? '&autoplay=1&auto_play=1' : '?autoplay=1&auto_play=1')" :width="player.width || '100%'" :heigth="player.height || 250" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen/>
|
2020-01-29 21:37:25 +02:00
|
|
|
</div>
|
2021-11-19 12:36:12 +02:00
|
|
|
<div v-else-if="tweetId && tweetExpanded" ref="twitter" class="twitter">
|
2022-01-16 01:24:53 +02:00
|
|
|
<iframe ref="tweet" scrolling="no" frameborder="no" :style="{ position: 'relative', width: '100%', height: `${tweetHeight}px` }" :src="`https://platform.twitter.com/embed/index.html?embedId=${embedId}&hideCard=false&hideThread=false&lang=en&theme=${$store.state.darkMode ? 'dark' : 'light'}&id=${tweetId}`"></iframe>
|
2020-01-29 21:37:25 +02:00
|
|
|
</div>
|
2022-12-28 02:32:22 +02:00
|
|
|
<div v-else class="mk-url-preview">
|
2022-12-30 07:24:00 +02:00
|
|
|
<component :is="self ? 'MkA' : 'a'" class="link" :class="{ compact }" :[attr]="self ? url.substr(local.length) : url" rel="nofollow noopener" :target="target" :title="url">
|
|
|
|
<div v-if="thumbnail" class="thumbnail" :style="`background-image: url('${thumbnail}')`">
|
|
|
|
</div>
|
|
|
|
<article>
|
|
|
|
<header>
|
|
|
|
<h1 v-if="unknownUrl">{{ url }}</h1>
|
|
|
|
<h1 v-else-if="fetching"><MkEllipsis/></h1>
|
|
|
|
<h1 v-else :title="title">{{ title }}</h1>
|
|
|
|
</header>
|
|
|
|
<p v-if="unknownUrl">{{ i18n.ts.cannotLoad }}</p>
|
|
|
|
<p v-else-if="fetching"><MkEllipsis/></p>
|
|
|
|
<p v-else-if="description" :title="description">{{ description.length > 85 ? description.slice(0, 85) + '…' : description }}</p>
|
|
|
|
<footer>
|
|
|
|
<img v-if="icon" class="icon" :src="icon"/>
|
|
|
|
<p v-if="unknownUrl">?</p>
|
|
|
|
<p v-else-if="fetching"><MkEllipsis/></p>
|
|
|
|
<p v-else :title="sitename">{{ sitename }}</p>
|
|
|
|
</footer>
|
|
|
|
</article>
|
|
|
|
</component>
|
2022-12-28 11:22:46 +02:00
|
|
|
<div v-if="tweetId" class="action">
|
|
|
|
<MkButton :small="true" inline @click="tweetExpanded = true">
|
2022-12-20 08:15:34 +02:00
|
|
|
<i class="ti ti-brand-twitter"></i> {{ i18n.ts.expandTweet }}
|
2022-12-28 11:22:46 +02:00
|
|
|
</MkButton>
|
|
|
|
</div>
|
|
|
|
<div v-if="!playerEnabled && player.url" class="action">
|
|
|
|
<MkButton :small="true" inline @click="playerEnabled = true">
|
|
|
|
<i class="ti ti-player-play"></i> {{ i18n.ts.enablePlayer }}
|
|
|
|
</MkButton>
|
|
|
|
<MkButton v-if="!isMobile" :small="true" inline @click="openPlayer()">
|
|
|
|
<i class="ti ti-picture-in-picture"></i> {{ i18n.ts.openInWindow }}
|
|
|
|
</MkButton>
|
2020-07-11 00:25:20 +03:00
|
|
|
</div>
|
2020-01-29 21:37:25 +02:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-01-16 01:24:53 +02:00
|
|
|
<script lang="ts" setup>
|
2022-09-22 02:20:31 +03:00
|
|
|
import { defineAsyncComponent, onMounted, onUnmounted } from 'vue';
|
2023-01-01 10:11:33 +02:00
|
|
|
import { url as local } from '@/config';
|
2022-07-20 16:24:26 +03:00
|
|
|
import { i18n } from '@/i18n';
|
2022-09-22 02:20:31 +03:00
|
|
|
import * as os from '@/os';
|
|
|
|
import { deviceKind } from '@/scripts/device-kind';
|
2022-12-28 11:22:46 +02:00
|
|
|
import MkButton from '@/components/MkButton.vue';
|
2023-01-01 10:11:33 +02:00
|
|
|
import { versatileLang } from '@/scripts/intl-const';
|
2020-01-29 21:37:25 +02:00
|
|
|
|
2022-01-16 01:24:53 +02:00
|
|
|
const props = withDefaults(defineProps<{
|
|
|
|
url: string;
|
|
|
|
detail?: boolean;
|
|
|
|
compact?: boolean;
|
|
|
|
}>(), {
|
|
|
|
detail: false,
|
|
|
|
compact: false,
|
|
|
|
});
|
|
|
|
|
2022-09-22 02:20:31 +03:00
|
|
|
const MOBILE_THRESHOLD = 500;
|
|
|
|
const isMobile = $ref(deviceKind === 'smartphone' || window.innerWidth <= MOBILE_THRESHOLD);
|
|
|
|
|
2022-01-16 01:24:53 +02:00
|
|
|
const self = props.url.startsWith(local);
|
|
|
|
const attr = self ? 'to' : 'href';
|
|
|
|
const target = self ? null : '_blank';
|
|
|
|
let fetching = $ref(true);
|
|
|
|
let title = $ref<string | null>(null);
|
|
|
|
let description = $ref<string | null>(null);
|
|
|
|
let thumbnail = $ref<string | null>(null);
|
|
|
|
let icon = $ref<string | null>(null);
|
|
|
|
let sitename = $ref<string | null>(null);
|
|
|
|
let player = $ref({
|
|
|
|
url: null,
|
|
|
|
width: null,
|
2022-07-05 17:13:28 +03:00
|
|
|
height: null,
|
2022-01-16 01:24:53 +02:00
|
|
|
});
|
|
|
|
let playerEnabled = $ref(false);
|
|
|
|
let tweetId = $ref<string | null>(null);
|
|
|
|
let tweetExpanded = $ref(props.detail);
|
2022-12-03 12:42:05 +02:00
|
|
|
const embedId = `embed${Math.random().toString().replace(/\D/, '')}`;
|
2022-01-16 01:24:53 +02:00
|
|
|
let tweetHeight = $ref(150);
|
2022-12-30 07:24:00 +02:00
|
|
|
let unknownUrl = $ref(false);
|
2022-01-16 01:24:53 +02:00
|
|
|
|
|
|
|
const requestUrl = new URL(props.url);
|
|
|
|
|
2022-08-24 15:02:06 +03:00
|
|
|
if (requestUrl.hostname === 'twitter.com' || requestUrl.hostname === 'mobile.twitter.com') {
|
2022-01-16 01:24:53 +02:00
|
|
|
const m = requestUrl.pathname.match(/^\/.+\/status(?:es)?\/(\d+)/);
|
|
|
|
if (m) tweetId = m[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (requestUrl.hostname === 'music.youtube.com' && requestUrl.pathname.match('^/(?:watch|channel)')) {
|
|
|
|
requestUrl.hostname = 'www.youtube.com';
|
|
|
|
}
|
|
|
|
|
|
|
|
requestUrl.hash = '';
|
|
|
|
|
2023-01-01 10:11:33 +02:00
|
|
|
window.fetch(`/url?url=${encodeURIComponent(requestUrl.href)}&lang=${versatileLang}`).then(res => {
|
2022-01-16 01:24:53 +02:00
|
|
|
res.json().then(info => {
|
2022-12-30 07:24:00 +02:00
|
|
|
if (info.url == null) {
|
|
|
|
unknownUrl = true;
|
|
|
|
return;
|
|
|
|
}
|
2022-01-16 01:24:53 +02:00
|
|
|
title = info.title;
|
|
|
|
description = info.description;
|
|
|
|
thumbnail = info.thumbnail;
|
|
|
|
icon = info.icon;
|
|
|
|
sitename = info.sitename;
|
|
|
|
fetching = false;
|
|
|
|
player = info.player;
|
2022-06-10 08:36:55 +03:00
|
|
|
});
|
2022-01-16 01:24:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
function adjustTweetHeight(message: any) {
|
|
|
|
if (message.origin !== 'https://platform.twitter.com') return;
|
|
|
|
const embed = message.data?.['twttr.embed'];
|
|
|
|
if (embed?.method !== 'twttr.private.resize') return;
|
|
|
|
if (embed?.id !== embedId) return;
|
|
|
|
const height = embed?.params[0]?.height;
|
|
|
|
if (height) tweetHeight = height;
|
|
|
|
}
|
|
|
|
|
2022-09-22 02:20:31 +03:00
|
|
|
const openPlayer = (): void => {
|
|
|
|
os.popup(defineAsyncComponent(() => import('@/components/MkYoutubePlayer.vue')), {
|
|
|
|
url: requestUrl.href,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-01-16 01:24:53 +02:00
|
|
|
(window as any).addEventListener('message', adjustTweetHeight);
|
2020-01-29 21:37:25 +02:00
|
|
|
|
2022-01-16 01:24:53 +02:00
|
|
|
onUnmounted(() => {
|
|
|
|
(window as any).removeEventListener('message', adjustTweetHeight);
|
2020-01-29 21:37:25 +02:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2022-12-27 11:29:39 +02:00
|
|
|
<style lang="scss" scoped>
|
|
|
|
.player {
|
2020-01-29 21:37:25 +02:00
|
|
|
position: relative;
|
|
|
|
width: 100%;
|
|
|
|
|
|
|
|
> button {
|
|
|
|
position: absolute;
|
|
|
|
top: -1.5em;
|
|
|
|
right: 0;
|
|
|
|
font-size: 1em;
|
|
|
|
width: 1.5em;
|
|
|
|
height: 1.5em;
|
|
|
|
padding: 0;
|
|
|
|
margin: 0;
|
|
|
|
color: var(--fg);
|
|
|
|
background: rgba(128, 128, 128, 0.2);
|
|
|
|
opacity: 0.7;
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
opacity: 0.9;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> iframe {
|
|
|
|
height: 100%;
|
|
|
|
left: 0;
|
|
|
|
position: absolute;
|
|
|
|
top: 0;
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.mk-url-preview {
|
2022-07-05 17:13:28 +03:00
|
|
|
> .link {
|
2020-01-29 21:37:25 +02:00
|
|
|
position: relative;
|
|
|
|
display: block;
|
|
|
|
font-size: 14px;
|
2020-03-20 12:06:50 +02:00
|
|
|
box-shadow: 0 0 0 1px var(--divider);
|
2021-01-01 15:41:20 +02:00
|
|
|
border-radius: 8px;
|
2021-03-02 15:57:16 +02:00
|
|
|
overflow: hidden;
|
2020-01-29 21:37:25 +02:00
|
|
|
|
|
|
|
&:hover {
|
|
|
|
text-decoration: none;
|
|
|
|
border-color: rgba(0, 0, 0, 0.2);
|
|
|
|
|
|
|
|
> article > header > h1 {
|
|
|
|
text-decoration: underline;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .thumbnail {
|
|
|
|
position: absolute;
|
|
|
|
width: 100px;
|
|
|
|
height: 100%;
|
|
|
|
background-position: center;
|
|
|
|
background-size: cover;
|
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
|
|
|
|
& + article {
|
|
|
|
left: 100px;
|
|
|
|
width: calc(100% - 100px);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> article {
|
|
|
|
position: relative;
|
|
|
|
box-sizing: border-box;
|
|
|
|
padding: 16px;
|
|
|
|
|
|
|
|
> header {
|
|
|
|
margin-bottom: 8px;
|
|
|
|
|
|
|
|
> h1 {
|
|
|
|
margin: 0;
|
|
|
|
font-size: 1em;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> p {
|
|
|
|
margin: 0;
|
|
|
|
font-size: 0.8em;
|
|
|
|
}
|
|
|
|
|
|
|
|
> footer {
|
|
|
|
margin-top: 8px;
|
|
|
|
height: 16px;
|
|
|
|
|
|
|
|
> img {
|
|
|
|
display: inline-block;
|
|
|
|
width: 16px;
|
|
|
|
height: 16px;
|
|
|
|
margin-right: 4px;
|
|
|
|
vertical-align: top;
|
|
|
|
}
|
|
|
|
|
|
|
|
> p {
|
|
|
|
display: inline-block;
|
|
|
|
margin: 0;
|
|
|
|
color: var(--urlPreviewInfo);
|
|
|
|
font-size: 0.8em;
|
|
|
|
line-height: 16px;
|
|
|
|
vertical-align: top;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
&.compact {
|
|
|
|
> article {
|
|
|
|
> header h1, p, footer {
|
2021-03-02 15:57:16 +02:00
|
|
|
overflow: hidden;
|
2020-01-29 21:37:25 +02:00
|
|
|
white-space: nowrap;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-28 11:22:46 +02:00
|
|
|
|
|
|
|
> .action {
|
|
|
|
display: flex;
|
|
|
|
gap: 6px;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
margin-top: 6px;
|
|
|
|
}
|
2020-01-29 21:37:25 +02:00
|
|
|
}
|
2022-12-26 01:40:13 +02:00
|
|
|
|
|
|
|
@container (max-width: 400px) {
|
|
|
|
.mk-url-preview {
|
|
|
|
> .link {
|
|
|
|
font-size: 12px;
|
|
|
|
|
|
|
|
> .thumbnail {
|
|
|
|
height: 80px;
|
|
|
|
}
|
|
|
|
|
|
|
|
> article {
|
|
|
|
padding: 12px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@container (max-width: 350px) {
|
|
|
|
.mk-url-preview {
|
|
|
|
> .link {
|
|
|
|
font-size: 10px;
|
|
|
|
|
|
|
|
> .thumbnail {
|
|
|
|
height: 70px;
|
|
|
|
}
|
|
|
|
|
|
|
|
> article {
|
|
|
|
padding: 8px;
|
|
|
|
|
|
|
|
> header {
|
|
|
|
margin-bottom: 4px;
|
|
|
|
}
|
|
|
|
|
|
|
|
> footer {
|
|
|
|
margin-top: 4px;
|
|
|
|
|
|
|
|
> img {
|
|
|
|
width: 12px;
|
|
|
|
height: 12px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
&.compact {
|
|
|
|
> .thumbnail {
|
|
|
|
position: absolute;
|
|
|
|
width: 56px;
|
|
|
|
height: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
> article {
|
|
|
|
left: 56px;
|
|
|
|
width: calc(100% - 56px);
|
|
|
|
padding: 4px;
|
|
|
|
|
|
|
|
> header {
|
|
|
|
margin-bottom: 2px;
|
|
|
|
}
|
|
|
|
|
|
|
|
> footer {
|
|
|
|
margin-top: 2px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 21:37:25 +02:00
|
|
|
</style>
|