Skip to content

Commit 6ab0105

Browse files
authored
Merge pull request #79 from th-ch/advanced-config
Refactor config, custom plugin options
2 parents 1b54b19 + 4dcbd2e commit 6ab0105

File tree

15 files changed

+282
-165
lines changed

15 files changed

+282
-165
lines changed

config/defaults.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const defaultConfig = {
2+
"window-size": {
3+
width: 1100,
4+
height: 550,
5+
},
6+
url: "https://music.youtube.com",
7+
options: {
8+
tray: false,
9+
appVisible: true,
10+
autoUpdates: true,
11+
hideMenu: false,
12+
startAtLogin: false,
13+
disableHardwareAcceleration: false,
14+
},
15+
plugins: {
16+
// Enabled plugins
17+
navigation: {
18+
enabled: true,
19+
},
20+
shortcuts: {
21+
enabled: true,
22+
},
23+
adblocker: {
24+
enabled: true,
25+
cache: true,
26+
additionalBlockLists: [], // Additional list of filters, e.g "https://raw.githubusercontent.com/uBlockOrigin/uAssets/master/filters/filters.txt"
27+
},
28+
// Disabled plugins
29+
downloader: {
30+
enabled: false,
31+
ffmpegArgs: [], // e.g. ["-b:a", "192k"] for an audio bitrate of 192kb/s
32+
downloadFolder: undefined, // Custom download folder (absolute path)
33+
},
34+
},
35+
};
36+
37+
module.exports = defaultConfig;

config/index.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const plugins = require("./plugins");
2+
const store = require("./store");
3+
4+
const set = (key, value) => {
5+
store.set(key, value);
6+
};
7+
8+
const get = (key) => {
9+
return store.get(key);
10+
};
11+
12+
module.exports = {
13+
get,
14+
set,
15+
edit: () => store.openInEditor(),
16+
watch: (cb) => store.onDidAnyChange(cb),
17+
plugins,
18+
};

config/plugins.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const store = require("./store");
2+
3+
function getEnabled() {
4+
const plugins = store.get("plugins");
5+
const enabledPlugins = Object.entries(plugins).filter(([plugin, options]) =>
6+
isEnabled(plugin)
7+
);
8+
return enabledPlugins;
9+
}
10+
11+
function isEnabled(plugin) {
12+
const pluginConfig = store.get("plugins")[plugin];
13+
return pluginConfig !== undefined && pluginConfig.enabled;
14+
}
15+
16+
function setOptions(plugin, options) {
17+
const plugins = store.get("plugins");
18+
store.set("plugins", {
19+
...plugins,
20+
[plugin]: {
21+
...plugins[plugin],
22+
...options,
23+
},
24+
});
25+
}
26+
27+
function enable(plugin) {
28+
setOptions(plugin, { enabled: true });
29+
}
30+
31+
function disable(plugin) {
32+
setOptions(plugin, { enabled: false });
33+
}
34+
35+
module.exports = {
36+
isEnabled,
37+
getEnabled,
38+
enable,
39+
disable,
40+
setOptions,
41+
};

config/store.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const Store = require("electron-store");
2+
3+
const defaults = require("./defaults");
4+
5+
const migrations = {
6+
">=1.7.0": (store) => {
7+
const enabledPlugins = store.get("plugins");
8+
if (!Array.isArray(enabledPlugins)) {
9+
console.warn("Plugins are not in array format, cannot migrate");
10+
return;
11+
}
12+
13+
// Include custom options
14+
const plugins = {
15+
adblocker: {
16+
enabled: true,
17+
cache: true,
18+
additionalBlockLists: [],
19+
},
20+
downloader: {
21+
enabled: false,
22+
ffmpegArgs: [], // e.g. ["-b:a", "192k"] for an audio bitrate of 192kb/s
23+
downloadFolder: undefined, // Custom download folder (absolute path)
24+
},
25+
};
26+
enabledPlugins.forEach((enabledPlugin) => {
27+
plugins[enabledPlugin] = {
28+
...plugins[enabledPlugin],
29+
enabled: true,
30+
};
31+
});
32+
store.set("plugins", plugins);
33+
},
34+
};
35+
36+
module.exports = new Store({
37+
defaults,
38+
clearInvalidConfig: false,
39+
migrations,
40+
});

index.js

+24-28
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,8 @@ const electron = require("electron");
55
const is = require("electron-is");
66
const { autoUpdater } = require("electron-updater");
77

8+
const config = require("./config");
89
const { setApplicationMenu } = require("./menu");
9-
const {
10-
autoUpdate,
11-
disableHardwareAcceleration,
12-
getEnabledPlugins,
13-
hideMenu,
14-
isAppVisible,
15-
isTrayEnabled,
16-
setOptions,
17-
store,
18-
startAtLogin,
19-
} = require("./store");
2010
const { fileExists, injectCSS } = require("./plugins/utils");
2111
const { isTesting } = require("./utils/testing");
2212
const { setUpTray } = require("./tray");
@@ -28,7 +18,7 @@ app.commandLine.appendSwitch(
2818
"--experimental-wasm-threads --experimental-wasm-bulk-memory"
2919
);
3020
app.allowRendererProcessReuse = true; // https://github.com/electron/electron/issues/18397
31-
if (disableHardwareAcceleration()) {
21+
if (config.get("options.disableHardwareAcceleration")) {
3222
if (is.dev()) {
3323
console.log("Disabling hardware acceleration");
3424
}
@@ -64,19 +54,19 @@ function loadPlugins(win) {
6454
}
6555
});
6656

67-
getEnabledPlugins().forEach((plugin) => {
57+
config.plugins.getEnabled().forEach(([plugin, options]) => {
6858
console.log("Loaded plugin - " + plugin);
6959
const pluginPath = path.join(__dirname, "plugins", plugin, "back.js");
7060
fileExists(pluginPath, () => {
7161
const handle = require(pluginPath);
72-
handle(win);
62+
handle(win, options);
7363
});
7464
});
7565
}
7666

7767
function createMainWindow() {
78-
const windowSize = store.get("window-size");
79-
const windowMaximized = store.get("window-maximized");
68+
const windowSize = config.get("window-size");
69+
const windowMaximized = config.get("window-maximized");
8070

8171
const win = new electron.BrowserWindow({
8272
icon: icon,
@@ -94,31 +84,34 @@ function createMainWindow() {
9484
},
9585
frame: !is.macOS(),
9686
titleBarStyle: is.macOS() ? "hiddenInset" : "default",
97-
autoHideMenuBar: hideMenu(),
87+
autoHideMenuBar: config.get("options.hideMenu"),
9888
});
9989
if (windowMaximized) {
10090
win.maximize();
10191
}
10292

103-
win.webContents.loadURL(store.get("url"));
93+
win.webContents.loadURL(config.get("url"));
10494
win.on("closed", onClosed);
10595

10696
win.on("move", () => {
10797
let position = win.getPosition();
108-
store.set("window-position", { x: position[0], y: position[1] });
98+
config.set("window-position", { x: position[0], y: position[1] });
10999
});
110100

111101
win.on("resize", () => {
112102
const windowSize = win.getSize();
113103

114-
store.set("window-maximized", win.isMaximized());
104+
config.set("window-maximized", win.isMaximized());
115105
if (!win.isMaximized()) {
116-
store.set("window-size", { width: windowSize[0], height: windowSize[1] });
106+
config.set("window-size", {
107+
width: windowSize[0],
108+
height: windowSize[1],
109+
});
117110
}
118111
});
119112

120113
win.once("ready-to-show", () => {
121-
if (isAppVisible()) {
114+
if (config.get("options.appVisible")) {
122115
win.show();
123116
}
124117
});
@@ -143,7 +136,7 @@ app.on("browser-window-created", (event, win) => {
143136
win.webContents.on("did-navigate-in-page", () => {
144137
const url = win.webContents.getURL();
145138
if (url.startsWith("https://music.youtube.com")) {
146-
store.set("url", url);
139+
config.set("url", url);
147140
}
148141
});
149142

@@ -196,14 +189,17 @@ app.on("activate", () => {
196189
app.on("ready", () => {
197190
mainWindow = createMainWindow();
198191
setApplicationMenu(mainWindow);
192+
config.watch(() => {
193+
setApplicationMenu(mainWindow);
194+
});
199195
setUpTray(app, mainWindow);
200196

201197
// Autostart at login
202198
app.setLoginItemSettings({
203-
openAtLogin: startAtLogin(),
199+
openAtLogin: config.get("options.startAtLogin"),
204200
});
205201

206-
if (!is.dev() && autoUpdate()) {
202+
if (!is.dev() && config.get("options.autoUpdates")) {
207203
autoUpdater.checkForUpdatesAndNotify();
208204
autoUpdater.on("update-available", () => {
209205
const downloadLink =
@@ -223,7 +219,7 @@ app.on("ready", () => {
223219
break;
224220
// Disable updates
225221
case 2:
226-
setOptions({ autoUpdates: false });
222+
config.set("options.autoUpdates", false);
227223
break;
228224
default:
229225
break;
@@ -234,7 +230,7 @@ app.on("ready", () => {
234230

235231
// Optimized for Mac OS X
236232
if (is.macOS()) {
237-
if (!isAppVisible()) {
233+
if (!config.get("options.appVisible")) {
238234
app.dock.hide();
239235
}
240236
}
@@ -244,7 +240,7 @@ app.on("ready", () => {
244240
forceQuit = true;
245241
});
246242

247-
if (is.macOS() || isTrayEnabled()) {
243+
if (is.macOS() || config.get("options.tray")) {
248244
mainWindow.on("close", (event) => {
249245
// Hide the window instead of quitting (quit is available in tray options)
250246
if (!forceQuit) {

0 commit comments

Comments
 (0)