Automatically loop and play Twitter animated gifs without video controls
This commit is contained in:
parent
198a5c29bf
commit
5338cd88fe
|
@ -36,8 +36,10 @@ struct TwitterExtendedMedia: Codable {
|
|||
switch type {
|
||||
case "photo":
|
||||
html += renderPhotoAsHTML()
|
||||
case "video", "animated_gif":
|
||||
case "video":
|
||||
html += renderVideoAsHTML()
|
||||
case "animated_gif":
|
||||
html += renderAnimatedGIFAsHTML()
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
@ -74,6 +76,21 @@ private extension TwitterExtendedMedia {
|
|||
return html
|
||||
}
|
||||
|
||||
func renderAnimatedGIFAsHTML() -> String {
|
||||
guard let bestVariantURL = findBestVariant()?.url else { return "" }
|
||||
|
||||
var html = "<video class=\"nnwAnimatedGIF\" "
|
||||
|
||||
if let httpsMediaURL = httpsMediaURL {
|
||||
html += "poster=\"\(httpsMediaURL)\" "
|
||||
} else if let mediaURL = mediaURL {
|
||||
html += "poster=\"\(mediaURL)\" "
|
||||
}
|
||||
|
||||
html += "src=\"\(bestVariantURL)\" autoplay muted loop></video>"
|
||||
return html
|
||||
}
|
||||
|
||||
func findBestVariant() -> TwitterVideo.Variant? {
|
||||
var best: TwitterVideo.Variant? = nil
|
||||
if let variants = video?.variants {
|
||||
|
|
|
@ -53,7 +53,9 @@ function wrapTables() {
|
|||
function inlineVideos() {
|
||||
document.querySelectorAll("video").forEach(element => {
|
||||
element.setAttribute("playsinline", true)
|
||||
if !element.classList.contains("nnwAnimatedGIF") {
|
||||
element.setAttribute("controls", true)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue