Skip to content

Commit ae9440f

Browse files
authored
Merge pull request #1 from InfrHQ/feature--capture-html
Feature capture html
2 parents 1fc74c8 + a5f0888 commit ae9440f

File tree

6 files changed

+250
-15
lines changed

6 files changed

+250
-15
lines changed

src/cron/index.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
const storage_client = require('../connectors/storage')
22
const {
33
getMacAppNameAndBundleID,
4-
getMacChromeCurrentTabURL,
4+
getMacBrowserCurrentTabURL,
55
getMacAppWindowTitle,
6+
getMacBrowserWindowHTML,
67
} = require('../tools/systemCall')
78
const { desktopCapturer, nativeImage } = require('electron')
89
const { getIncognitoKeywords } = require('../tools/incognitoKeywords')
@@ -35,14 +36,25 @@ class DataStore {
3536

3637
async _getAttributeData() {
3738
const { appName, bundleId } = getMacAppNameAndBundleID()
38-
const data = {
39+
var data = {
3940
app_name: appName,
4041
bundle_id: bundleId,
4142
window_name: getMacAppWindowTitle(),
4243
}
4344

44-
if (appName === 'Google Chrome') {
45-
data.current_url = getMacChromeCurrentTabURL()
45+
if (
46+
appName === 'Google Chrome' ||
47+
appName === 'Brave Browser' ||
48+
appName === 'Safari'
49+
) {
50+
data.current_url = getMacBrowserCurrentTabURL(appName)
51+
try {
52+
if (this.setupData.code_storage_enabled !== false)
53+
data.page_html = getMacBrowserWindowHTML(appName)
54+
else console.log('Code storage disabled, not storing HTML')
55+
} catch (err) {
56+
console.error("Error fetching browser's active tab HTML:", err)
57+
}
4658
}
4759

4860
return data
@@ -108,6 +120,7 @@ class DataStore {
108120
setup_check__server: storage_client.get('setup_check__server'),
109121
manual_stop: false,
110122
incognito_keywords: getIncognitoKeywords(),
123+
code_storage_enabled: storage_client.get('code_storage_enabled'),
111124
}
112125
}
113126

src/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ ipcMain.handle('dashboard-set-incognito-keywords', async (event, keywords) => {
112112
return resp
113113
})
114114

115+
ipcMain.handle('dashboard-get-code-storage-enabled', async (event) => {
116+
let val = storage_client.get('code_storage_enabled')
117+
return val
118+
})
119+
120+
ipcMain.handle('dashboard-set-code-storage-enabled', async (event, enabled) => {
121+
storage_client.set('code_storage_enabled', enabled)
122+
dataStoreCron.updateData()
123+
})
124+
115125
ipcMain.handle('show-window', async (event, window) => {
116126
await mainWindow.loadFile(path.join(__dirname, window))
117127
mainWindow.show()

src/pages/dashboard.html

Lines changed: 105 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
/>
2929
</head>
3030

31-
<body class="">
31+
<body class="mb-10 p-10">
3232
<!-- Nav Bar -->
3333
<nav class="navbar bg-body-tertiary">
3434
<div class="container-fluid">
@@ -89,6 +89,108 @@ <h5 class="card-title">
8989
</div>
9090
-->
9191

92+
<!-- Modal -->
93+
<div
94+
class="modal fade"
95+
id="how_to_enable_applescript_js_modal"
96+
tabindex="-1"
97+
aria-labelledby="how_to_enable_applescript_js_modal"
98+
aria-hidden="true"
99+
>
100+
<div class="modal-dialog">
101+
<div class="modal-content">
102+
<div class="modal-header">
103+
<h1
104+
class="modal-title fs-5"
105+
id="how_to_enable_applescript_js_modal"
106+
>
107+
Enable Applescript on Your Browsers
108+
</h1>
109+
<button
110+
type="button"
111+
class="btn-close"
112+
data-bs-dismiss="modal"
113+
aria-label="Close"
114+
></button>
115+
</div>
116+
<div class="modal-body">
117+
<p class="">
118+
To enable granular storage, you will need to allow
119+
applescript execution in Brave, Chrome and Safari.
120+
</p>
121+
122+
<p>For Brave and Chrome:</p>
123+
<ol>
124+
<li>Open the browsers (Brave and Chrome)</li>
125+
<li>Go to "View"</li>
126+
<li>Go to "Developer"</li>
127+
<li>
128+
Click on "Allow Javascript from Apple Events"
129+
</li>
130+
<li>Restart Chrome, Brave and Infr</li>
131+
</ol>
132+
133+
<p>For Safari:</p>
134+
<ol>
135+
<li>Open the Safari Browser</li>
136+
<li>Go to "Develop"</li>
137+
<li>
138+
Click on "Allow Javascript from Apple Events"
139+
</li>
140+
<li>Restart Safari and Infr</li>
141+
</ol>
142+
<img
143+
class="img-fluid"
144+
src="https://res.cloudinary.com/dcwz20wdd/video/upload/q_auto:eco/v1694600455/Enable_AppleScript_Access_trjggc.gif"
145+
/>
146+
</div>
147+
<div class="modal-footer"></div>
148+
</div>
149+
</div>
150+
</div>
151+
152+
<!-- Code Storage -->
153+
<div class="container mt-5">
154+
<div class="card">
155+
<h5 class="card-header bg-primary text-white">
156+
Granular Storage
157+
<button class="mb-n2 btn btn-success btn-sm ml-3">
158+
Recommended
159+
</button>
160+
</h5>
161+
<div class="card-body">
162+
<p class="card-text">
163+
Allow Infr to store relivable snippets from websites you
164+
visit. This will allow you to revisit the page at any
165+
time in the future.
166+
</p>
167+
<p
168+
class="h6 mb-3"
169+
type="button"
170+
class="btn btn-primary"
171+
data-bs-toggle="modal"
172+
data-bs-target="#how_to_enable_applescript_js_modal"
173+
>
174+
How do I enable this on the browser?
175+
</p>
176+
<input
177+
type="checkbox"
178+
class="btn-check"
179+
id="btn_is_code_storage_enabled"
180+
onclick="handleChangeInCodeStorage()"
181+
autocomplete="off"
182+
checked
183+
/>
184+
<label
185+
class="btn btn-outline-success"
186+
for="btn_is_code_storage_enabled"
187+
id="btn_is_code_storage_enabled_label"
188+
>Code Storage Enabled</label
189+
><br />
190+
</div>
191+
</div>
192+
</div>
193+
92194
<!-- incognito Keywords -->
93195
<div class="container mt-5">
94196
<div class="card">
@@ -98,8 +200,8 @@ <h5 class="card-header bg-primary d-flex text-white">
98200
<div class="card-body">
99201
<p class="card-text">
100202
Apps or websites that include any of the following
101-
keywords will not be tracked. Capitalization does not
102-
matter.
203+
keywords will not be tracked, stored or recorded in any
204+
way. Capitalization does not matter.
103205
</p>
104206
<p class="h6">Useful defaults</p>
105207

src/preload.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ contextBridge.exposeInMainWorld('infrDashboard', {
3737
)
3838
return resp
3939
},
40+
getCodeStorageEnabled: async function () {
41+
let resp = await ipcRenderer.invoke(
42+
'dashboard-get-code-storage-enabled',
43+
)
44+
return resp
45+
},
46+
setCodeStorageEnabled: async function (enabled) {
47+
let resp = await ipcRenderer.invoke(
48+
'dashboard-set-code-storage-enabled',
49+
enabled,
50+
)
51+
return resp
52+
},
4053
})
4154

4255
contextBridge.exposeInMainWorld('infrWindow', {

src/scripts/dashboard.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,58 @@ function addTagOnClick(tag_name) {
6060
tagify.addTags(tag_name)
6161
}
6262

63+
function handleChangeInCodeStorage() {
64+
let btn = document.getElementById('btn_is_code_storage_enabled')
65+
let label = document.getElementById('btn_is_code_storage_enabled_label')
66+
67+
if (btn.checked) {
68+
window.infrDashboard.setCodeStorageEnabled(true)
69+
70+
// Update text & class
71+
label.classList.remove('btn-outline-danger')
72+
label.classList.add('btn-outline-success')
73+
label.innerHTML = 'Code Storage Enabled'
74+
} else {
75+
window.infrDashboard.setCodeStorageEnabled(false)
76+
77+
// Update text
78+
label.classList.remove('btn-outline-success')
79+
label.classList.add('btn-outline-danger')
80+
label.innerHTML = 'Code Storage Disabled'
81+
}
82+
}
83+
84+
function loadCodeStorageEnabled() {
85+
try {
86+
window.infrDashboard.getCodeStorageEnabled().then((enabled) => {
87+
let btn = document.getElementById('btn_is_code_storage_enabled')
88+
let label = document.getElementById(
89+
'btn_is_code_storage_enabled_label',
90+
)
91+
92+
// Null is considered true
93+
if (enabled === false) {
94+
btn.checked = false
95+
96+
// Update text
97+
label.classList.remove('btn-outline-success')
98+
label.classList.add('btn-outline-danger')
99+
label.innerHTML = 'Code Storage Disabled'
100+
} else {
101+
btn.checked = true
102+
103+
// Update text & class
104+
label.classList.remove('btn-outline-danger')
105+
label.classList.add('btn-outline-success')
106+
label.innerHTML = 'Code Storage Enabled'
107+
}
108+
})
109+
} catch (err) {}
110+
}
111+
63112
function loadPage() {
64113
loadIncognitoKeywords()
114+
loadCodeStorageEnabled()
65115
}
66116

67117
loadPage()

src/tools/systemCall.js

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,27 @@ function getMacAppWindowTitle() {
2525
}
2626
}
2727

28-
function getMacChromeCurrentTabURL() {
28+
function getMacBrowserCurrentTabURL(browserName) {
2929
try {
30-
const url = execSync(
31-
'osascript -e \'tell app "Google Chrome" to get the URL of the active tab of its first window\'',
32-
)
33-
.toString()
34-
.trim()
30+
let script = ''
31+
32+
if (browserName === 'Safari') {
33+
script =
34+
'tell application "Safari" to get URL of current tab of front window'
35+
} else if (
36+
browserName === 'Google Chrome' ||
37+
browserName === 'Brave Browser'
38+
) {
39+
script = `tell application "${browserName}" to get the URL of the active tab of its first window`
40+
} else {
41+
throw new Error(`Unsupported browser: ${browserName}`)
42+
}
3543

44+
const url = execSync(`osascript -e '${script}'`).toString().trim()
45+
console.log(`${browserName} URL: ${url}`)
3646
return url
3747
} catch (err) {
38-
console.error("Error fetching Chrome's active tab URL:", err)
48+
console.error(`Error fetching ${browserName}'s active tab URL:`, err)
3949
return null
4050
}
4151
}
@@ -95,10 +105,47 @@ function getMacAppNameAndBundleID() {
95105
}
96106
}
97107

108+
function getMacBrowserWindowHTML(browserName) {
109+
var script = ``
110+
if (browserName === 'Google Chrome') {
111+
script = `
112+
tell application "Google Chrome"
113+
set activeTab to active tab of window 1
114+
set pageHTML to execute activeTab javascript "document.documentElement.outerHTML"
115+
return pageHTML
116+
end tell
117+
`
118+
} else if (browserName === 'Safari') {
119+
script = `
120+
tell application "Safari"
121+
set pageHTML to do JavaScript "document.documentElement.outerHTML" in current tab of window 1
122+
return pageHTML
123+
end tell
124+
`
125+
} else if (browserName === 'Brave Browser') {
126+
script = `
127+
tell application "Brave Browser"
128+
set activeTab to active tab of window 1
129+
set pageHTML to execute activeTab javascript "document.documentElement.outerHTML"
130+
return pageHTML
131+
end tell
132+
`
133+
}
134+
try {
135+
const html = execSync(`osascript -e '${script}'`).toString().trim()
136+
137+
return html
138+
} catch (err) {
139+
console.error("Error fetching browser's active tab HTML:", err)
140+
return null
141+
}
142+
}
143+
98144
module.exports = {
99145
getMacAppWindowTitle,
100-
getMacChromeCurrentTabURL,
101146
getMacAppName,
102147
getMacBundleId,
103148
getMacAppNameAndBundleID,
149+
getMacBrowserCurrentTabURL,
150+
getMacBrowserWindowHTML,
104151
}

0 commit comments

Comments
 (0)