Share url, removed hash

This commit is contained in:
Pablo Ferreiro 2022-02-07 22:23:33 +01:00
parent d871ed4e68
commit 7203bc1509
No known key found for this signature in database
GPG key ID: 41FBCE65B779FA24
3 changed files with 64 additions and 66 deletions

View file

@ -3,17 +3,17 @@
<section class="section"> <section class="section">
<div class="columns is-multiline is-vcentered"> <div class="columns is-multiline is-vcentered">
{foreach $feed->items as $item} {foreach $feed->items as $item}
<div class="column is-one-quarter"> {do $share_url = 'https://tiktok.com/@' . $item->author->uniqueId . '/' . $item->id}
<a class="clickable-img" id="{$item->id}" href="#{$item->id}" <div class="column is-one-quarter clickable-img" id="{$item->id}" onclick="openVideo(this.id)"
data-video_url="{path('/stream?url=' . urlencode($item->video->playAddr))}" data-video_url="{path('/stream?url=' . urlencode($item->video->playAddr))}"
data-video_download_watermark="{path('/stream?url=' . urlencode($item->video->playAddr) . '&download=1&id=' . $item->id . '&user=' . $item->author->uniqueId) . '&watermark='}" data-video_download_watermark="{path('/stream?url=' . urlencode($item->video->playAddr) . '&download=1&id=' . $item->id . '&user=' . $item->author->uniqueId) . '&watermark='}"
data-video_download_nowatermark="{path('/stream?url=' . urlencode('https://tiktok.com/@' . $item->author->uniqueId . '/' . $item->id) . '&download=1&id=' . $item->id . '&user=' . $item->author->uniqueId)}" data-video_download_nowatermark="{path('/stream?url=' . urlencode($share_url) . '&download=1&id=' . $item->id . '&user=' . $item->author->uniqueId)}"
data-desc="{$item->desc}" data-video_share_url="{$share_url}"
data-music_title="{$item->music->title}" data-desc="{$item->desc}"
data-music_url="{path('/stream?url=' . urlencode($item->music->playUrl))}"> data-music_title="{$item->music->title}"
<img loading="lazy" src="{path('/stream?url=' . urlencode($item->video->originCover))}" /> data-music_url="{path('/stream?url=' . urlencode($item->music->playUrl))}">
<img class="hidden" loading="lazy" data-src="{path('/stream?url=' . urlencode($item->video->dynamicCover))}" /> <img loading="lazy" src="{path('/stream?url=' . urlencode($item->video->originCover))}" />
</a> <img class="hidden" loading="lazy" data-src="{path('/stream?url=' . urlencode($item->video->dynamicCover))}" />
</div> </div>
{/foreach} {/foreach}
</div> </div>
@ -35,10 +35,18 @@
<p class="modal-card-title" id="item_title"></p> <p class="modal-card-title" id="item_title"></p>
</header> </header>
<section class="modal-card-body has-text-centered" style="overflow: hidden;"> <section class="modal-card-body has-text-centered" style="overflow: hidden;">
<video id="video" controls preload="none"></video> <video id="video" autoplay controls></video>
</section> </section>
<footer class="modal-card-foot has-text-centered"> <footer class="modal-card-foot has-text-centered">
<div class="container"> <div class="container">
<div class="field has-addons has-addons-centered">
<div class="control">
<input id="share_input" class="input" readonly />
</div>
<div class="control">
<button class="button is-primary" onclick="copyShare()">Copy</button>
</div>
</div>
<div class="buttons is-centered"> <div class="buttons is-centered">
<a target="_blank" id="download_watermark" class="button is-info" download>Download with watermark</a> <a target="_blank" id="download_watermark" class="button is-info" download>Download with watermark</a>
<a target="_blank" id="download_nowatermark" class="button is-info">Download without watermark</a> <a target="_blank" id="download_nowatermark" class="button is-info">Download without watermark</a>

View file

@ -1,3 +1,5 @@
var opened_video_id = null
const video = document.getElementById('video') const video = document.getElementById('video')
const item_title = document.getElementById('item_title') const item_title = document.getElementById('item_title')
const audio = document.getElementById('audio') const audio = document.getElementById('audio')
@ -5,13 +7,12 @@ const audio_title = document.getElementById('audio_title')
const modal = document.getElementById('modal') const modal = document.getElementById('modal')
const download_watermark = document.getElementById('download_watermark') const download_watermark = document.getElementById('download_watermark')
const download_nowatermark = document.getElementById('download_nowatermark') const download_nowatermark = document.getElementById('download_nowatermark')
const share_input = document.getElementById('share_input')
// -- HELPERS -- // const getVideoDataById = id => {
const getHash = () => window.location.hash.substring(1)
const getVideoDataById = (id) => {
const el = document.getElementById(id) const el = document.getElementById(id)
if (el) { if (el) {
opened_video_id = id
return el.dataset return el.dataset
} }
return false return false
@ -19,19 +20,20 @@ const getVideoDataById = (id) => {
const isModalActive = () => modal.classList.contains('is-active') const isModalActive = () => modal.classList.contains('is-active')
const toggleButton = (id, force) => document.getElementById(id) ? document.getElementById(id).toggleAttribute('disabled', force) : alert('That button does not exist') const toggleButton = (id, force) => document.getElementById(id).toggleAttribute('disabled', force)
// -- MODAL -- // // -- MODAL -- //
const swapData = ({ video_url, desc, video_download_watermark, video_download_nowatermark, music_title, music_url }) => { const swapData = ({ video_url, desc, video_download_watermark, video_download_nowatermark, video_share_url, music_title, music_url }) => {
video.src = video_url video.src = video_url
item_title.innerText = desc item_title.innerText = desc
download_watermark.href = video_download_watermark download_watermark.href = video_download_watermark
download_nowatermark.href = video_download_nowatermark download_nowatermark.href = video_download_nowatermark
share_input.value = video_share_url
audio_title.innerText = music_title audio_title.innerText = music_title
audio.src = music_url audio.src = music_url
} }
const showModal = (id) => { const showModal = id => {
const dataset = getVideoDataById(id) const dataset = getVideoDataById(id)
if (dataset) { if (dataset) {
swapData(dataset) swapData(dataset)
@ -50,27 +52,27 @@ const hideModel = () => {
history.pushState('', document.title, window.location.pathname + window.location.search) history.pushState('', document.title, window.location.pathname + window.location.search)
} }
const getPrevOrNext = (forward) => { const getPrevOrNext = forward => {
const hash = getHash() if (opened_video_id) {
if (hash) { const el = document.getElementById(opened_video_id)
const el = document.getElementById(hash)
if (el) { if (el) {
if (forward) { if (forward) {
return el.parentElement.nextElementSibling ? el.parentElement.nextElementSibling.children[0] : null return el.nextElementSibling
} }
return el.parentElement.previousElementSibling ? el.parentElement.previousElementSibling.children[0] : null return el.previousElementSibling
} }
} }
return null return null
} }
const moveVideo = (forward) => { const moveVideo = forward => {
// Re-enable buttons // Re-enable buttons
toggleButton('back-button', false) toggleButton('back-button', false)
toggleButton('next-button', false) toggleButton('next-button', false)
const new_el = getPrevOrNext(forward) const new_el = getPrevOrNext(forward)
if (new_el) { if (new_el) {
window.location.hash = new_el.id opened_video_id = new_el.id
swapData(new_el.dataset)
} else { } else {
// Max reached, disable buttons depending on direction // Max reached, disable buttons depending on direction
if (forward) { if (forward) {
@ -82,58 +84,42 @@ const moveVideo = (forward) => {
} }
// EVENTS // // EVENTS //
const hashChange = () => { const openVideo = video_id => {
if (window.location.hash) { if (isModalActive()) {
const hash = getHash() const dataset = getVideoDataById(video_id)
if (hash) { if (dataset) {
// Check first if the modal is already active swapData(dataset)
if (isModalActive()) {
// If it is, get hash video id and swap data
const dataset = getVideoDataById(hash)
if (dataset) {
swapData(dataset)
video.play()
}
} else {
showModal(hash)
}
} }
} else {
showModal(video_id)
} }
} }
const swapImages = (e, mode) => { const swapImages = e => {
let img = null const div = e.target
let gif = null const img = div.children[0]
if (mode === 'gif') { const gif = div.children[1]
const a = e.target if (!gif.src) {
img = a.children[0] gif.src = gif.dataset.src
gif = a.children[1]
} else if (mode === 'img') {
gif = e.target
img = e.target.parentElement.children[0]
} }
img.classList.toggle('hidden')
gif.classList.toggle('hidden')
}
if (img && gif) { const copyShare = () => {
if (!gif.src) { share_input.select();
gif.src = gif.dataset.src navigator.clipboard.writeText(share_input.value);
} alert('Copied!')
img.classList.toggle('hidden')
gif.classList.toggle('hidden')
}
} }
document.getElementById('modal-background').addEventListener('click', hideModel) document.getElementById('modal-background').addEventListener('click', hideModel)
document.getElementById('modal-close').addEventListener('click', hideModel) document.getElementById('modal-close').addEventListener('click', hideModel)
document.getElementById('back-button').addEventListener('click', () => moveVideo(false)) document.getElementById('back-button').addEventListener('click', () => moveVideo(false))
document.getElementById('next-button').addEventListener('click', () => moveVideo(true)) document.getElementById('next-button').addEventListener('click', () => moveVideo(true))
window.addEventListener('hashchange', hashChange, false)
// Image hover // Image hover
const images = document.getElementsByClassName("clickable-img") const images = document.getElementsByClassName("clickable-img")
for (let i = 0; i < images.length; i++) { for (let i = 0; i < images.length; i++) {
images[i].addEventListener('mouseenter', e => swapImages(e, 'gif'), false) images[i].addEventListener('mouseenter', swapImages, false)
images[i].addEventListener('mouseout', e => swapImages(e, 'img'), false) images[i].addEventListener('mouseout', swapImages, false)
} }
hashChange()

View file

@ -12,8 +12,12 @@ figure {
display: none; display: none;
} }
/* Make gifs take all available space */ .clickable-img {
cursor: pointer;
}
.clickable-img img { .clickable-img img {
width: 100%; width: 100%;
height: 100%; height: 100%;
pointer-events: none;
} }