Skip to content

Commit

Permalink
fix: script injection for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelgomesxyz committed Apr 15, 2023
1 parent f731c8a commit b4865b3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
6 changes: 5 additions & 1 deletion src/common/ScriptInjector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class _ScriptInjector {
params: Record<string, unknown> = {},
tabId: number | null = null
): Promise<T | null> {
if (!url) {
if (!url && Shared.manifestVersion !== 3) {
return null;
}

Expand All @@ -206,6 +206,8 @@ class _ScriptInjector {
target: { tabId },
func: Shared.functionsToInject[id],
args: [params],
// @ts-expect-error This is a newer value, so it's missing from the types.
world: 'MAIN',
})
.then((results) => {
const value = results[0].result as T | null;
Expand All @@ -225,6 +227,8 @@ class _ScriptInjector {
target: { tabId },
func: Shared.functionsToInject[id],
args: [params],
// @ts-expect-error This is a newer value, so it's missing from the types.
world: 'MAIN',
});
value = results[0].result as T | null;
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/common/ScrobbleParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export abstract class ScrobbleParser {
* Below are the methods that can be used to parse the playback. Generic methods do not need to be overridden in the child class, as they should work out-of-the-box. If one method fails, the next one is attempted, in the order listed.
*
* 1. **video player:** generic method, based on `videoPlayerSelector`, which can be specified through the options
* 2. **injected script:** specific method (requires adding a function to the `{serviceId}-playback` key in `Shared.functionsToInject` e.g. `netflix-playback`)
* 2. **injected script:** specific method (requires adding a function to the `{serviceId}-playback` key in `Shared.functionsToInject` at the `{serviceName}Api.ts` file e.g. `netflix-playback` at `NetflixApi.ts`)
* 3. **DOM:** specific method (requires override)
* 4. **custom:** specific method (requires override)
*/
Expand Down Expand Up @@ -193,7 +193,7 @@ export abstract class ScrobbleParser {
* Below are the methods that can be used to parse the item. Generic methods do not need to be overridden in the child class, as they should work out-of-the-box. If one method fails, the next one is attempted, in the order listed.
*
* 1. **API:** generic method, but requires a non-null return from `parseItemId` and the implementation of `*Api#getItem`
* 2. **injected script:** specific method (requires adding a function to the `{serviceId}-item` key in `Shared.functionsToInject` e.g. `netflix-item`)
* 2. **injected script:** specific method (requires adding a function to the `{serviceId}-item` key in `Shared.functionsToInject` at the `{serviceName}Api.ts` file e.g. `netflix-item` at `NetflixApi.ts`)
* 3. **DOM:** specific method (requires override)
* 4. **custom:** specific method (requires override)
*/
Expand Down Expand Up @@ -254,7 +254,7 @@ export abstract class ScrobbleParser {
* Below are the methods that can be used to parse the item ID. Generic methods do not need to be overridden in the child class, as they should work out-of-the-box. If one method fails, the next one is attempted, in the order listed.
*
* 1. **URL:** generic method, based on `watchingUrlRegex`, which can be specified through the options
* 2. **injected script:** specific method (requires adding a function to the `{serviceId}-item-id` key in `Shared.functionsToInject` e.g. `netflix-item-id`)
* 2. **injected script:** specific method (requires adding a function to the `{serviceId}-item-id` key in `Shared.functionsToInject` at the `{serviceName}Api.ts` file e.g. `netflix-item-id` at `NetflixApi.ts`)
* 3. **DOM:** specific method (requires override)
* 4. **custom:** specific method (requires override)
*/
Expand Down
13 changes: 13 additions & 0 deletions src/services/nrk/NrkApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NrkService } from '@/nrk/NrkService';
import { ServiceApi } from '@apis/ServiceApi';
import { Requests, withHeaders } from '@common/Requests';
import { Shared } from '@common/Shared';
import { Utils } from '@common/Utils';
import {
BaseItemValues,
Expand Down Expand Up @@ -333,4 +334,16 @@ class _NrkApi extends ServiceApi {
}
}

Shared.functionsToInject[`${NrkService.id}-item-id`] = () => {
let itemId: string | null = null;
const { player } = window;
if (player) {
const playbackSession = player.getPlaybackSession();
if (playbackSession) {
itemId = playbackSession.mediaItem?.id ?? null;
}
}
return itemId;
};

export const NrkApi = new _NrkApi();
13 changes: 0 additions & 13 deletions src/services/nrk/NrkParser.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
import { NrkApi } from '@/nrk/NrkApi';
import { ScrobbleParser } from '@common/ScrobbleParser';
import { Shared } from '@common/Shared';

class _NrkParser extends ScrobbleParser {
constructor() {
super(NrkApi, { videoPlayerSelector: '.tv-series-video-player video' });
}
}

Shared.functionsToInject[`${NrkApi.id}-item-id`] = () => {
let itemId: string | null = null;
const { player } = window;
if (player) {
const playbackSession = player.getPlaybackSession();
if (playbackSession) {
itemId = playbackSession.mediaItem?.id ?? null;
}
}
return itemId;
};

export const NrkParser = new _NrkParser();

0 comments on commit b4865b3

Please sign in to comment.