Skip to content

Commit

Permalink
Merge pull request #127 from th-ch/fix-plugins-context-isolation
Browse files Browse the repository at this point in the history
Fix plugins with context isolation
  • Loading branch information
th-ch authored Jan 13, 2021
2 parents b54c501 + 39c8031 commit 074840e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ function createMainWindow() {
backgroundColor: "#000",
show: false,
webPreferences: {
contextIsolation: true,
// TODO: re-enable contextIsolation once it can work with ffmepg.wasm
// Possible bundling? https://github.com/ffmpegwasm/ffmpeg.wasm/issues/126
contextIsolation: false,
preload: path.join(__dirname, "preload.js"),
nodeIntegrationInSubFrames: true,
nativeWindowOpen: true, // window.open return Window object(like in regular browsers), not BrowserWindowProxy
Expand Down
6 changes: 6 additions & 0 deletions plugins/downloader/front.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { contextBridge } = require("electron");

const { ElementFromFile, templatePath, triggerAction } = require("../utils");
const { ACTIONS, CHANNEL } = require("./actions.js");
const { downloadVideoToMP3 } = require("./youtube-dl");
Expand Down Expand Up @@ -28,6 +30,9 @@ const reinit = () => {
}
};

// TODO: re-enable once contextIsolation is set to true
// contextBridge.exposeInMainWorld("downloader", {
// download: () => {
global.download = () => {
const videoUrl = window.location.href;

Expand All @@ -48,6 +53,7 @@ global.download = () => {
pluginOptions
);
};
// });

function observeMenu(options) {
pluginOptions = { ...pluginOptions, ...options };
Expand Down
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
7 changes: 5 additions & 2 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,7 +10,10 @@ 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 || {};
const actions = require(pluginPath).actions || {};

// TODO: re-enable once contextIsolation is set to true
// contextBridge.exposeInMainWorld(plugin + "Actions", actions);
Object.keys(actions).forEach((actionName) => {
global[actionName] = actions[actionName];
});
Expand Down

0 comments on commit 074840e

Please sign in to comment.