Skip to content

Commit

Permalink
Support twitch.tv
Browse files Browse the repository at this point in the history
  • Loading branch information
killergerbah committed Feb 28, 2024
1 parent f542a39 commit 0d530de
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion extension/src/controllers/video-data-sync-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export default class VideoDataSyncController {
: {
open: true,
isLoading: this._context.subSyncAvailable && this._waitingForSubtitles,
suggestedName: '',
suggestedName: document.title,
selectedSubtitle: ['-'],
error: '',
showSubSelect: true,
Expand Down
4 changes: 4 additions & 0 deletions extension/src/pages.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@
"autoSync": {
"enabled": true
}
},
{
"host": "www.twitch.tv",
"allowBlankSrc": true
}
]
}
10 changes: 8 additions & 2 deletions extension/src/services/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import pagesConfig from '../pages.json';

interface PageConfig {
host: string;
script: string;
script?: string;
path?: string;
hash?: string;
autoSync: {
allowBlankSrc?: boolean;
autoSync?: {
enabled: boolean;
videoSrc?: string;
elementId?: string;
Expand Down Expand Up @@ -40,6 +41,10 @@ export class PageDelegate {
}

loadScripts() {
if (this.config.script === undefined) {
return;
}

const s = document.createElement('script');
s.src = chrome.runtime.getURL(`pages/${this.config.script}`);
s.onload = () => s.remove();
Expand Down Expand Up @@ -68,6 +73,7 @@ export class PageDelegate {

canAutoSync(element: HTMLMediaElement) {
return (
this.config.autoSync !== undefined &&
this.config.autoSync.enabled &&
(this.config.autoSync.elementId === undefined || element.id === this.config.autoSync.elementId) &&
(this.config.autoSync.videoSrc === undefined || new RegExp(this.config.autoSync.videoSrc).test(element.src))
Expand Down
14 changes: 9 additions & 5 deletions extension/src/video.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Binding from './services/binding';
import { currentPageDelegate } from './services/pages';
import { PageDelegate, currentPageDelegate } from './services/pages';
import VideoSelectController from './controllers/video-select-controller';
import {
CopyToClipboardMessage,
Expand Down Expand Up @@ -41,7 +41,11 @@ const bindToggleSidePanel = () => {
});
};

const hasValidVideoSource = (videoElement: HTMLVideoElement) => {
const hasValidVideoSource = (videoElement: HTMLVideoElement, page?: PageDelegate) => {
if (page?.config?.allowBlankSrc) {
return true;
}

if (videoElement.src) {
return true;
}
Expand All @@ -60,7 +64,7 @@ const hasValidVideoSource = (videoElement: HTMLVideoElement) => {
const bind = () => {
const bindings: Binding[] = [];
const page = currentPageDelegate();
let subSyncAvailable = page !== undefined;
let subSyncAvailable = page?.config.script !== undefined;
let frameInfoListener: FrameInfoListener | undefined;
let frameInfoBroadcaster: FrameInfoBroadcaster | undefined;
const isParentDocument = window.self === window.top;
Expand All @@ -81,7 +85,7 @@ const bind = () => {
const videoElement = videoElements[i];
const bindingExists = bindings.filter((b) => b.video.isSameNode(videoElement)).length > 0;

if (!bindingExists && hasValidVideoSource(videoElement) && !page?.shouldIgnore(videoElement)) {
if (!bindingExists && hasValidVideoSource(videoElement, page) && !page?.shouldIgnore(videoElement)) {
const b = new Binding(videoElement, subSyncAvailable, frameInfoBroadcaster?.frameId);
b.bind();
bindings.push(b);
Expand All @@ -95,7 +99,7 @@ const bind = () => {
for (let j = 0; j < videoElements.length; ++j) {
const videoElement = videoElements[j];

if (videoElement.isSameNode(b.video) && hasValidVideoSource(videoElement)) {
if (videoElement.isSameNode(b.video) && hasValidVideoSource(videoElement, page)) {
videoElementExists = true;
break;
}
Expand Down

0 comments on commit 0d530de

Please sign in to comment.