Move store of modification in Cookie instead of localStorage

This commit is contained in:
Féry Mathieu (Mathius) 2022-02-12 22:07:41 +01:00
parent 17ae2648ed
commit 7048193f00
No known key found for this signature in database
GPG Key ID: F9CCC80C18A59037
4 changed files with 48 additions and 15 deletions

View File

@ -176,17 +176,55 @@ if (video_data.params.video_start > 0 || video_data.params.video_end > 0) {
player.currentTime(video_data.params.video_start);
}
/*
If the video settings are default, we enable the management of the settings by
the videojs-persist module otherwise we apply the preferences.
*/
if (video_data.params.volume == 100 && video_data.params.speed == "1.0")
player.persist();
else {
player.volume(video_data.params.volume / 100);
player.playbackRate(video_data.params.speed);
/**
* Method for get content of Cookie
* @param {String} name Name of cookie
* @returns cookieValue
*/
function getCookieValue(name) {
var value = document.cookie.split(";").filter(item => {
return item.includes(name + "=");
});
return value != null && value.length >= 1 ? value[0].substring((name + "=").length, value[0].length) : null;
}
/**
* Method for update Prefs cookie (Or create if missing)
* @param {number} newVolume New Volume defined (Null if unchanged)
* @param {number} newSpeed New Speed defined (Null if unchanged)
*/
function updateCookie(newVolume, newSpeed) {
var volumeValue = newVolume != null ? newVolume : video_data.params.volume;
var speedValue = newSpeed != null ? newSpeed : video_data.params.speed;
var cookieValue = getCookieValue('PREFS');
if (cookieValue != null) {
var cookieJson = JSON.parse(decodeURIComponent(cookieValue));
cookieJson.volume = volumeValue;
cookieJson.speed = speedValue;
document.cookie = document.cookie.replace(getCookieValue('PREFS'), encodeURIComponent(JSON.stringify(cookieJson)));
} else {
var date = new Date();
//Set expiration in 2 year
date.setTime(date.getTime() + 63115200);
document.cookie = 'PREFS=' +
encodeURIComponent(JSON.stringify({ 'volume': volumeValue, 'speed': speedValue })) +
'; expires=' + date.toGMTString() + '; SameSite=Strict; path=/';
}
video_data.params.volume = volumeValue;
video_data.params.speed = speedValue;
}
player.on('ratechange', function () {
updateCookie(null, player.playbackRate());
});
player.on('volumechange', function () {
updateCookie(Math.ceil(player.volume() * 100), null);
});
player.on('waiting', function () {
if (player.playbackRate() > 1 && player.liveTracker.isLive() && player.liveTracker.atLiveEdge()) {
console.log('Player has caught up to source, resetting playbackRate.')

View File

@ -30,7 +30,7 @@ struct Invidious::User
value: URI.encode_www_form(preferences.to_json),
expires: Time.utc + 2.years,
secure: SECURE,
http_only: true
http_only: false
)
end
end

View File

@ -11,7 +11,6 @@
<script src="/videojs/videojs-contrib-quality-levels/videojs-contrib-quality-levels.js?v=<%= ASSET_COMMIT %>"></script>
<script src="/videojs/videojs-http-source-selector/videojs-http-source-selector.js?v=<%= ASSET_COMMIT %>"></script>
<script src="/videojs/videojs-markers/videojs-markers.js?v=<%= ASSET_COMMIT %>"></script>
<script src="/videojs/videojs-persist/videojs-persist.js?v=<%= ASSET_COMMIT %>"></script>
<script src="/videojs/videojs-share/videojs-share.js?v=<%= ASSET_COMMIT %>"></script>
<script src="/videojs/videojs-vtt-thumbnails/videojs-vtt-thumbnails.js?v=<%= ASSET_COMMIT %>"></script>

View File

@ -25,10 +25,6 @@ videojs-overlay:
version: 2.1.4
shasum: 5a103b25374dbb753eb87960d8360c2e8f39cc05
videojs-persist:
version: 0.1.2
shasum: 44da05aced1fbf15693a36b7cce3cc4a9960dabe
videojs-share:
version: 3.2.1
shasum: 0a3024b981387b9d21c058c829760a72c14b8ceb