Skip to content

Commit c8596a7

Browse files
committed
Added Setting to allow for disabling of video embeds. Aligns with similar settings for other content types.
1 parent d9c3e67 commit c8596a7

5 files changed

Lines changed: 62 additions & 4 deletions

File tree

App/Misc/HTMLRenderingHelpers.swift

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,51 @@ extension HTMLDocument {
458458

459459
a.parent?.replace(child: a, with: videoElement)
460460
}
461-
461+
462+
}
463+
}
464+
}
465+
466+
/**
467+
Replaces video-embed elements with plain links to the underlying URL. Pair with skipping `embedVideos()` and `useHTML5VimeoPlayer()` to honour a "don't embed videos" preference.
468+
*/
469+
func linkifyResidualVideoEmbeds() {
470+
for iframe in nodes(matchingParsedSelector: .cached("iframe[src]")) {
471+
guard let src = iframe["src"], !src.isEmpty else { continue }
472+
let link = HTMLElement(tagName: "a", attributes: ["href": src])
473+
link.textContent = src
474+
iframe.parent?.replace(child: iframe, with: link)
475+
}
476+
477+
for video in nodes(matchingParsedSelector: .cached("video")) {
478+
let src = video["src"] ?? video.firstNode(matchingParsedSelector: .cached("source[src]"))?["src"]
479+
guard let src, !src.isEmpty else { continue }
480+
let link = HTMLElement(tagName: "a", attributes: ["href": src])
481+
link.textContent = src
482+
video.parent?.replace(child: video, with: link)
483+
}
484+
485+
for param in nodes(matchingParsedSelector: .cached("div.bbcode_video object param[name='movie']")) {
486+
guard
487+
let value = param["value"],
488+
let object = param.parentElement,
489+
object.tagName == "object",
490+
let div = object.parentElement,
491+
div.tagName == "div",
492+
div.hasClass("bbcode_video")
493+
else { continue }
494+
495+
let href: String
496+
if let sourceURL = URL(string: value),
497+
let clipID = sourceURL.valueForFirstQueryItem(named: "clip_id") {
498+
href = "https://vimeo.com/\(clipID)"
499+
} else {
500+
href = value
462501
}
502+
503+
let link = HTMLElement(tagName: "a", attributes: ["href": href])
504+
link.textContent = href
505+
div.parent?.replace(child: div, with: link)
463506
}
464507
}
465508
}

App/View Controllers/Posts/PostRenderModel.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ private func massageHTML(_ html: String, isIgnored: Bool, forumID: String) -> St
103103
document.removeEmptyEditedByParagraphs()
104104
document.addAttributeToBlueskyLinks()
105105
document.addAttributeToTweetLinks()
106-
document.useHTML5VimeoPlayer()
106+
let embedVideos = UserDefaults.standard.defaultingValue(for: Settings.embedVideos)
107+
if embedVideos {
108+
document.useHTML5VimeoPlayer()
109+
}
107110
if let username = UserDefaults.standard.value(for: Settings.username) {
108111
document.identifyQuotesCitingUser(named: username, shouldHighlight: true)
109112
document.identifyMentionsOfUser(named: username, shouldHighlight: true)
@@ -118,7 +121,11 @@ private func massageHTML(_ html: String, isIgnored: Bool, forumID: String) -> St
118121
if (ForumTweaks(ForumID(forumID))?.magicCake) == true {
119122
document.addMagicCakeCSS()
120123
}
121-
document.embedVideos()
124+
if embedVideos {
125+
document.embedVideos()
126+
} else {
127+
document.linkifyResidualVideoEmbeds()
128+
}
122129
return document.bodyElement?.innerHTML ?? ""
123130
}
124131

AwfulSettings/Sources/AwfulSettings/Settings.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public enum Settings {
6262
/// Turn each link to a tweet in a post into an embedded tweet.
6363
public static let embedTweets = Setting(key: "embed_tweets", default: true)
6464

65+
/// When `false`, leave video URLs as plain links instead of embedding YouTube, Vimeo, Imgur, etc. video players in posts.
66+
public static let embedVideos = Setting(key: "embed_videos", default: true)
67+
6568
/// Show custom titles for authors in the posts view (when it's wide enough).
6669
public static let enableCustomTitlePostLayout = Setting(key: "enable_custom_title_post_layout", default: false)
6770

AwfulSettingsUI/Sources/AwfulSettingsUI/Localizable.xcstrings

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@
9999
},
100100
"Embed Tweets" : {
101101

102+
},
103+
"Embed Videos" : {
104+
102105
},
103106
"Enable Custom Title Post Layout" : {
104107

@@ -207,4 +210,4 @@
207210
}
208211
},
209212
"version" : "1.0"
210-
}
213+
}

AwfulSettingsUI/Sources/AwfulSettingsUI/SettingsView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public struct SettingsView: View {
1919
@AppStorage(Settings.jumpToPostEndOnDoubleTap) private var doubleTapPostToJump
2020
@AppStorage(Settings.embedBlueskyPosts) private var embedBlueskyPosts
2121
@AppStorage(Settings.embedTweets) private var embedTweets
22+
@AppStorage(Settings.embedVideos) private var embedVideos
2223
@AppStorage(Settings.enableHaptics) private var enableHaptics
2324
@AppStorage(Settings.fontScale) private var fontScale
2425
@AppStorage(Settings.frogAndGhostEnabled) private var frogAndGhostEnabled
@@ -151,6 +152,7 @@ public struct SettingsView: View {
151152
Toggle("Always Animate GIFs", bundle: .module, isOn: $alwaysAnimateGIFs)
152153
Toggle("Embed Bluesky Posts", bundle: .module, isOn: $embedBlueskyPosts)
153154
Toggle("Embed Tweets", bundle: .module, isOn: $embedTweets)
155+
Toggle("Embed Videos", bundle: .module, isOn: $embedVideos)
154156
Toggle("Double-Tap Post to Jump", bundle: .module, isOn: $doubleTapPostToJump)
155157
Toggle("Immersive Mode", bundle: .module, isOn: $immersiveModeEnabled)
156158
Toggle("Enable Haptics", bundle: .module, isOn: $enableHaptics)

0 commit comments

Comments
 (0)