prevent attempts to open notion urls in menu window

This commit is contained in:
dragonwocky 2021-12-12 23:11:41 +11:00
parent ab300633ec
commit 01dc6d4bb7
5 changed files with 33 additions and 23 deletions

View File

@ -6,16 +6,17 @@
'use strict'; 'use strict';
module.exports = async function ({ env }, db, __exports, __eval) { module.exports = async function (api, db, __exports, __eval) {
const { BrowserWindow } = require('electron'), const notionCreateWindow = __exports.createWindow;
notionCreateWindow = __exports.createWindow;
__exports.createWindow = (relativeUrl = '', args) => { __exports.createWindow = (relativeUrl = '', args) => {
const windows = BrowserWindow.getAllWindows(); const windows = api.electron.getNotionWindows();
if (relativeUrl && windows.length) { // '/' is used to create new windows intentionally
windows[0].webContents.send('notion-enhancer:open-tab', { if (relativeUrl && relativeUrl !== '/' && windows.length) {
const window = api.electron.getFocusedNotionWindow() || windows[0];
window.webContents.send('notion-enhancer:open-tab', {
notionUrl: `notion://www.notion.so${relativeUrl}`, notionUrl: `notion://www.notion.so${relativeUrl}`,
}); });
return windows[0]; return window;
} }
return notionCreateWindow(relativeUrl, args); return notionCreateWindow(relativeUrl, args);
}; };

View File

@ -15,16 +15,22 @@ module.exports = async function ({}, db, __exports, __eval) {
__eval(` __eval(`
const notionHandleActivate = handleActivate; const notionHandleActivate = handleActivate;
handleActivate = (relativeUrl) => { handleActivate = (relativeUrl) => {
const { BrowserWindow } = require('electron'), const api = require('notion-enhancer/api/index.cjs'),
windows = BrowserWindow.getAllWindows(), { BrowserWindow } = require('electron'),
focusedWindow = BrowserWindow.getFocusedWindow(); windows = api.electron.getNotionWindows(),
electronWindows = BrowserWindow.getAllWindows();
if (relativeUrl && windows.length) { if (relativeUrl && windows.length) {
const win = focusedWindow || windows[0]; const win = api.electron.getFocusedNotionWindow() || windows[0];
win.webContents.send('notion-enhancer:open-tab', { win.webContents.send('notion-enhancer:open-tab', {
notionUrl: \`notion://www.notion.so\$\{relativeUrl\}\`, notionUrl: \`notion://www.notion.so\$\{relativeUrl\}\`,
}); });
win.show(); win.show();
win.focus(); win.focus();
} else if (relativeUrl && electronWindows.length && !windows.length) {
// enhancer menu is open: prevent override
const { createWindow } = api.electron.notionRequire('main/createWindow'),
win = createWindow(relativeUrl);
win.focus();
} else notionHandleActivate(relativeUrl); } else notionHandleActivate(relativeUrl);
}; };
`); `);

View File

@ -8,10 +8,11 @@
let focusedTab; let focusedTab;
module.exports = async function ({ components, env, web, fmt, fs }, db, tabCache = new Map()) { module.exports = async function (api, db, tabCache = new Map()) {
const electron = require('electron'), const { components, web, fmt, fs } = api,
electron = require('electron'),
electronWindow = electron.remote.getCurrentWindow(), electronWindow = electron.remote.getCurrentWindow(),
notionIpc = env.notionRequire('helpers/notionIpc'), notionIpc = api.electron.notionRequire('helpers/notionIpc'),
xIcon = await components.feather('x'); xIcon = await components.feather('x');
return class Tab { return class Tab {

View File

@ -6,9 +6,9 @@
'use strict'; 'use strict';
module.exports = async function ({ env }, db, __exports, __eval) { module.exports = async function (api, db, __exports, __eval) {
const electron = require('electron'), const electron = require('electron'),
urlHelpers = env.notionRequire('helpers/urlHelpers'), urlHelpers = api.electron.notionRequire('helpers/urlHelpers'),
runInBackground = await db.get(['run_in_background']); runInBackground = await db.get(['run_in_background']);
if (!runInBackground) return; if (!runInBackground) return;
@ -19,7 +19,7 @@ module.exports = async function ({ env }, db, __exports, __eval) {
const notionCreateWindow = __exports.createWindow; const notionCreateWindow = __exports.createWindow;
__exports.createWindow = (relativeUrl = '', args) => { __exports.createWindow = (relativeUrl = '', args) => {
const windows = electron.BrowserWindow.getAllWindows(); const windows = api.electron.getNotionWindows();
if (windows.length) windows.forEach((win) => win.show()); if (windows.length) windows.forEach((win) => win.show());
if (relativeUrl || !windows.length) { if (relativeUrl || !windows.length) {
@ -44,14 +44,15 @@ module.exports = async function ({ env }, db, __exports, __eval) {
return window; return window;
} else { } else {
const window = api.electron.getFocusedNotionWindow() || windows[0];
// prevents duplicate windows on dock/taskbar click // prevents duplicate windows on dock/taskbar click
windows[0].focus(); window.focus();
if (relativeUrl) { if (relativeUrl) {
// handle requests passed via the notion:// protocol // handle requests passed via the notion:// protocol
// or ctrl+click // or ctrl+click
windows[0].loadURL(urlHelpers.getIndexUrl(relativeUrl)); window.loadURL(urlHelpers.getIndexUrl(relativeUrl));
} }
return windows[0]; return window;
} }
}; };
}; };

View File

@ -8,8 +8,9 @@
let tray; let tray;
module.exports = async function ({ env, registry }, db, __exports, __eval) { module.exports = async function (api, db, __exports, __eval) {
const electron = require('electron'), const { env, registry } = api,
electron = require('electron'),
path = require('path'), path = require('path'),
enhancerIcon = path.resolve(`${__dirname}/../../media/colour-x16.png`), enhancerIcon = path.resolve(`${__dirname}/../../media/colour-x16.png`),
hotkey = await db.get(['hotkey']), hotkey = await db.get(['hotkey']),
@ -47,7 +48,7 @@ module.exports = async function ({ env, registry }, db, __exports, __eval) {
// since notion's default is broken by // since notion's default is broken by
// duplicate window prevention // duplicate window prevention
const createWindow = () => { const createWindow = () => {
const { createWindow } = env.notionRequire('main/createWindow.js'); const { createWindow } = api.electron.notionRequire('main/createWindow.js');
createWindow('/'); createWindow('/');
}; };
electron.ipcMain.on('notion-enhancer:create-new-window', createWindow); electron.ipcMain.on('notion-enhancer:create-new-window', createWindow);