Skip to content

Commit 8475479

Browse files
authored
feat(chromecast): Allow async access to cast.__platform__ via postMessage (#84)
Expose `cast.__platform__` asynchronously through postMessage. Cannot be used to proxy synchronous calls, but could be used for debugging or with an async shim and `await` on all calls from the client.
1 parent f68b226 commit 8475479

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

.github/workflows/test.yaml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,30 @@ jobs:
1212
matrix:
1313
# Oldest supported LTS version through current LTS
1414
node-version: [12.x, 14.x, 16.x]
15-
os: [macOS-latest, windows-latest, ubuntu-latest]
15+
# NOTE: Old versions aren't available for mac-arm64, so we use macos-13
16+
# (the last CI image based on x64 hardware)
17+
os: [macos-13, windows-latest, ubuntu-latest]
1618
fail-fast: false
1719

1820
steps:
1921
- name: Configure git
2022
run: git config --global core.autocrlf false
2123

2224
- name: Checkout code
23-
uses: actions/checkout@v2
25+
uses: actions/checkout@v4
2426

2527
- name: Setup Node.js ${{ matrix.node-version }}
26-
uses: actions/setup-node@v1
28+
uses: actions/setup-node@v4
2729
with:
2830
node-version: ${{ matrix.node-version }}
2931

32+
# Default Java on macos-13 seems to have an issue with building our jar
33+
- name: Setup Java
34+
uses: actions/setup-java@v4
35+
with:
36+
distribution: zulu
37+
java-version: 11
38+
3039
- name: Install deps
3140
shell: bash
3241
run: |

backends/chromecast/receiver.html

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,44 @@
3737
</style>
3838
<script>
3939

40+
// Expose cast.__platform__ asynchronously through postMessage.
41+
// Cannot be used to proxy synchronous calls, but could be used for debugging
42+
// or with an async shim and `await` on all calls from the client.
43+
window.addEventListener('message', (event) => {
44+
const data = event.data;
45+
console.log('Top window received message:', data);
46+
47+
if (data.type == 'cast.__platform__') {
48+
const platform = cast.__platform__;
49+
const command = platform[data.command];
50+
51+
const args = data.args;
52+
try {
53+
const result = command.apply(platform, args);
54+
55+
const message = {
56+
id: data.id,
57+
type: data.type + ':result',
58+
result: result,
59+
};
60+
61+
console.log('Top window sending result:', message);
62+
event.source.postMessage(message, '*');
63+
} catch (error) {
64+
console.log('Failed:', error);
65+
66+
const message = {
67+
id: data.id,
68+
type: data.type + ':error',
69+
error: error.message,
70+
};
71+
72+
console.log('Top window sending error:', message);
73+
event.source.postMessage(message, '*');
74+
}
75+
}
76+
});
77+
4078
window.addEventListener('DOMContentLoaded', () => {
4179
// Ignore the leading '?'. The rest is the URL.
4280
const frameUrl = (location.search + location.hash).substr(1);

0 commit comments

Comments
 (0)