feat: Playtube Stream Dock Plugin 1.0.0 (Ajazz AKP153E) mit Icon-Pack

This commit is contained in:
2026-09-26 21:00:36 +02:00
commit 179a785adb
221 changed files with 10254 additions and 0 deletions
+220
View File
@@ -0,0 +1,220 @@
'use strict';
/**
* Alle Tastenaktionen des Plugins: was ein Tastendruck an Playtube schickt (request) und wie die
* Taste dazu aussieht (render). Reine Funktionen ohne Netzwerk - dadurch gut testbar.
*
* Zustaende (State) entsprechen den Bildern im manifest.json, Reihenfolge = Index.
*/
const PREFIX = 'com.fojadrachi.playtube.';
const DEFAULTS = Object.freeze({
target: 'auto', // "auto" | "youtube" | "music"
step: 5, // Lautstaerke-Schritt in Prozent
volume: 30, // Ziel-Lautstaerke der Aktion "Lautstaerke setzen"
seconds: 10, // Sprungweite beim Vor-/Zurueckspulen
tab: 'toggle', // Aktion "Tab wechseln": "toggle" | "youtube" | "music"
playlistId: '',
playlistTitle: '',
});
const TARGETS = ['auto', 'youtube', 'music'];
const TABS = ['toggle', 'youtube', 'music'];
function clampInt(value, min, max, fallback) {
const number = Number.parseInt(value, 10);
if (!Number.isFinite(number)) return fallback;
return Math.max(min, Math.min(max, number));
}
/** Macht aus den (evtl. leeren/fehlerhaften) Einstellungen einer Taste gueltige Werte. */
function normalizeSettings(raw, titleDefault = false) {
const settings = { ...DEFAULTS, ...(raw || {}) };
if (!TARGETS.includes(settings.target)) settings.target = DEFAULTS.target;
if (!TABS.includes(settings.tab)) settings.tab = DEFAULTS.tab;
settings.step = clampInt(settings.step, 1, 25, DEFAULTS.step);
settings.volume = clampInt(settings.volume, 0, 100, DEFAULTS.volume);
settings.seconds = clampInt(settings.seconds, 1, 300, DEFAULTS.seconds);
const show = raw && raw.showTitle !== undefined ? raw.showTitle : titleDefault;
settings.showTitle = show === true || show === 'true';
settings.playlistId = typeof settings.playlistId === 'string' ? settings.playlistId.trim() : '';
settings.playlistTitle = typeof settings.playlistTitle === 'string' ? settings.playlistTitle : '';
return settings;
}
/** Welcher Tab ist gemeint? "auto" = der Tab, den Playtube als aktiv meldet. */
function pickTab(state, target) {
if (!state || !state.tabs) return null;
const name = target === 'auto' ? state.activeTab : target;
const info = state.tabs[name];
return info ? { name, info } : null;
}
/** Bricht Text fuer die kleinen Tasten-Displays um (Woerter, dann harte Trennung). */
function wrapTitle(text, perLine = 8, maxLines = 3) {
const words = String(text || '').trim().split(/\s+/).filter(Boolean);
const lines = [];
let current = '';
let truncated = false;
const push = (line) => {
if (lines.length < maxLines) lines.push(line);
else truncated = true;
};
for (let word of words) {
while (word.length > perLine) {
if (current) {
push(current);
current = '';
}
push(word.slice(0, perLine));
word = word.slice(perLine);
}
if (!word) continue;
if (!current) current = word;
else if (current.length + 1 + word.length <= perLine) current += ` ${word}`;
else {
push(current);
current = word;
}
}
if (current) push(current);
if (truncated && lines.length) lines[lines.length - 1] = `${lines[lines.length - 1].slice(0, perLine - 2)}..`;
return lines.join('\n');
}
function volumeTitle(tab) {
const volume = tab && tab.info.volume;
return typeof volume === 'number' ? `${Math.round(volume)}%` : '';
}
const OFFLINE_TITLE = 'Playtube\noffline';
/**
* Definition pro Aktion. `id` = letzter Teil der UUID (siehe manifest.json).
* - request(settings): {cmd, fields} oder null (Taste tut dann nichts, zeigt Alarm)
* - render(settings, state, connected): {state?, title?, coverUrl?}
* - press: "up" (Standard, beim Loslassen) oder "down" (sofort; mit repeat = Dauerdruck)
*/
const DEFINITIONS = {
playpause: {
titleDefault: false,
request: (s) => ({ cmd: 'play_pause', fields: { target: s.target } }),
render: (s, state, connected) => {
if (!connected) return { state: 0, title: s.showTitle ? OFFLINE_TITLE : '' };
const tab = pickTab(state, s.target);
return {
state: tab && tab.info.playing ? 1 : 0,
title: s.showTitle && tab && tab.info.title ? wrapTitle(tab.info.title) : '',
};
},
},
next: { request: (s) => ({ cmd: 'next', fields: { target: s.target } }) },
previous: { request: (s) => ({ cmd: 'previous', fields: { target: s.target } }) },
seekback: {
request: (s) => ({ cmd: 'seek', fields: { target: s.target, value: -s.seconds } }),
render: (s) => ({ title: `-${s.seconds}s` }),
},
seekforward: {
request: (s) => ({ cmd: 'seek', fields: { target: s.target, value: s.seconds } }),
render: (s) => ({ title: `+${s.seconds}s` }),
},
volumeup: {
press: 'down',
repeat: { delayMs: 450, intervalMs: 140 },
request: (s) => ({ cmd: 'volume_change', fields: { target: s.target, value: s.step } }),
render: (s, state, connected) => ({ title: connected ? volumeTitle(pickTab(state, s.target)) : '' }),
},
volumedown: {
press: 'down',
repeat: { delayMs: 450, intervalMs: 140 },
request: (s) => ({ cmd: 'volume_change', fields: { target: s.target, value: -s.step } }),
render: (s, state, connected) => ({ title: connected ? volumeTitle(pickTab(state, s.target)) : '' }),
},
volumeset: {
request: (s) => ({ cmd: 'set_volume', fields: { target: s.target, value: s.volume } }),
render: (s) => ({ title: `${s.volume}%` }),
},
mute: {
request: (s) => ({ cmd: 'mute_toggle', fields: { target: s.target } }),
render: (s, state, connected) => {
const tab = connected ? pickTab(state, s.target) : null;
return { state: tab && tab.info.muted ? 1 : 0, title: tab && !tab.info.muted ? volumeTitle(tab) : '' };
},
},
nowplaying: {
titleDefault: true,
request: (s) => ({ cmd: 'play_pause', fields: { target: s.target } }),
render: (s, state, connected) => {
if (!connected) return { coverUrl: null, title: s.showTitle ? OFFLINE_TITLE : '' };
const tab = pickTab(state, s.target);
const info = tab && tab.info;
return {
coverUrl: info && info.hasVideo && info.thumbnail ? info.thumbnail : null,
title: s.showTitle && info && info.title ? wrapTitle(info.title) : '',
};
},
},
like: {
request: (s) => ({ cmd: 'like', fields: { target: s.target } }),
render: (s, state, connected) => {
const tab = connected ? pickTab(state, s.target) : null;
return { state: tab && tab.info.likeStatus === 'like' ? 1 : 0 };
},
},
shuffle: { request: (s) => ({ cmd: 'shuffle', fields: { target: s.target } }) },
repeat: {
request: (s) => ({ cmd: 'repeat', fields: { target: s.target } }),
render: (s, state, connected) => {
const tab = connected ? pickTab(state, s.target) : null;
const mode = String((tab && tab.info.repeatMode) || '').toUpperCase();
return { state: mode === 'ALL' ? 1 : mode === 'ONE' ? 2 : 0 };
},
},
playlist: {
request: (s) => (s.playlistId ? { cmd: 'play_playlist', fields: { playlist: s.playlistId } } : null),
render: (s) => ({ title: s.playlistTitle ? wrapTitle(s.playlistTitle) : '' }),
},
switchtab: {
request: (s) => ({ cmd: 'switch_tab', fields: { target: s.tab === 'toggle' ? 'auto' : s.tab } }),
render: (s, state, connected) => {
if (!connected || !state) return { title: '' };
if (state.visibleTab === 'music') return { title: 'Music' };
if (state.visibleTab === 'youtube') return { title: 'YouTube' };
return { title: '' };
},
},
show: { request: () => ({ cmd: 'show', fields: {} }) },
};
function actionIdFromUuid(uuid) {
if (typeof uuid !== 'string' || !uuid.startsWith(PREFIX)) return null;
const id = uuid.slice(PREFIX.length);
return Object.prototype.hasOwnProperty.call(DEFINITIONS, id) ? id : null;
}
/** Darstellung einer Taste; fehlende Felder heissen "nicht aendern". */
function renderAction(id, rawSettings, state, connected) {
const definition = DEFINITIONS[id];
const settings = normalizeSettings(rawSettings, definition.titleDefault);
return definition.render ? definition.render(settings, state, connected) : {};
}
/** {cmd, fields} fuer einen Tastendruck oder null. */
function buildRequest(id, rawSettings) {
const definition = DEFINITIONS[id];
const settings = normalizeSettings(rawSettings, definition.titleDefault);
return definition.request(settings);
}
module.exports = {
PREFIX,
DEFAULTS,
DEFINITIONS,
normalizeSettings,
pickTab,
wrapTitle,
actionIdFromUuid,
renderAction,
buildRequest,
};