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';
module.exports = async function ({ env }, db, __exports, __eval) {
const { BrowserWindow } = require('electron'),
notionCreateWindow = __exports.createWindow;
module.exports = async function (api, db, __exports, __eval) {
const notionCreateWindow = __exports.createWindow;
__exports.createWindow = (relativeUrl = '', args) => {
const windows = BrowserWindow.getAllWindows();
if (relativeUrl && windows.length) {
windows[0].webContents.send('notion-enhancer:open-tab', {
const windows = api.electron.getNotionWindows();
// '/' is used to create new windows intentionally
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}`,
});
return windows[0];
return window;
}
return notionCreateWindow(relativeUrl, args);
};

View File

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

View File

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

View File

@ -6,9 +6,9 @@
'use strict';
module.exports = async function ({ env }, db, __exports, __eval) {
module.exports = async function (api, db, __exports, __eval) {
const electron = require('electron'),
urlHelpers = env.notionRequire('helpers/urlHelpers'),
urlHelpers = api.electron.notionRequire('helpers/urlHelpers'),
runInBackground = await db.get(['run_in_background']);
if (!runInBackground) return;
@ -19,7 +19,7 @@ module.exports = async function ({ env }, db, __exports, __eval) {
const notionCreateWindow = __exports.createWindow;
__exports.createWindow = (relativeUrl = '', args) => {
const windows = electron.BrowserWindow.getAllWindows();
const windows = api.electron.getNotionWindows();
if (windows.length) windows.forEach((win) => win.show());
if (relativeUrl || !windows.length) {
@ -44,14 +44,15 @@ module.exports = async function ({ env }, db, __exports, __eval) {
return window;
} else {
const window = api.electron.getFocusedNotionWindow() || windows[0];
// prevents duplicate windows on dock/taskbar click
windows[0].focus();
window.focus();
if (relativeUrl) {
// handle requests passed via the notion:// protocol
// 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;
module.exports = async function ({ env, registry }, db, __exports, __eval) {
const electron = require('electron'),
module.exports = async function (api, db, __exports, __eval) {
const { env, registry } = api,
electron = require('electron'),
path = require('path'),
enhancerIcon = path.resolve(`${__dirname}/../../media/colour-x16.png`),
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
// duplicate window prevention
const createWindow = () => {
const { createWindow } = env.notionRequire('main/createWindow.js');
const { createWindow } = api.electron.notionRequire('main/createWindow.js');
createWindow('/');
};
electron.ipcMain.on('notion-enhancer:create-new-window', createWindow);