-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (127 loc) · 4.4 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
const { app, BrowserWindow, session, globalShortcut, ipcMain } = require('electron');
const dbus = require('dbus-next');
const debug = process.env.ADFRIFY_DEBUG || false;
/**
* Handle D-Bus keys
* @param window BrowserWindow
* @param desktopEnv Enviroment
* @param session Session
* @returns {Promise<void>}
*/
async function registerBindings(window, desktopEnv, session) {
const listener = (n, keyName) => {
if (debug)
console.log(keyName);
switch (keyName) {
case 'Next':
sendEvent(window, 'shortcut', 'MediaNextTrack');
return;
case 'Previous':
sendEvent(window, 'shortcut', 'MediaPreviousTrack');
return;
case "Play":
sendEvent(window, 'shortcut', 'MediaPlayPause');
return
default:
return;
}
};
const legacy = await session.getProxyObject(`org.${desktopEnv}.SettingsDaemon`, `/org/${desktopEnv}/SettingsDaemon/MediaKeys`);
const interfaceLegacy = legacy.getInterface(`org.${desktopEnv}.SettingsDaemon.MediaKeys`);
interfaceLegacy.on('MediaPlayerKeyPressed', listener);
app.on('browser-window-focus', () => {
interfaceLegacy.GrabMediaPlayerKeys('GPMDP', 0); // eslint-disable-line
});
const future = await session.getProxyObject(`org.${desktopEnv}.SettingsDaemon.MediaKeys`, `/org/${desktopEnv}/SettingsDaemon/MediaKeys`);
const interfaceFuture = future.getInterface(`org.${desktopEnv}.SettingsDaemon.MediaKeys`);
interfaceFuture.on('MediaPlayerKeyPressed', listener);
app.on('browser-window-focus', () => {
interfaceFuture.GrabMediaPlayerKeys('GPMDP', 0); // eslint-disable-line
});
}
//const DBus = require('dbus');
function createWindow () {
const win = new BrowserWindow({
width: 1290,
height: 750,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
webSecurity: false,
webviewTag: true
},
icon: 'icon.png'
})
win.loadFile('index.html');
return win;
}
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
/**
* Block Ads and change user agent
*/
function applyTrafficMonitor() {
session.defaultSession.webRequest.onBeforeSendHeaders((details, callback) => {
details.requestHeaders['User-Agent'] = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0';
callback({ cancel: false, requestHeaders: details.requestHeaders });
});
session.defaultSession.webRequest.onBeforeRequest(
function(request, callback) {
if (debug)
console.log("[SPAB] -> onBeforeRequest began");
if (request && request.url) {
if (debug)
console.log("[SPAB] -> "+request.url);
if (request.url.includes("storage-resolve/files/")) {
if (debug)
console.log("[SPAB] -> Found AD Request!");
callback({cancel: true});
return;
}
}
callback({cancel: false});
}
);
}
function sendEvent(window, name, ...args) {
if (window != null) {
window.webContents.send(name, [...args]);
ipcMain.emit(name);
}
}
function registerKeys(window) {
globalShortcut.register('MediaPlayPause', () => {
if (debug)
console.log("PLAYPAUSE");
sendEvent(window, 'shortcut', 'MediaPlayPause');
});
globalShortcut.register('medianexttrack', () => {
if (debug)
console.log("NEXT");
sendEvent(window, 'shortcut', 'MediaNextTrack');
});
globalShortcut.register('mediaprevioustrack', () => {
if (debug)
console.log("PREV");
sendEvent(window, 'shortcut', 'MediaPreviousTrack');
});
}
app.commandLine.appendSwitch('disable-site-isolation-trials');
app.whenReady().then(() => {
let win = createWindow();
applyTrafficMonitor();
try {
const session = dbus.sessionBus();
registerBindings(win, 'gnome', session).catch(() => {
registerBindings(win, 'mate', session).catch(() => {
// Fallback!
registerKeys(win);
});
});
} catch (e) {
// Fallback!
registerKeys(win);
}
})