Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strategies POC #156

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/content/Control.Strategies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

import { Status } from './Status.Types.js';

export class BaseControlStrategy {
static play() {}
static pause() {}
}

export class oneOfTheVideos extends BaseControlStrategy {
static getVideosArray() {
return Array.from(document.getElementsByTagName('video'));
}

static pause() {
oneOfTheVideos.getVideosArray()
.filter((player) => !player.paused)
.forEach((player) => {
player.pause();
});
}

static play() {
oneOfTheVideos.getVideosArray()
.filter((player) => player.paused && player.played.length > 0)
.forEach((player) => {
player.play();
});
}

}
18 changes: 18 additions & 0 deletions src/content/Service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Base Service class */
export class Service {
constructor(options) {
this.options = options;
}

getStatus() {
return this.options.statusStrategy.getStatus();
}

play() {
this.options.controlStrategy.play();
}

pause() {
this.options.controlStrategy.pause();
}
}
27 changes: 27 additions & 0 deletions src/content/ServicesRegistry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Service } from './Service.js';
import * as StatusStrategies from './Status.Strategies.js';
import * as ControlStrategies from './Control.Strategies.js';

export const servicesRegistry = [
{
hosts: ['ted.com', 'facebook.com', 'kickstarter.com', 'music.youtube.com' ], // we can add support fields, like `host: string`, `hosts: Array<string>`
options: {
statusStrategy: StatusStrategies.oneOfTheVideosPlaying,
controlStrategy: ControlStrategies.oneOfTheVideos
}
},
];

export function getService(domain) {
const matchedService = servicesRegistry.find(serviceConfig => {
return serviceConfig.host === domain
|| serviceConfig.hosts.includes(domain)
|| serviceConfig.hostPattern.match(domain);
});

if (!matchedService) {
return;
}

return new Service(matchedService.options);
}
18 changes: 18 additions & 0 deletions src/content/Status.Strategies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Status } from './Status.Types.js';

export class BaseStatusStrategy {
static getStatus() {}
}

/* A shared mixin for when there is a video tag on page */
export class oneOfTheVideosPlaying extends BaseStatusStrategy {
static getStatus() {
let status = Status.PAUSED;
const videos = document.getElementsByTagName("video");
if (videos.length > 0) {
const hasPlayingVideo = Array.from(videos).some((player) => !player.paused);
status = hasPlayingVideo ? Status.PLAYING : Status.PAUSED;
}
return status;
}
}
28 changes: 27 additions & 1 deletion src/content/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* StoPlay Content JS */
import { CheckTimer } from './CheckTimer.js';
import { ProviderCheckStatus } from './ProviderCheckStatus.js';
import { getService } from './ServicesRegistry.js';
import { Status } from './Status.Types.js';

function safeGetElementTextContentByQuery(query) {
Expand Down Expand Up @@ -45,6 +46,7 @@ class Provider {
this.customLastPauseSelector = null;

this.checkStatus = new ProviderCheckStatus();
this.service = null;

chrome.storage.sync.get({
enabled: true,
Expand All @@ -54,7 +56,13 @@ class Provider {
this.timer = new CheckTimer({
delay: CHECK_TIMEOUT,
callback: () => {
let status = this.checkStatus.check();
let status;
if (this.service) {
status = this.service.getStatus();
console.log('service getstatus', status);
} else {
status = this.checkStatus.check();
}
this.__changeState(status);
},
recursive: true
Expand Down Expand Up @@ -161,6 +169,9 @@ class Provider {
clearSubDomains = "bandcamp.com";
}
if (clearSubDomains) this.host = clearSubDomains;

this.service = getService(this.host);

return this.host;
}

Expand Down Expand Up @@ -277,6 +288,13 @@ class Provider {
let p, selector, selectorQuery, playerPauseButton;

if (this.status === Status.PLAYING) {
if (this.service) {
this.service.pause();
// #TODO: remove duplication for POC
this.__changeState(Status.PAUSED);
return;
}

switch(this.host) {
case "radiolist.com.ua":
if (this.customLastPlayerSelector) {
Expand Down Expand Up @@ -549,6 +567,14 @@ class Provider {
let p, selector, selectorQuery, playerPlayButton;

if (this.status !== Status.PLAYING) {
if (this.service) {
this.service.play();
// #TODO: remove duplication for POC
this.__changeState(Status.PLAYING);
return;

}

switch(this.host) {
case "radiolist.com.ua":
if (this.customLastPlayerSelector) {
Expand Down