Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions assets/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,14 @@ player.on('timeupdate', function () {
let elem_yt_watch = document.getElementById('link-yt-watch');
let elem_yt_embed = document.getElementById('link-yt-embed');

let base_url_yt_watch = elem_yt_watch.getAttribute('data-base-url');
let base_url_yt_embed = elem_yt_embed.getAttribute('data-base-url');
// YT links could be turned off by the user
if (elem_yt_watch) {
let base_url_yt_watch = elem_yt_watch.getAttribute('data-base-url');
let base_url_yt_embed = elem_yt_embed.getAttribute('data-base-url');

elem_yt_watch.href = addCurrentTimeToURL(base_url_yt_watch);
elem_yt_embed.href = addCurrentTimeToURL(base_url_yt_embed);
elem_yt_watch.href = addCurrentTimeToURL(base_url_yt_watch);
elem_yt_embed.href = addCurrentTimeToURL(base_url_yt_embed);
}

// Invidious links

Expand Down Expand Up @@ -174,7 +177,7 @@ var shareOptions = {
};

if (location.pathname.startsWith('/embed/')) {
var overlay_content = '<h1><a rel="noopener" target="_blank" href="' + location.origin + '/watch?v=' + video_data.id + '">' + player_data.title + '</a></h1>';
var overlay_content = '<h1><a rel="noopener noreferrer" target="_blank" href="' + location.origin + '/watch?v=' + video_data.id + '">' + player_data.title + '</a></h1>';
player.overlay({
overlays: [
{ start: 'loadstart', content: overlay_content, end: 'playing', align: 'top'},
Expand Down
2 changes: 1 addition & 1 deletion assets/js/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function get_reddit_comments() {
</b> \
</p> \
<b> \
<a rel="noopener" target="_blank" href="https://reddit.com{permalink}">{redditPermalinkText}</a> \
<a rel="noopener noreferrer" target="_blank" href="https://reddit.com{permalink}">{redditPermalinkText}</a> \
</b> \
</div> \
<div>{contentHtml}</div> \
Expand Down
14 changes: 14 additions & 0 deletions config/config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,20 @@ default_user_preferences:
##
#automatic_instance_redirect: false

##
## Display YouTube redirect links on pages where they are provided.
## - Watch on Youtube (video page)
## - [YT] Comment link (comments)
## - After which you should try to page (error page)
## - watch on youtube button (search results page)
## - View channel on YouTube (channel page)
## - View playlist on YouTube (playlist page)
##
## Accepted values: true, false
## Default: true
##
#include_youtube_links: true

##
## Show the entire video description by default (when set to 'false',
## only the first few lines of the description are shown and a
Expand Down
1 change: 1 addition & 0 deletions locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"preferences_thin_mode_label": "Thin mode: ",
"preferences_category_misc": "Miscellaneous preferences",
"preferences_automatic_instance_redirect_label": "Automatic instance redirection (fallback to redirect.invidious.io): ",
"preferences_include_youtube_links_label": "Show YouTube redirect links: ",
"preferences_category_subscription": "Subscription preferences",
"preferences_annotations_subscribed_label": "Show annotations by default for subscribed channels? ",
"Redirect homepage to feed: ": "Redirect homepage to feed: ",
Expand Down
12 changes: 6 additions & 6 deletions src/invidious/channels/community.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
private IMAGE_QUALITIES = {320, 560, 640, 1280, 2000}

# TODO: Add "sort_by"
def fetch_channel_community(ucid, cursor, locale, format, thin_mode)
def fetch_channel_community(ucid, cursor, locale, format, thin_mode, include_youtube_links)
if cursor.nil?
# Egljb21tdW5pdHk%3D is the protobuf object to load "community"
initial_data = YoutubeAPI.browse(ucid, params: "Egljb21tdW5pdHk%3D")
Expand All @@ -21,10 +21,10 @@ def fetch_channel_community(ucid, cursor, locale, format, thin_mode)
items = container.as_a
end

return extract_channel_community(items, ucid: ucid, locale: locale, format: format, thin_mode: thin_mode)
return extract_channel_community(items, ucid: ucid, locale: locale, format: format, thin_mode: thin_mode, include_youtube_links: include_youtube_links)
end

def fetch_channel_community_post(ucid, post_id, locale, format, thin_mode)
def fetch_channel_community_post(ucid, post_id, locale, format, thin_mode, include_youtube_links)
object = {
"2:string" => "community",
"25:embedded" => {
Expand All @@ -47,10 +47,10 @@ def fetch_channel_community_post(ucid, post_id, locale, format, thin_mode)
items << item
end

return extract_channel_community(items, ucid: ucid, locale: locale, format: format, thin_mode: thin_mode, is_single_post: true)
return extract_channel_community(items, ucid: ucid, locale: locale, format: format, thin_mode: thin_mode, include_youtube_links: include_youtube_links, is_single_post: true)
end

def extract_channel_community(items, *, ucid, locale, format, thin_mode, is_single_post : Bool = false)
def extract_channel_community(items, *, ucid, locale, format, thin_mode, include_youtube_links, is_single_post : Bool = false)
if message = items[0]["messageRenderer"]?
error_message = (message["text"]["simpleText"]? ||
message["text"]["runs"]?.try &.[0]?.try &.["text"]?)
Expand Down Expand Up @@ -279,7 +279,7 @@ def extract_channel_community(items, *, ucid, locale, format, thin_mode, is_sing

if format == "html"
response = JSON.parse(response)
content_html = IV::Frontend::Comments.template_youtube(response, locale, thin_mode)
content_html = IV::Frontend::Comments.template_youtube(response, locale, thin_mode, include_youtube_links)

response = JSON.build do |json|
json.object do
Expand Down
8 changes: 4 additions & 4 deletions src/invidious/comments/youtube.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Invidious::Comments
extend self

def fetch_youtube(id, cursor, format, locale, thin_mode, region, sort_by = "top")
def fetch_youtube(id, cursor, format, locale, thin_mode, region, include_youtube_links, sort_by = "top")
case cursor
when nil, ""
ctoken = Comments.produce_continuation(id, cursor: "", sort_by: sort_by)
Expand All @@ -13,7 +13,7 @@ module Invidious::Comments

client_config = YoutubeAPI::ClientConfig.new(region: region)
response = YoutubeAPI.next(continuation: ctoken, client_config: client_config)
return parse_youtube(id, response, format, locale, thin_mode, sort_by)
return parse_youtube(id, response, format, locale, thin_mode, include_youtube_links, sort_by)
end

def fetch_community_post_comments(ucid, post_id)
Expand Down Expand Up @@ -57,7 +57,7 @@ module Invidious::Comments
return initial_data
end

def parse_youtube(id, response, format, locale, thin_mode, sort_by = "top", is_post = false)
def parse_youtube(id, response, format, locale, thin_mode, include_youtube_links, sort_by = "top", is_post = false)
contents = nil

if on_response_received_endpoints = response["onResponseReceivedEndpoints"]?
Expand Down Expand Up @@ -302,7 +302,7 @@ module Invidious::Comments

if format == "html"
response = JSON.parse(response)
content_html = Frontend::Comments.template_youtube(response, locale, thin_mode)
content_html = Frontend::Comments.template_youtube(response, locale, thin_mode, include_youtube_links)
response = JSON.build do |json|
json.object do
json.field "contentHtml", content_html
Expand Down
1 change: 1 addition & 0 deletions src/invidious/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct ConfigPreferences
property default_home : String? = "Popular"
property feed_menu : Array(String) = ["Popular", "Trending", "Subscriptions", "Playlists"]
property automatic_instance_redirect : Bool = false
property include_youtube_links : Bool = true
property region : String = "US"
property related_videos : Bool = true
property sort : String = "published"
Expand Down
24 changes: 13 additions & 11 deletions src/invidious/frontend/comments_youtube.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Invidious::Frontend::Comments
extend self

def template_youtube(comments, locale, thin_mode, is_replies = false)
def template_youtube(comments, locale, thin_mode, include_youtube_links, is_replies = false)
String.build do |html|
root = comments["comments"].as_a
root.each do |child|
Expand Down Expand Up @@ -147,16 +147,18 @@ module Invidious::Frontend::Comments
|
END_HTML

if comments["videoId"]?
html << <<-END_HTML
<a rel="noreferrer noopener" href="https://www.youtube.com/watch?v=#{comments["videoId"]}&lc=#{child["commentId"]}" title="#{translate(locale, "YouTube comment permalink")}">[YT]</a>
|
END_HTML
elsif comments["authorId"]?
html << <<-END_HTML
<a rel="noreferrer noopener" href="https://www.youtube.com/channel/#{comments["authorId"]}/community?lb=#{child["commentId"]}" title="#{translate(locale, "YouTube comment permalink")}">[YT]</a>
|
END_HTML
if include_youtube_links
if comments["videoId"]?
html << <<-END_HTML
<a rel="noreferrer noopener" href="https://www.youtube.com/watch?v=#{comments["videoId"]}&lc=#{child["commentId"]}" title="#{translate(locale, "YouTube comment permalink")}">[YT]</a>
|
END_HTML
elsif comments["authorId"]?
html << <<-END_HTML
<a rel="noreferrer noopener" href="https://www.youtube.com/channel/#{comments["authorId"]}/community?lb=#{child["commentId"]}" title="#{translate(locale, "YouTube comment permalink")}">[YT]</a>
|
END_HTML
end
end

html << <<-END_HTML
Expand Down
12 changes: 11 additions & 1 deletion src/invidious/helpers/errors.cr
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def error_redirect_helper(env : HTTP::Server::Context)
go_to_youtube = translate(locale, "next_steps_error_message_go_to_youtube")
switch_instance = translate(locale, "Switch Invidious Instance")

return <<-END_HTML
html = <<-END_HTML
<p style="margin-bottom: 4px;">#{next_steps_text}</p>
<ul>
<li>
Expand All @@ -191,11 +191,21 @@ def error_redirect_helper(env : HTTP::Server::Context)
<li>
<a href="/redirect?referer=#{env.get("current_page")}">#{switch_instance}</a>
</li>
END_HTML

if env.get("preferences").as(Preferences).include_youtube_links
html += <<-END_HTML
<li>
<a rel="noreferrer noopener" href="https://youtube.com#{env.request.resource}">#{go_to_youtube}</a>
</li>
END_HTML
end

html += <<-END_HTML
</ul>
END_HTML

return html
else
return ""
end
Expand Down
9 changes: 6 additions & 3 deletions src/invidious/routes/api/v1/channels.cr
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ module Invidious::Routes::API::V1::Channels

def self.community(env)
locale = env.get("preferences").as(Preferences).locale
include_youtube_links = env.get("preferences").as(Preferences).include_youtube_links

env.response.content_type = "application/json"

Expand All @@ -385,14 +386,15 @@ module Invidious::Routes::API::V1::Channels
# sort_by = env.params.query["sort_by"]?.try &.downcase

begin
fetch_channel_community(ucid, continuation, locale, format, thin_mode)
fetch_channel_community(ucid, continuation, locale, format, thin_mode, include_youtube_links)
rescue ex
return error_json(500, ex)
end
end

def self.post(env)
locale = env.get("preferences").as(Preferences).locale
include_youtube_links = env.get("preferences").as(Preferences).include_youtube_links

env.response.content_type = "application/json"
id = env.params.url["id"].to_s
Expand All @@ -413,14 +415,15 @@ module Invidious::Routes::API::V1::Channels
end

begin
fetch_channel_community_post(ucid, id, locale, format, thin_mode)
fetch_channel_community_post(ucid, id, locale, format, thin_mode, include_youtube_links)
rescue ex
return error_json(500, ex)
end
end

def self.post_comments(env)
locale = env.get("preferences").as(Preferences).locale
include_youtube_links = env.get("preferences").as(Preferences).include_youtube_links

env.response.content_type = "application/json"

Expand All @@ -441,7 +444,7 @@ module Invidious::Routes::API::V1::Channels
else
comments = YoutubeAPI.browse(continuation: continuation)
end
return Comments.parse_youtube(id, comments, format, locale, thin_mode, is_post: true)
return Comments.parse_youtube(id, comments, format, locale, thin_mode, include_youtube_links, is_post: true)
end

def self.channels(env)
Expand Down
3 changes: 2 additions & 1 deletion src/invidious/routes/api/v1/videos.cr
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ module Invidious::Routes::API::V1::Videos

def self.comments(env)
locale = env.get("preferences").as(Preferences).locale
include_youtube_links = env.get("preferences").as(Preferences).include_youtube_links
region = env.params.query["region"]?

env.response.content_type = "application/json"
Expand All @@ -345,7 +346,7 @@ module Invidious::Routes::API::V1::Videos
sort_by ||= "top"

begin
comments = Comments.fetch_youtube(id, continuation, format, locale, thin_mode, region, sort_by: sort_by)
comments = Comments.fetch_youtube(id, continuation, format, locale, thin_mode, region, include_youtube_links, sort_by: sort_by)
rescue ex : NotFoundException
return error_json(404, ex)
rescue ex
Expand Down
11 changes: 6 additions & 5 deletions src/invidious/routes/channels.cr
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ module Invidious::Routes::Channels
thin_mode = env.params.query["thin_mode"]? || env.get("preferences").as(Preferences).thin_mode
thin_mode = thin_mode == "true"

include_youtube_links = env.get("preferences").as(Preferences).include_youtube_links

continuation = env.params.query["continuation"]?

if !channel.tabs.includes? "community"
Expand All @@ -223,7 +225,7 @@ module Invidious::Routes::Channels
sort_options = [] of String

begin
items = JSON.parse(fetch_channel_community(ucid, continuation, locale, "json", thin_mode))
items = JSON.parse(fetch_channel_community(ucid, continuation, locale, "json", thin_mode, include_youtube_links))
rescue ex : InfoException
env.response.status_code = 500
error_message = ex.message
Expand All @@ -245,6 +247,7 @@ module Invidious::Routes::Channels
prefs = env.get("preferences").as(Preferences)

locale = prefs.locale
include_youtube_links = prefs.include_youtube_links

thin_mode = env.params.query["thin_mode"]? || prefs.thin_mode
thin_mode = thin_mode == "true"
Expand All @@ -256,21 +259,19 @@ module Invidious::Routes::Channels

if !ucid.nil?
ucid = ucid.to_s
post_response = fetch_channel_community_post(ucid, id, locale, "json", thin_mode)
else
# resolve the url to get the author's UCID
response = YoutubeAPI.resolve_url("https://www.youtube.com/post/#{id}")
return error_template(400, "Invalid post ID") if response["error"]?

ucid = response.dig("endpoint", "browseEndpoint", "browseId").as_s
post_response = fetch_channel_community_post(ucid, id, locale, "json", thin_mode)
end

post_response = JSON.parse(post_response)
post_response = JSON.parse(fetch_channel_community_post(ucid, id, locale, "json", thin_mode, include_youtube_links))

if nojs
comments = Comments.fetch_community_post_comments(ucid, id)
comment_html = JSON.parse(Comments.parse_youtube(id, comments, "html", locale, thin_mode, is_post: true))["contentHtml"]
comment_html = JSON.parse(Comments.parse_youtube(id, comments, "html", locale, thin_mode, include_youtube_links, is_post: true))["contentHtml"]
end
templated "post"
end
Expand Down
5 changes: 5 additions & 0 deletions src/invidious/routes/preferences.cr
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ module Invidious::Routes::PreferencesRoute
automatic_instance_redirect ||= "off"
automatic_instance_redirect = automatic_instance_redirect == "on"

include_youtube_links = env.params.body["include_youtube_links"]?.try &.as(String)
include_youtube_links ||= "off"
include_youtube_links = include_youtube_links == "on"

region = env.params.body["region"]?.try &.as(String)

locale = env.params.body["locale"]?.try &.as(String)
Expand Down Expand Up @@ -168,6 +172,7 @@ module Invidious::Routes::PreferencesRoute
default_home: default_home,
feed_menu: feed_menu,
automatic_instance_redirect: automatic_instance_redirect,
include_youtube_links: include_youtube_links,
region: region,
related_videos: related_videos,
sort: sort,
Expand Down
Loading