Skip to content

Commit

Permalink
fix(Stremio): resolve player state promise before submitting the event (
Browse files Browse the repository at this point in the history
#8540)

* fix(Stremio): resolve player state promise before submitting the event

There's an issue with Chromium-based browsers (such as Chrome and Microsoft Edge) in particular where the received player state is always `null`. On Firefox this issue does not occur. I'm not 100% sure of the exact cause of the discrepancy between the two browsers, but it seems like Chromium has trouble resolving promises that were dispatched by the `CustomEvent` event. So, to address this issue we have to resolve the promise first before dispatching the event.

I also added a check to gracefully handle cases where the `metaItem` field is still `undefined`, which may happen to users with slow network speeds.

* chore(Stremio): bump version
  • Loading branch information
sleeyax authored Jun 30, 2024
1 parent ab66a80 commit d8ef9f7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion websites/S/Stremio/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"web.strem.io",
"web.stremio.com"
],
"version": "2.0.4",
"version": "2.0.5",
"logo": "https://cdn.rcd.gg/PreMiD/websites/S/Stremio/assets/logo.png",
"thumbnail": "https://cdn.rcd.gg/PreMiD/websites/S/Stremio/assets/thumbnail.jpg",
"color": "#8A5AAB",
Expand Down
17 changes: 12 additions & 5 deletions websites/S/Stremio/presence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,17 @@ function _eval(js: string): Promise<any> {
script.id = eventName;
script.appendChild(
document.createTextNode(`
var core = window.services.core;
var pmdEvent = new CustomEvent("${eventName}", {detail: ${js}});
window.dispatchEvent(pmdEvent);
`)
var core = window.services.core;
var result = ${js};
if (result instanceof Promise) {
result.then((awaitedResult) => {
window.dispatchEvent(new CustomEvent("${eventName}", { detail: awaitedResult }));
});
} else {
window.dispatchEvent(new CustomEvent("${eventName}", { detail: result }));
}
`)
);

document.head.appendChild(script);
Expand Down Expand Up @@ -331,7 +338,7 @@ presence.on("UpdateData", async () => {
const playerState = await _eval(
"core.transport.getState('player')"
);
if (playerState.metaItem.type.toLowerCase() === "ready") {
if (playerState?.metaItem?.type?.toLowerCase() === "ready") {
const {
metaItem: { content },
seriesInfo,
Expand Down

0 comments on commit d8ef9f7

Please sign in to comment.