|
| 1 | +'use strict'; |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +const frameJs = (volume) => `(function (volume) { |
| 6 | + if (window.__slickQuietSpotify) { |
| 7 | + window.__slickQuietSpotify.volume = volume; |
| 8 | + return; |
| 9 | + } |
| 10 | + const state = (window.__slickQuietSpotify = { volume }); |
| 11 | + const og = Audio.prototype.play; |
| 12 | + Audio.prototype.play = function () { |
| 13 | + this.volume = state.volume; |
| 14 | + return og.apply(this, arguments); |
| 15 | + }; |
| 16 | +})(${volume});`; |
| 17 | + |
| 18 | +module.exports = { |
| 19 | + meta: { |
| 20 | + name: 'QuietSpotify', |
| 21 | + description: 'Customize the volume of Spotify embeds so they are not stupidly loud', |
| 22 | + version: '1.0.0', |
| 23 | + }, |
| 24 | + |
| 25 | + settings: { |
| 26 | + volume: { |
| 27 | + type: 'number', |
| 28 | + label: 'Volume (%)', |
| 29 | + description: '0-100. Anything above 10% is very loud.', |
| 30 | + default: 10, |
| 31 | + }, |
| 32 | + }, |
| 33 | + |
| 34 | + main(ctx) { |
| 35 | + const settingsFile = path.join(ctx.app.getPath('userData'), 'slick', 'plugin-settings.json'); |
| 36 | + |
| 37 | + const volume = () => { |
| 38 | + let v = ctx.settings.volume; |
| 39 | + try { |
| 40 | + const stored = JSON.parse(fs.readFileSync(settingsFile, 'utf8'))[ctx.name]; |
| 41 | + if (stored && stored.volume !== undefined) v = Number(stored.volume); |
| 42 | + } catch (e) {} |
| 43 | + if (!Number.isFinite(v)) v = 10; |
| 44 | + return Math.min(Math.max(v, 0), 100) / 100; |
| 45 | + }; |
| 46 | + |
| 47 | + ctx.onWindow((win) => { |
| 48 | + win.webContents.on('frame-created', (_event, { frame }) => { |
| 49 | + if (!frame) return; |
| 50 | + frame.on('dom-ready', () => { |
| 51 | + if (!frame.url.startsWith('https://open.spotify.com/embed/')) return; |
| 52 | + frame.executeJavaScript(frameJs(volume())).catch((e) => ctx.log('inject failed:', e.message)); |
| 53 | + }); |
| 54 | + }); |
| 55 | + }); |
| 56 | + }, |
| 57 | +}; |
0 commit comments