Skip to content

Commit 0931180

Browse files
Add error handling to the deciphering code (FreeTubeApp#8139)
1 parent c3f1a0d commit 0931180

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

src/index.ejs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<body>
1515
<div id="app"></div>
16-
<% if (process.env.SUPPORTS_LOCAL_API) { %>
16+
<% if (process.env.IS_ELECTRON) { %>
1717
<iframe
1818
id="sigFrame"
1919
src="<%= sigFrameSrc %>"
@@ -24,8 +24,7 @@
2424
style="display: none; pointer-events: none"
2525
tabindex="-1"
2626
></iframe>
27-
<% } %>
28-
<% if (!process.env.IS_ELECTRON) { %>
27+
<% } else { %>
2928
<script>
3029
// This is the service worker with the Advanced caching
3130

src/renderer/helpers/api/local.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const TRACKING_PARAM_NAMES = [
2222

2323
if (process.env.SUPPORTS_LOCAL_API) {
2424
Platform.shim.eval = (data, env) => {
25-
return new Promise((resolve) => {
25+
return new Promise((resolve, reject) => {
2626
const properties = []
2727

2828
if (env.n) {
@@ -43,23 +43,31 @@ if (process.env.SUPPORTS_LOCAL_API) {
4343
? crypto.randomUUID()
4444
: `${Date.now()}-${Math.floor(Math.random() * 10000)}`
4545

46-
const iframe = document.getElementById('sigFrame')
46+
if (process.env.IS_ELECTRON) {
47+
const iframe = document.getElementById('sigFrame')
4748

48-
/** @param {MessageEvent} event */
49-
const listener = (event) => {
50-
if (event.source === iframe.contentWindow && typeof event.data === 'string') {
51-
const data = JSON.parse(event.data)
49+
/** @param {MessageEvent} event */
50+
const listener = (event) => {
51+
if (event.source === iframe.contentWindow && typeof event.data === 'string') {
52+
const data = JSON.parse(event.data)
5253

53-
if (data.id === messageId) {
54-
window.removeEventListener('message', listener)
54+
if (data.id === messageId) {
55+
window.removeEventListener('message', listener)
5556

56-
resolve(data.result)
57+
if (data.error) {
58+
reject(data.error)
59+
} else {
60+
resolve(data.result)
61+
}
62+
}
5763
}
5864
}
59-
}
6065

61-
window.addEventListener('message', listener)
62-
iframe.contentWindow.postMessage(JSON.stringify({ id: messageId, code }), '*')
66+
window.addEventListener('message', listener)
67+
iframe.contentWindow.postMessage(JSON.stringify({ id: messageId, code }), '*')
68+
} else {
69+
reject(new Error('Please setup the eval function for the n/sig deciphering'))
70+
}
6371
})
6472
}
6573
}

src/renderer/sigFrameScript.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ window.addEventListener('message', (event) => {
44
// eslint-disable-next-line @stylistic/semi
55
const data = JSON.parse(event.data);
66

7-
window.parent.postMessage(JSON.stringify({
8-
id: data.id,
9-
// eslint-disable-next-line no-new-func
10-
result: new Function(data.code)()
11-
}), '*')
7+
try {
8+
window.parent.postMessage(JSON.stringify({
9+
id: data.id,
10+
// eslint-disable-next-line no-new-func
11+
result: new Function(data.code)()
12+
}), '*')
13+
} catch (error) {
14+
window.parent.postMessage(JSON.stringify({
15+
id: data.id,
16+
error
17+
}), '*')
18+
}
1219
})

0 commit comments

Comments
 (0)