Skip to content

Commit 36c2e89

Browse files
committed
feat(*): disable web security features so more sites can be embedded
1 parent 533f693 commit 36c2e89

File tree

5 files changed

+32
-19
lines changed

5 files changed

+32
-19
lines changed

main/background.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { app, BrowserWindow, webFrameMain } from 'electron';
22
import { ipcMain as ipc } from 'electron-better-ipc';
33
import serve from 'electron-serve';
44
import { readFile } from 'fs/promises';
5+
import { omitBy, toLower } from 'lodash';
56
import fetch from 'node-fetch';
67
import { join } from 'path';
78
import { createWindow } from './helpers';
@@ -64,6 +65,17 @@ if (isProd) {
6465
icon: join(__dirname, 'images', 'logo', '256x256.png'),
6566
alwaysOnTop: true,
6667
hasShadow: false,
68+
webPreferences: {
69+
webSecurity: false,
70+
},
71+
});
72+
73+
mainWindow.webContents.session.webRequest.onHeadersReceived({ urls: ['*://*/*'] }, (details, callback) => {
74+
const responseHeaders = omitBy({ ...(details?.responseHeaders ?? {}) }, (value, key) =>
75+
['x-frame-options', 'content-security-policy'].includes(toLower(key))
76+
);
77+
78+
callback({ cancel: false, responseHeaders });
6779
});
6880

6981
mainWindow.setVisibleOnAllWorkspaces(true);
@@ -111,6 +123,17 @@ if (isProd) {
111123
}
112124
);
113125

126+
mainWindow.webContents.on('did-navigate-in-page', async (event, url, isMainFrame, frameProcessId, frameRoutingId) => {
127+
if (isMainFrame) return;
128+
if (url === 'about:blank') return;
129+
130+
const frame = webFrameMain.fromId(frameProcessId, frameRoutingId);
131+
132+
if (frame) {
133+
frame.executeJavaScript(`parent.postMessage({key: 'location', msg: location.href}, "*")`);
134+
}
135+
});
136+
114137
if (isProd) {
115138
await mainWindow.loadURL('app://./index.html');
116139

@@ -140,5 +163,10 @@ ipc.answerRenderer('please-quit', () => app.quit());
140163
app.on('window-all-closed', () => app.quit());
141164

142165
function getFlotEmbed() {
143-
return (mainWindow?.webContents.mainFrame.frames ?? []).find((f) => f.name === 'flot-embed');
166+
const frames = mainWindow?.webContents.mainFrame.frames ?? [];
167+
168+
// Some websites seem to cause the embed frame to be renamed...not sure how, it's
169+
// supposed to be a readonly property, but ah well, if this happens, just try the
170+
// using the first frame
171+
return frames.find((f) => f.name === 'flot-embed') ?? frames[0];
144172
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"private": true,
33
"name": "Flot",
44
"description": "Open websites in a floating window",
5-
"version": "1.2.2",
5+
"version": "1.3.0",
66
"author": {
77
"name": "Andrew Brey",
88
"email": "[email protected]"

renderer/pages/_app.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function MyApp({ Component, pageProps }: AppProps) {
1616
switch (key) {
1717
case 'location':
1818
if (typeof window !== 'undefined') localStorage.setItem('flot-last-url', msg);
19+
useStore.setState({ url: msg });
1920
break;
2021
case 'active':
2122
useStore.setState({ childActive: msg });

renderer/store.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import create from 'zustand';
2-
import { prepareDailyMotionUrl, prepareTwitchUrl, prepareVimeoUrl, prepareYoutubeUrl } from './util/magic-urls';
2+
import { prepareDailyMotionUrl, prepareVimeoUrl, prepareYoutubeUrl } from './util/magic-urls';
33

44
interface FlotState {
55
url: string;
@@ -74,7 +74,6 @@ function preprocessURL(target: string) {
7474

7575
target = prepareYoutubeUrl(target);
7676
target = prepareVimeoUrl(target);
77-
target = prepareTwitchUrl(target);
7877
target = prepareDailyMotionUrl(target);
7978

8079
return new URL(target);

renderer/util/magic-urls.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,6 @@ export function prepareVimeoUrl(target: string) {
5656
return `https://player.vimeo.com/video/${videoId}?${queryParams}`;
5757
}
5858

59-
export function prepareTwitchUrl(target: string) {
60-
const asURL = new URL(target);
61-
62-
if (!asURL.host.includes('twitch.tv')) {
63-
return target;
64-
}
65-
66-
const queryParams = asURL.searchParams || {};
67-
68-
queryParams.set('channel', asURL.pathname.substring(1));
69-
queryParams.set('parent', 'localhost'); // TODO, this is a required parameter but it might not be possible to fake
70-
71-
return `https://player.twitch.tv?${queryParams}`;
72-
}
73-
7459
export function prepareDailyMotionUrl(target: string) {
7560
const normalized = target.replace(/^http(s)?:\/\/dai\.ly\//, 'http://www.dailymotion.com/video/');
7661

0 commit comments

Comments
 (0)