Skip to content

Commit

Permalink
Use contextBridge in preload script + update navigation plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
th-ch committed Jan 12, 2021
1 parent 79e8fc2 commit 9ad1dad
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
24 changes: 12 additions & 12 deletions plugins/navigation/actions.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
const { triggerAction } = require('../utils');
const { triggerAction } = require("../utils");

const CHANNEL = "navigation";
const ACTIONS = {
NEXT: "next",
BACK: 'back',
}
NEXT: "next",
BACK: "back",
};

function goToNextPage() {
triggerAction(CHANNEL, ACTIONS.NEXT);
triggerAction(CHANNEL, ACTIONS.NEXT);
}

function goToPreviousPage() {
triggerAction(CHANNEL, ACTIONS.BACK);
triggerAction(CHANNEL, ACTIONS.BACK);
}

module.exports = {
CHANNEL: CHANNEL,
ACTIONS: ACTIONS,
global: {
goToNextPage: goToNextPage,
goToPreviousPage: goToPreviousPage,
}
CHANNEL: CHANNEL,
ACTIONS: ACTIONS,
actions: {
goToNextPage: goToNextPage,
goToPreviousPage: goToPreviousPage,
},
};
8 changes: 4 additions & 4 deletions plugins/navigation/back.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
const path = require("path");

const { injectCSS, listenAction } = require("../utils");
const { ACTIONS, CHANNEL } = require("./actions.js");
const { ACTIONS, CHANNEL } = require("./actions.js");

function handle(win) {
injectCSS(win.webContents, path.join(__dirname, "style.css"));
listenAction(CHANNEL, (event, action) => {
switch (action) {
case ACTIONS.NEXT:
case ACTIONS.NEXT:
if (win.webContents.canGoForward()) {
win.webContents.goForward();
}
break;
case ACTIONS.BACK:
case ACTIONS.BACK:
if (win.webContents.canGoBack()) {
win.webContents.goBack();
}
break;
default:
default:
console.log("Unknown action: " + action);
}
});
Expand Down
2 changes: 1 addition & 1 deletion plugins/navigation/templates/back.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
class="style-scope ytmusic-pivot-bar-renderer navigation-item"
tab-id="FEmusic_back"
role="tab"
onclick="goToPreviousPage()"
onclick="navigationActions.goToPreviousPage()"
>
<div
class="search-icon style-scope ytmusic-search-box"
Expand Down
2 changes: 1 addition & 1 deletion plugins/navigation/templates/forward.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
class="style-scope ytmusic-pivot-bar-renderer navigation-item"
tab-id="FEmusic_next"
role="tab"
onclick="goToNextPage()"
onclick="navigationActions.goToNextPage()"
>
<div
class="search-icon style-scope ytmusic-search-box"
Expand Down
8 changes: 3 additions & 5 deletions preload.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require("path");

const { remote } = require("electron");
const { contextBridge, remote } = require("electron");

const config = require("./config");
const { fileExists } = require("./plugins/utils");
Expand All @@ -10,10 +10,8 @@ const plugins = config.plugins.getEnabled();
plugins.forEach(([plugin, options]) => {
const pluginPath = path.join(__dirname, "plugins", plugin, "actions.js");
fileExists(pluginPath, () => {
const actions = require(pluginPath).global || {};
Object.keys(actions).forEach((actionName) => {
global[actionName] = actions[actionName];
});
const actions = require(pluginPath).actions || {};
contextBridge.exposeInMainWorld(plugin + "Actions", actions);
});
});

Expand Down

0 comments on commit 9ad1dad

Please sign in to comment.