nusqasun / VKVideo - better life

// ==UserScript==
// @name         VKVideo - better life
// @namespace    http://tampermonkey.net/
// @copyright    2025, nusqasun
// @license MIT
// @version      2025-03-04
// @description  vkvideo.ru - sound normalize, save playback speed in cookie
// @author       You
// @match        https://vkvideo.ru/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=vkvideo.ru
// @updateURL https://openuserjs.org/meta/nusqasun/VKVideo_-_better_life.meta.js
// @downloadURL https://openuserjs.org/install/nusqasun/VKVideo_-_better_life.user.js

// ==/UserScript==

(function() {
    'use strict';
    var playAfterLoad=0;
    const interval = setInterval(function() {
        var video = document.querySelector('video');
        if(playAfterLoad==0){
            var savedSpeed=getCookie("playspeed");
            console.log(video);
            if(video!==undefined){
                if(parseInt(savedSpeed)>0) video.playbackRate = savedSpeed;
                else{video.playbackRate = 1.5;}
                playAfterLoad=1;

                const audioCtx = new AudioContext();
                const source = audioCtx.createMediaElementSource(video);

                const compressor = audioCtx.createDynamicsCompressor();
                compressor.threshold.setValueAtTime(-50, audioCtx.currentTime);
                compressor.knee.setValueAtTime(40, audioCtx.currentTime);
                compressor.ratio.setValueAtTime(12, audioCtx.currentTime);
                compressor.attack.setValueAtTime(0, audioCtx.currentTime);
                compressor.release.setValueAtTime(0.25, audioCtx.currentTime);

                source.connect(compressor).connect(audioCtx.destination);
            }
        }
    }, 2000);
    function KeyPress(e) {
		var evtobj = window.event? event : e
		if (evtobj.keyCode == 190 && evtobj.shiftKey){
				videoSetSpeed(0.25)
		}
		if (evtobj.keyCode == 188 && evtobj.shiftKey){
				videoSetSpeed(-0.25);
		}
	}
	function videoSetSpeed(e){
        var video = document.querySelector('video');
        var playspeed=video.playbackRate;
        playspeed+=e;
        if(playspeed<0)playspeed=0;
        else if(playspeed>3)playspeed=3;
        video.playbackRate = playspeed;
        setCookie("playspeed", playspeed, 999);
	}
	document.onkeydown = KeyPress;

    function setCookie(cname, cvalue, exdays) {
        const d = new Date();
        d.setTime(d.getTime() + (exdays*24*60*60*1000));
        let expires = "expires="+ d.toUTCString();
        document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
    }
    function getCookie(cname) {
        let name = cname + "=";
        let ca = document.cookie.split(';');
        for(let i = 0; i < ca.length; i++) {
            let c = ca[i];
            while (c.charAt(0) == ' ') {
                c = c.substring(1);
            }
            if (c.indexOf(name) == 0) {
                return c.substring(name.length, c.length);
            }
        }
        return "";
    }
})();