This repository has been archived by the owner on Sep 29, 2023. It is now read-only.
/
helpers.ts
98 lines (84 loc) · 2.34 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { BrowserWindow } from 'electron';
const INJECT_CSS_PATH = path.join(__dirname, '..', 'inject/inject.css');
export function isOSX(): boolean {
return os.platform() === 'darwin';
}
export function isLinux(): boolean {
return os.platform() === 'linux';
}
export function isWindows(): boolean {
return os.platform() === 'win32';
}
export function linkIsInternal(
currentUrl: string,
newUrl: string,
internalUrlRegex: string | RegExp,
): boolean {
if (newUrl === 'about:blank') {
return true;
}
if (internalUrlRegex) {
const regex = RegExp(internalUrlRegex);
return regex.test(newUrl);
}
try {
const currentDomain = new URL(currentUrl).hostname;
const newDomain = new URL(newUrl).hostname;
return currentDomain === newDomain;
} catch (err) {
console.warn(
'Failed to parse domains as determining if link is internal. From:',
currentUrl,
'To:',
newUrl,
);
return false;
}
}
export function shouldInjectCss(): boolean {
try {
fs.accessSync(INJECT_CSS_PATH);
return true;
} catch (e) {
return false;
}
}
export function getCssToInject(): string {
return fs.readFileSync(INJECT_CSS_PATH).toString();
}
/**
* Helper to print debug messages from the main process in the browser window
*/
export function debugLog(browserWindow: BrowserWindow, message: string): void {
// Need a delay, as it takes time for the preloaded js to be loaded by the window
setTimeout(() => {
browserWindow.webContents.send('debug', message);
}, 3000);
console.info(message);
}
export function getAppIcon(): string {
// Prefer ICO under Windows, see
// https://www.electronjs.org/docs/api/browser-window#new-browserwindowoptions
// https://www.electronjs.org/docs/api/native-image#supported-formats
if (isWindows()) {
const ico = path.join(__dirname, '..', 'icon.ico');
if (fs.existsSync(ico)) {
return ico;
}
}
const png = path.join(__dirname, '..', 'icon.png');
if (fs.existsSync(png)) {
return png;
}
}
export function nativeTabsSupported(): boolean {
return isOSX();
}
export function getCounterValue(title: string): string {
const itemCountRegex = /[([{]([\d.,]*)\+?[}\])]/;
const match = itemCountRegex.exec(title);
return match ? match[1] : undefined;
}