2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-06-12 00:52:20 +00:00
nativefier/app/src/helpers/helpers.js
David Kramer ac99c6424d macOS: Add native tabs (PR #579)
Electron supports using native tabs on macOS (API added in Electron 1.8.1). This change adds a context menu item on platforms that support it (macOS for now) to open links in new tabs, and also adds support for {command,middle}-clicking links to open them in a new tab.

Maintainer (@ronjouch) note: this feature is macOS-only. Windows/Linux patches welcome 🙂.
2018-05-01 19:24:35 -04:00

77 lines
1.6 KiB
JavaScript

import wurl from 'wurl';
import os from 'os';
import fs from 'fs';
import path from 'path';
const INJECT_CSS_PATH = path.join(__dirname, '..', 'inject/inject.css');
const log = require('loglevel');
function isOSX() {
return os.platform() === 'darwin';
}
function isLinux() {
return os.platform() === 'linux';
}
function isWindows() {
return os.platform() === 'win32';
}
function linkIsInternal(currentUrl, newUrl, internalUrlRegex) {
if (internalUrlRegex) {
const regex = RegExp(internalUrlRegex);
return regex.test(newUrl);
}
const currentDomain = wurl('domain', currentUrl);
const newDomain = wurl('domain', newUrl);
return currentDomain === newDomain;
}
function shouldInjectCss() {
try {
fs.accessSync(INJECT_CSS_PATH, fs.F_OK);
return true;
} catch (e) {
return false;
}
}
function getCssToInject() {
return fs.readFileSync(INJECT_CSS_PATH).toString();
}
/**
* Helper method to print debug messages from the main process in the browser window
* @param {BrowserWindow} browserWindow
* @param message
*/
function debugLog(browserWindow, message) {
// need the timeout as it takes time for the preload javascript to be loaded in the window
setTimeout(() => {
browserWindow.webContents.send('debug', message);
}, 3000);
log.info(message);
}
function getAppIcon() {
return path.join(__dirname, '../', `/icon.${isWindows() ? 'ico' : 'png'}`);
}
function nativeTabsSupported() {
return isOSX();
}
export default {
isOSX,
isLinux,
isWindows,
linkIsInternal,
getCssToInject,
debugLog,
shouldInjectCss,
getAppIcon,
nativeTabsSupported,
};