-
Notifications
You must be signed in to change notification settings - Fork 882
/
Copy pathpreload.js
103 lines (82 loc) · 2.36 KB
/
preload.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
const toPull = require('stream-to-pull-stream')
const { ipcRenderer, remote } = require('electron')
const readdir = require('recursive-readdir')
const fs = require('fs-extra')
const path = require('path')
const screenshotHook = require('./screenshot')
const connectionHook = require('./connection-status')
const { COUNTLY_KEY, VERSION } = require('../common/consts')
screenshotHook()
connectionHook()
const urlParams = new URLSearchParams(window.location.search)
function checkIfVisible () {
if (document.hidden) {
previousHash = window.location.hash
window.location.hash = '/blank'
} else {
window.location.hash = previousHash
}
}
var originalSetItem = window.localStorage.setItem
window.localStorage.setItem = function () {
if (arguments[0] === 'i18nextLng') {
ipcRenderer.send('updateLanguage', arguments[1])
}
originalSetItem.apply(this, arguments)
}
let previousHash = null
ipcRenderer.on('updatedPage', (_, url) => {
previousHash = url
window.location.hash = url
})
document.addEventListener('visibilitychange', () => {
checkIfVisible()
})
document.addEventListener('DOMContentReady', () => {
checkIfVisible()
})
window.ipfsDesktop = {
countlyAppKey: COUNTLY_KEY,
countlyDeviceId: urlParams.get('deviceId'),
countlyActions: [
'ADD_VIA_DESKTOP',
'DAEMON_START',
'DAEMON_STOP',
'DOWNLOAD_HASH',
'MOVE_REPOSITORY',
'SCREENSHOT_TAKEN'
],
version: VERSION,
selectDirectory: async () => {
const response = await remote.dialog.showOpenDialog(remote.getCurrentWindow(), {
title: 'Select a directory',
properties: [
'openDirectory',
'createDirectory'
]
})
if (!response || response.canceled) {
return
}
const files = []
const filesToRead = response.filePaths[0]
const prefix = path.dirname(filesToRead)
for (const path of await readdir(filesToRead)) {
const size = (await fs.stat(path)).size
files.push({
path: path.substring(prefix.length, path.length),
content: toPull.source(fs.createReadStream(path)),
size: size
})
}
return files
},
removeConsent: (consent) => {
ipcRenderer.send('countly.removeConsent', consent)
},
addConsent: (consent) => {
ipcRenderer.send('countly.addConsent', consent)
}
}
// Inject api address
window.localStorage.setItem('ipfsApi', urlParams.get('api'))