Add continuous playback

This commit is contained in:
Omar Roth 2018-11-11 11:45:05 -06:00
parent 1465cefa17
commit 4b76b93610
5 changed files with 69 additions and 1 deletions

View File

@ -965,6 +965,10 @@ post "/preferences" do |env|
autoplay ||= "off" autoplay ||= "off"
autoplay = autoplay == "on" autoplay = autoplay == "on"
continue = env.params.body["continue"]?.try &.as(String)
continue ||= "off"
continue = continue == "on"
listen = env.params.body["listen"]?.try &.as(String) listen = env.params.body["listen"]?.try &.as(String)
listen ||= "off" listen ||= "off"
listen = listen == "on" listen = listen == "on"
@ -1024,6 +1028,7 @@ post "/preferences" do |env|
preferences = { preferences = {
"video_loop" => video_loop, "video_loop" => video_loop,
"autoplay" => autoplay, "autoplay" => autoplay,
"continue" => continue,
"listen" => listen, "listen" => listen,
"speed" => speed, "speed" => speed,
"quality" => quality, "quality" => quality,

View File

@ -70,6 +70,10 @@ class Preferences
JSON.mapping({ JSON.mapping({
video_loop: Bool, video_loop: Bool,
autoplay: Bool, autoplay: Bool,
continue: {
type: Bool,
default: false,
},
listen: { listen: {
type: Bool, type: Bool,
default: false, default: false,

View File

@ -730,6 +730,7 @@ end
def process_video_params(query, preferences) def process_video_params(query, preferences)
autoplay = query["autoplay"]?.try &.to_i? autoplay = query["autoplay"]?.try &.to_i?
continue = query["continue"]?.try &.to_i?
listen = query["listen"]? && (query["listen"] == "true" || query["listen"] == "1").to_unsafe listen = query["listen"]? && (query["listen"] == "true" || query["listen"] == "1").to_unsafe
preferred_captions = query["subtitles"]?.try &.split(",").map { |a| a.downcase } preferred_captions = query["subtitles"]?.try &.split(",").map { |a| a.downcase }
quality = query["quality"]? quality = query["quality"]?
@ -739,6 +740,7 @@ def process_video_params(query, preferences)
if preferences if preferences
autoplay ||= preferences.autoplay.to_unsafe autoplay ||= preferences.autoplay.to_unsafe
continue ||= preferences.continue.to_unsafe
listen ||= preferences.listen.to_unsafe listen ||= preferences.listen.to_unsafe
preferred_captions ||= preferences.captions preferred_captions ||= preferences.captions
quality ||= preferences.quality quality ||= preferences.quality
@ -748,6 +750,7 @@ def process_video_params(query, preferences)
end end
autoplay ||= 0 autoplay ||= 0
continue ||= 0
listen ||= 0 listen ||= 0
preferred_captions ||= [] of String preferred_captions ||= [] of String
quality ||= "hd720" quality ||= "hd720"
@ -756,6 +759,7 @@ def process_video_params(query, preferences)
volume ||= 100 volume ||= 100
autoplay = autoplay == 1 autoplay = autoplay == 1
continue = continue == 1
listen = listen == 1 listen = listen == 1
video_loop = video_loop == 1 video_loop = video_loop == 1
@ -786,6 +790,7 @@ def process_video_params(query, preferences)
params = { params = {
autoplay: autoplay, autoplay: autoplay,
continue: continue,
controls: controls, controls: controls,
listen: listen, listen: listen,
preferred_captions: preferred_captions, preferred_captions: preferred_captions,

View File

@ -23,6 +23,11 @@ function update_value(element) {
<input name="autoplay" id="autoplay" type="checkbox" <% if user.preferences.autoplay %>checked<% end %>> <input name="autoplay" id="autoplay" type="checkbox" <% if user.preferences.autoplay %>checked<% end %>>
</div> </div>
<div class="pure-control-group">
<label for="continue">Automatically play next video: </label>
<input name="continue" id="continue" type="checkbox" <% if user.preferences.continue %>checked<% end %>>
</div>
<div class="pure-control-group"> <div class="pure-control-group">
<label for="listen">Listen by default: </label> <label for="listen">Listen by default: </label>
<input name="listen" id="listen" type="checkbox" <% if user.preferences.listen %>checked<% end %>> <input name="listen" id="listen" type="checkbox" <% if user.preferences.listen %>checked<% end %>>

View File

@ -140,6 +140,15 @@
<% if !preferences || preferences && preferences.related_videos %> <% if !preferences || preferences && preferences.related_videos %>
<div class="h-box"> <div class="h-box">
<% if !plid && !rvs.empty? %>
<div class="pure-control-group">
<label for="continue">Autoplay next video: </label>
<input name="continue" onclick="continue_autoplay(this)" id="continue" type="checkbox" <% if params[:continue] %>checked<% end %>>
</div>
<hr>
<% end %>
<% rvs.each do |rv| %> <% rvs.each do |rv| %>
<% if rv.has_key?("id") %> <% if rv.has_key?("id") %>
<a href="/watch?v=<%= rv["id"] %>"> <a href="/watch?v=<%= rv["id"] %>">
@ -163,6 +172,46 @@
</div> </div>
<script> <script>
<% if !rvs.empty? && !plid && params[:continue] %>
player.on('ended', function() {
window.location.replace("/watch?v="
+ "<%= rvs[0]?.try &.["id"] %>"
+ "&continue=1"
<% if params[:listen] %>
+ "&listen=1"
<% end %>
<% if params[:autoplay] %>
+ "&autoplay=1"
<% end %>
<% if params[:speed] %>
+ "&speed=<%= params[:speed] %>"
<% end %>
);
});
<% end %>
function continue_autoplay(target) {
if (target.checked) {
player.on('ended', function() {
window.location.replace("/watch?v="
+ "<%= rvs[0]?.try &.["id"] %>"
+ "&continue=1"
<% if params[:listen] %>
+ "&listen=1"
<% end %>
<% if params[:autoplay] %>
+ "&autoplay=1"
<% end %>
<% if params[:speed] %>
+ "&speed=<%= params[:speed] %>"
<% end %>
);
});
} else {
player.off('ended');
}
}
function number_with_separator(val) { function number_with_separator(val) {
while (/(\d+)(\d{3})/.test(val.toString())) { while (/(\d+)(\d{3})/.test(val.toString())) {
val = val.toString().replace(/(\d+)(\d{3})/, "$1" + "," + "$2"); val = val.toString().replace(/(\d+)(\d{3})/, "$1" + "," + "$2");