player_timejump.js

Pasted Nov 28, 2026, 8:09 AM1 file155 lines

Scan to open on your phone

Point a camera at the QR code - it opens this exact paste, no app needed.

https://codepastes.com/zzgrtz6rc326x9pa
player_timejump.js
/*
 * Lampa plugin: jump to an exact timecode in the built-in player
 *
 * Adds a button to the player's on-screen panel (next to settings / pip /
 * fullscreen). Pressing it opens Lampa's own text input (on-screen keyboard
 * on TV, native keyboard on phone/PC) pre-filled with the current position;
 * entering a time and confirming seeks the video there.
 *
 * Accepted formats: "1:02:03", "02:03", or a plain number of seconds ("125").
 *
 * Built on Lampa's own public player API:
 *   Lampa.PlayerPanel  - the on-screen panel, built once and reused for every
 *                        playback, so the button is inserted only once
 *   Lampa.PlayerVideo  - video() for the element/duration, to(seconds) to seek
 *   Lampa.Input        - the same text-input/keyboard component Settings uses
 *
 * ES5 only, so it works on old Tizen / WebOS TV browsers.
 */
(function () {
    'use strict';

    if (window.player_timejump_plugin) return;
    window.player_timejump_plugin = true;

    var TXT = {
        uk: { tooltip: 'Перейти до часу', title: 'Введіть час (гг:хх:сс)', bad: 'Невірний формат часу', ok: 'Перехід до ' },
        ru: { tooltip: 'Перейти к времени', title: 'Введите время (чч:мм:сс)', bad: 'Неверный формат времени', ok: 'Переход к ' },
        en: { tooltip: 'Jump to time', title: 'Enter time (h:mm:ss)', bad: 'Invalid time format', ok: 'Jumping to ' }
    };

    function lang() {
        var l = 'uk';
        try { l = Lampa.Storage.get('language', 'uk') || 'uk'; } catch (e) {}
        if (l === 'uk') return 'uk';
        if (l === 'ru' || l === 'be') return 'ru';
        return 'en';
    }

    function t(key) { return TXT[lang()][key]; }

    function noty(text) {
        try { Lampa.Noty.show(text); } catch (e) {}
    }

    // "1:02:03" / "02:03" / "125" / "125.5" -> seconds (number), or null if invalid
    function parseTime(str) {
        if (!str) return null;
        str = ('' + str).trim().replace(',', '.');
        if (!str) return null;

        var parts = str.split(':');
        if (parts.length > 3) return null;

        for (var i = 0; i < parts.length; i++) {
            if (!/^\d+(\.\d+)?$/.test(parts[i].trim())) return null;
        }

        var seconds = 0;
        for (i = 0; i < parts.length; i++) {
            seconds = seconds * 60 + parseFloat(parts[i].trim());
        }

        return isNaN(seconds) ? null : seconds;
    }

    function formatTime(seconds) {
        try { return Lampa.Utils.secondsToTime(Math.max(0, Math.floor(seconds || 0))); }
        catch (e) { return '00:00:00'; }
    }

    function doJump() {
        var video = null;
        try { video = Lampa.PlayerVideo.video(); } catch (e) {}

        var current = video ? (video.currentTime || 0) : 0;
        var back = null;
        try { back = Lampa.Controller.enabled().name; } catch (e) {}

        Lampa.Input.edit({
            title: t('title'),
            value: formatTime(current),
            free: true,
            nosave: true
        }, function (value) {
            var seconds = parseTime(value);

            if (seconds === null) {
                noty(t('bad'));
                try { if (back) Lampa.Controller.toggle(back); } catch (e) {}
                return;
            }

            try { Lampa.PlayerVideo.to(seconds); } catch (e) {}
            noty(t('ok') + formatTime(seconds));

            try { if (back) Lampa.Controller.toggle(back); } catch (e) {}
        });
    }

    var ICON = '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" ' +
        'stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="9"></circle>' +
        '<path d="M12 7v5l3.2 2"></path></svg>';

    function injectButton() {
        var panel;
        try { panel = Lampa.PlayerPanel.render(); } catch (e) { return false; }
        if (!panel || !panel.length) return false;
        if (panel.find('.player-panel__timejump').length) return true;

        var box = panel.find('.player-panel__right.player-panel__tv-visible .player-panel__box-buttons').last();
        if (!box.length) box = panel.find('.player-panel__box-buttons').last();
        if (!box.length) return false;

        var btn = $(
            '<div class="player-panel__timejump button selector">' + ICON +
            '<div class="tooltip">' + t('tooltip') + '</div></div>'
        );

        var last_fire = 0;
        var fire = function () {
            var now = Date.now();
            if (now - last_fire < 500) return;
            last_fire = now;
            doJump();
        };

        btn.on('hover:enter', fire);
        btn[0].addEventListener('click', fire);

        var settings = box.find('.player-panel__settings').eq(0);
        if (settings.length) settings.before(btn);
        else box.prepend(btn);

        return true;
    }

    function start() {
        if (injectButton()) return;

        // Panel.init() normally runs during app startup together with Player.init(),
        // but if this plugin loads first, retry briefly until the panel exists.
        var tries = 0;
        var timer = setInterval(function () {
            tries++;
            if (injectButton() || tries > 20) clearInterval(timer);
        }, 500);
    }

    if (window.appready) start();
    else {
        Lampa.Listener.follow('app', function (e) {
            if (e.type === 'ready') start();
        });
    }
})();

5,426 chars · codepastes.com