PEE/src/Embedding.svelte

386 lines
9.9 KiB
Svelte
Raw Normal View History

2022-01-04 20:26:05 +00:00
<script lang="ts">
2022-01-12 08:09:30 +00:00
import { fileTypeFromBuffer, FileTypeResult } from 'file-type'
2022-01-07 04:43:28 +00:00
import { settings, appState } from './stores'
2022-01-05 01:14:23 +00:00
import { beforeUpdate, tick } from 'svelte'
2022-01-12 08:09:30 +00:00
import type { EmbeddedFile } from './main'
import { createEventDispatcher } from 'svelte'
import { GM_head, headerStringToObject } from '../dist/requests'
2022-01-07 04:43:28 +00:00
2022-01-12 08:09:30 +00:00
export const dispatch = createEventDispatcher()
2022-01-04 20:26:05 +00:00
2022-01-05 01:14:23 +00:00
export let file: EmbeddedFile
2022-01-04 20:26:05 +00:00
let isVideo = false
let isImage = false
let isAudio = false
let url = ''
let settled = false
2022-01-12 08:09:30 +00:00
let contracted = true
2022-01-04 20:26:05 +00:00
let hovering = false
2022-01-12 08:09:30 +00:00
let ftype = ''
2022-01-04 20:26:05 +00:00
let place: HTMLDivElement
let hoverElem: HTMLDivElement
let imgElem: HTMLImageElement
let videoElem: HTMLVideoElement
let hoverVideo: HTMLVideoElement
let dims: [number, number] = [0, 0]
2022-01-12 08:09:30 +00:00
let furl: string | undefined = undefined
2022-01-04 20:26:05 +00:00
2022-01-12 08:09:30 +00:00
let visible = false
export const isNotChrome = !navigator.userAgent.includes('Chrome/')
2022-01-05 19:12:12 +00:00
2022-01-12 08:09:30 +00:00
export let id = ''
document.addEventListener('reveal', (e: any) => {
if (e.detail.id == id) visible = !visible
})
2022-01-05 20:50:44 +00:00
2022-01-07 04:43:28 +00:00
export function isContracted() {
2022-01-12 08:09:30 +00:00
return contracted
2022-01-07 04:43:28 +00:00
}
2022-01-04 20:26:05 +00:00
beforeUpdate(async () => {
if (settled) return
settled = true
2022-01-12 08:09:30 +00:00
const thumb = file.thumbnail || file.data
let type: FileTypeResult | undefined
if (typeof thumb != 'string') {
type = await fileTypeFromBuffer(thumb)
url = URL.createObjectURL(new Blob([thumb], { type: type?.mime }))
if (!type) return
} else {
let head = headerStringToObject(await GM_head(thumb, undefined))
type = { ext: '' as any, mime: head['content-type'].split(';')[0].trim() }
}
ftype = type.mime
2022-01-04 20:26:05 +00:00
isVideo = type.mime.startsWith('video/')
isAudio = type.mime.startsWith('audio/')
isImage = type.mime.startsWith('image/')
2022-01-12 08:09:30 +00:00
dispatch('fileinfo', { type })
2022-01-04 20:26:05 +00:00
2022-01-07 04:43:28 +00:00
if (isImage) {
2022-01-12 08:09:30 +00:00
contracted = !$settings.xpi
2022-01-07 04:43:28 +00:00
}
2022-01-04 20:26:05 +00:00
if (isVideo) {
2022-01-07 04:43:28 +00:00
contracted = !$settings.xpv && !$appState.isCatalog
}
2022-01-12 08:09:30 +00:00
if ($appState.isCatalog) contracted = true
2022-01-07 04:43:28 +00:00
if ($settings.pre) {
2022-01-12 08:09:30 +00:00
unzip() // not awaiting on purpose
2022-01-04 20:26:05 +00:00
}
2022-01-07 04:43:28 +00:00
if ($settings.prev) {
2022-01-12 08:09:30 +00:00
let obs = new IntersectionObserver(
(entries, obs) => {
for (const item of entries) {
if (!item.isIntersecting) continue
unzip()
obs.unobserve(place)
}
},
{ root: null, rootMargin: '0px', threshold: 0.01 },
)
obs.observe(place)
2022-01-07 04:43:28 +00:00
}
2022-01-12 08:09:30 +00:00
})
2022-01-04 20:26:05 +00:00
2022-01-12 08:09:30 +00:00
let unzipping = false
let progress = [0, 0]
2022-01-05 01:14:23 +00:00
async function unzip() {
2022-01-12 08:09:30 +00:00
if (!file.thumbnail) return
if (unzipping) return
let type: FileTypeResult | undefined
if (typeof file.data != 'string') {
unzipping = true
let lisn = new EventTarget()
lisn.addEventListener('progress', (e: any) => {
progress = e.detail
})
let full = await file.data(lisn)
type = await fileTypeFromBuffer(full)
furl = URL.createObjectURL(new Blob([full], { type: type?.mime }))
} else {
url = file.data
furl = file.data
let head = headerStringToObject(await GM_head(file.data, undefined))
type = { ext: '' as any, mime: head['content-type'].split(';')[0].trim() }
}
if (!type) return
2022-01-05 01:14:23 +00:00
isVideo = type.mime.startsWith('video/')
isAudio = type.mime.startsWith('audio/')
isImage = type.mime.startsWith('image/')
2022-01-12 08:09:30 +00:00
unzipping = false
dispatch('fileinfo', { type })
2022-01-07 04:43:28 +00:00
2022-01-05 01:14:23 +00:00
if (hovering) {
// reset hovering to recompute proper image coordinates
setTimeout(() => {
2022-01-12 08:09:30 +00:00
recompute()
hoverUpdate()
}, 20)
2022-01-05 01:14:23 +00:00
}
}
2022-01-04 20:26:05 +00:00
function hasAudio(video: any) {
return (
video.mozHasAudio ||
2022-01-12 08:09:30 +00:00
!!video.webkitAudioDecodedByteCount ||
2022-01-05 01:14:23 +00:00
!!(video.audioTracks && video.audioTracks.length)
2022-01-04 20:26:05 +00:00
)
}
2022-01-07 04:43:28 +00:00
export async function bepis(ev: MouseEvent) {
2022-01-12 08:09:30 +00:00
if ($appState.isCatalog) return
2022-01-07 04:43:28 +00:00
if (ev.button == 0) {
contracted = !contracted
if (hovering) hoverStop()
if (contracted && isVideo) {
videoElem.controls = false
videoElem.pause()
}
if (!contracted && isVideo) {
videoElem.controls = true
// has to be delayed
setTimeout(async () => {
2022-01-12 08:09:30 +00:00
videoElem.currentTime = hoverVideo.currentTime || 0
2022-01-07 04:43:28 +00:00
await videoElem.play()
}, 10)
}
if (file.thumbnail && !furl) {
// don't know how you managed to click before hovering but oh well
unzip()
}
2022-01-12 08:09:30 +00:00
ev.preventDefault()
} else if (ev.button == 1) {
// middle click
let src = furl || url
2022-01-07 04:43:28 +00:00
if (ev.altKey && file.source) {
2022-01-12 08:09:30 +00:00
src = file.source
2022-01-07 04:43:28 +00:00
}
if (ev.shiftKey && file.page) {
2022-01-12 08:09:30 +00:00
src = file.page.url
2022-01-07 04:43:28 +00:00
}
2022-01-12 08:09:30 +00:00
ev.preventDefault()
2022-01-07 04:43:28 +00:00
if (isNotChrome) {
2022-01-12 08:09:30 +00:00
window.open(src, '_blank')
} else await GM.openInTab(src, { active: false, insert: true })
2022-01-04 20:26:05 +00:00
}
}
2022-01-12 08:09:30 +00:00
const getViewport = () =>
(typeof visualViewport != 'undefined'
? () => [visualViewport.width, visualViewport.height]
: () => [
document.documentElement.clientWidth,
document.documentElement.clientHeight,
])()
2022-01-05 01:14:23 +00:00
function recompute() {
2022-01-12 08:09:30 +00:00
const [sw, sh] = getViewport()
2022-01-04 20:26:05 +00:00
let [iw, ih] = [0, 0]
if (isImage) {
;[iw, ih] = [imgElem.naturalWidth, imgElem.naturalHeight]
} else {
;[iw, ih] = [videoElem.videoWidth, videoElem.videoHeight]
}
let scale = Math.min(1, sw / iw, sh / ih)
dims = [~~(iw * scale), ~~(ih * scale)]
hoverElem.style.width = `${dims[0]}px`
hoverElem.style.height = `${dims[1]}px`
2022-01-05 01:14:23 +00:00
}
async function hoverStart(ev?: MouseEvent) {
2022-01-12 08:09:30 +00:00
if ($settings.dh) return
2022-01-05 01:14:23 +00:00
if (file.thumbnail && !furl) {
2022-01-12 08:09:30 +00:00
unzip()
2022-01-05 01:14:23 +00:00
}
if (!isImage && !isVideo) return
if (!contracted) return
2022-01-12 08:09:30 +00:00
recompute()
2022-01-05 16:38:02 +00:00
hovering = true
2022-01-05 01:14:23 +00:00
2022-01-12 08:09:30 +00:00
if (isVideo) {
2022-01-05 15:56:45 +00:00
try {
2022-01-12 08:09:30 +00:00
await hoverVideo.play()
} catch (e) {
// probably didn't interact with document error, mute the video and try again?
hoverVideo.muted = true
hoverVideo.volume = 0
await hoverVideo.play()
2022-01-05 15:56:45 +00:00
}
2022-01-05 01:14:23 +00:00
}
2022-01-12 08:09:30 +00:00
}
2022-01-04 20:26:05 +00:00
function hoverStop(ev?: MouseEvent) {
2022-01-12 08:09:30 +00:00
if ($settings.dh) return
2022-01-04 20:26:05 +00:00
hovering = false
if (isVideo) hoverVideo.pause()
}
2022-01-12 08:09:30 +00:00
let lastev: MouseEvent | undefined
function hoverUpdate(ev?: MouseEvent) {
2022-01-12 08:09:30 +00:00
lastev = lastev || ev
if ($settings.dh) return
2022-01-04 20:26:05 +00:00
if (!contracted) return
const [sw, sh] = [visualViewport.width, visualViewport.height]
// shamelessly stolen from 4chanX
2022-01-12 08:09:30 +00:00
if (dims[0] == 0 && dims[1] == 0) recompute()
2022-01-04 20:26:05 +00:00
let width = dims[0]
let height = dims[1] + 25
2022-01-12 08:09:30 +00:00
let { clientX, clientY } = ev || lastev!
2022-01-04 20:26:05 +00:00
let top = Math.max(0, (clientY * (sh - height)) / sh)
let threshold = sw / 2
let marginX: number | string =
(clientX <= threshold ? clientX : sw - clientX) + 45
marginX = Math.min(marginX, sw - width)
marginX = marginX + 'px'
let [left, right] = clientX <= threshold ? [marginX, ''] : ['', marginX]
let { style } = hoverElem
style.top = top + 'px'
style.left = left
style.right = right
}
function adjustAudio(ev: WheelEvent) {
2022-01-07 04:43:28 +00:00
if (!$settings.ca) return
2022-01-04 20:26:05 +00:00
if (!isVideo) return
2022-01-07 04:43:28 +00:00
if ($settings.dh && contracted) return
2022-01-12 08:09:30 +00:00
if (!hasAudio(videoElem)) return
let vol = videoElem.volume * (ev.deltaY > 0 ? 0.9 : 1.1)
vol = Math.max(0, Math.min(1, vol))
videoElem.volume = vol
hoverVideo.volume = videoElem.volume
hoverVideo.muted = vol < 0
2022-01-07 04:43:28 +00:00
ev.preventDefault()
2022-01-04 20:26:05 +00:00
}
</script>
2022-01-05 19:12:12 +00:00
{#if !$settings.eye || visible}
<!-- svelte-ignore a11y-mouse-events-have-key-events -->
<div
class:contract={contracted}
class="place"
2022-01-12 08:09:30 +00:00
on:click={(e) => e.preventDefault()}
on:auxclick={(e) => e.preventDefault()}
on:mousedown={bepis}
2022-01-05 19:12:12 +00:00
on:mouseover={hoverStart}
on:mouseout={hoverStop}
on:mousemove={hoverUpdate}
on:wheel={adjustAudio}
bind:this={place}
>
{#if isImage}
<!-- svelte-ignore a11y-missing-attribute -->
2022-01-12 08:09:30 +00:00
<img
referrerpolicy="no-referrer"
bind:this={imgElem}
alt={file.filename}
src={furl || url}
/>
2022-01-05 19:12:12 +00:00
{/if}
{#if isAudio}
2022-01-05 22:20:20 +00:00
<audio
2022-01-12 08:09:30 +00:00
referrerpolicy="no-referrer"
2022-01-05 22:20:20 +00:00
controls
src={furl || url}
loop={$settings.loop}
alt={file.filename}
>
2022-01-05 22:04:15 +00:00
<source src={furl || url} type={ftype} />
2022-01-05 19:12:12 +00:00
</audio>
{/if}
{#if isVideo}
<!-- svelte-ignore a11y-media-has-caption -->
<!-- svelte-ignore a11y-missing-attribute -->
2022-01-12 08:09:30 +00:00
<video
referrerpolicy="no-referrer"
loop={$settings.loop}
bind:this={videoElem}
src={furl || url}
/>
2022-01-05 19:12:12 +00:00
<!-- assoom videos will never be loaded from thumbnails -->
{/if}
</div>
<div
bind:this={hoverElem}
class:visible={hovering && contracted}
class:unzipping
class="hoverer"
>
{#if unzipping}<span class="progress">[{progress[0]} / {progress[1]}]</span
>{/if}
{#if isImage}
2022-01-12 08:09:30 +00:00
<img referrerpolicy="no-referrer" alt={file.filename} src={furl || url} />
2022-01-05 19:12:12 +00:00
{/if}
{#if isVideo}
<!-- svelte-ignore a11y-media-has-caption -->
2022-01-12 08:09:30 +00:00
<video
referrerpolicy="no-referrer"
loop={$settings.loop}
bind:this={hoverVideo}
src={furl || url}
/>
2022-01-05 19:12:12 +00:00
<!-- assoom videos will never be loaded from thumbnails -->
{/if}
</div>
{/if}
2022-01-04 20:26:05 +00:00
<style scoped>
.place {
cursor: pointer;
max-width: 100vw;
max-height: 100vh;
}
.unzipping > img {
filter: brightness(0.5) blur(10px);
}
.progress {
color: black;
-webkit-text-stroke: 0.7px white;
font-weight: bold;
left: 50%;
top: 50%;
font-size: larger;
display: inline-block;
position: absolute;
z-index: 10;
2022-01-04 20:26:05 +00:00
}
.hoverer {
display: none;
position: fixed;
pointer-events: none;
}
.visible {
display: block;
2022-01-05 15:56:45 +00:00
z-index: 9;
2022-01-04 20:26:05 +00:00
}
.contract img,
.contract video {
2022-01-07 07:05:59 +00:00
max-width: 125px !important;
max-height: 125px !important;
2022-01-04 20:26:05 +00:00
width: auto;
height: auto;
}
.place:not(.contract) video,
.place:not(.contract) img,
2022-01-04 20:26:05 +00:00
.hoverer > video,
.hoverer > img {
2022-01-05 15:56:45 +00:00
max-width: 100vw;
max-height: 100vh;
2022-01-04 20:26:05 +00:00
}
</style>