-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
154 lines (132 loc) · 3.84 KB
/
index.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const {
app,
BrowserWindow,
Menu,
shell,
session,
globalShortcut
} = require("electron");
var https = require("https");
var http = require("http");
var path = require("path");
const { parse } = require("url");
const defaultMenu = require("electron-default-menu");
const fetch = require("isomorphic-fetch");
const sites = require("./sites.json");
const shortners = require("./shortners.json");
const menuItems = sites
.filter(site => site.url)
.map(site => ({ label: site.label, url: site.url }));
const getPrefsDir = () => {
if (process.env.APPDATA) {
// Windows
return path.resolve(process.env.APPDATA, "Bento");
} else if (process.platform == "darwin") {
// Mac
return path.resolve(process.env.HOME, "Library", "Preferences", "Bento");
} else {
// Linux
return path.resolve(process.env.HOME, ".bento");
}
};
const openUrl = (urlString, stack = []) => {
fetch(urlString, { method: "HEAD", mode: "cors", redirect: "manual" }).then(
response => {
if (response.status === 301) {
const location = response.headers.get("Location");
if (location !== urlString) {
openUrl(location, [...stack, urlString]);
} else {
reallyOpenUrl(urlString, stack);
}
} else {
reallyOpenUrl(urlString, stack);
}
}
);
};
const reallyOpenUrl = (urlString, stack) => {
if (stack.length > 0) {
console.log(stack.join(" -> "), "->", urlString);
}
const url = parse(urlString);
const matched = sites.filter(site => site.host === url.host);
if (matched.length === 0) {
console.log(urlString);
shell.openExternal(urlString);
return;
}
const site = matched[0];
const ses = session.fromPartition("persist:" + site.partition);
if (!site.stateful) {
ses.clearCache(() => undefined);
ses.clearStorageData();
}
var mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: false,
session: ses
}
});
mainWindow.on("app-command", (e, cmd) => {
// Navigate the window back when the user hits their mouse back button
if (cmd === "browser-backward" && win.webContents.canGoBack()) {
win.webContents.goBack();
}
});
ses.webRequest.onBeforeSendHeaders(filter, (details, callback) => {
const url = parse(details.url);
const found = site.hosts.filter(hostPart => url.host.endsWith(hostPart));
if (found.length > 0 || site.host === url.host) {
callback({ cancel: false, requestHeaders: details.requestHeaders });
} else {
console.log(details.method, details.url);
callback({ cancel: true, requestHeaders: details.requestHeaders });
if (details.resourceType === "mainFrame" && details.method === "GET") {
openUrl(details.url);
mainWindow.webContents.goBack();
}
}
});
mainWindow.webContents.on("new-window", function(e, url) {
e.preventDefault();
openUrl(url);
});
mainWindow.maximize();
mainWindow.loadURL(urlString);
};
const prefsDir = getPrefsDir();
app.setPath("userData", path.resolve(prefsDir, "data"), app.name);
app.setPath("userCache", path.resolve(prefsDir, "cache"), app.name);
app.on("ready", () => {
sites
.filter(site => site.shortcut)
.forEach(site => {
globalShortcut.register(site.shortcut, () => {
openUrl(site.url);
});
});
// Get template for default menu
const menu = defaultMenu(app, shell);
// Add custom menu
menu.splice(4, 0, {
label: "Sites",
submenu: menuItems.map(menuItem => {
return {
label: menuItem.label,
click: () => {
openUrl(menuItem.url);
}
};
})
});
// Set top-level application menu, using modified template
Menu.setApplicationMenu(Menu.buildFromTemplate(menu));
});
// Quit when all windows are closed.
app.on("window-all-closed", () => {
// app.quit()
});
const filter = {
urls: ["*"]
};