Skip to content

Commit

Permalink
Merge pull request #212 from StoPlay/211-service-request-freemusicarc…
Browse files Browse the repository at this point in the history
…hiveorg

New service: freemusicarchive.org
  • Loading branch information
beshur authored Nov 1, 2022
2 parents 2ef5333 + 82f37c3 commit 4a27c81
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 23 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ At the moment we fully support (stop and play)
- beatport.com
- anchor.fm
- qub.ca
- cikava-ideya.top
- freemusicarchive.org

## How it works

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@
"grunt-webstore-upload": "^0.9.21",
"grunt-zip": "^0.18.2"
}
}
}
1 change: 1 addition & 0 deletions src/background/models/ProvidersList.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ export const ProvidersList = [
"cikava-ideya.top",
"tortuga.wtf",
"ashdi.vip",
"freemusicarchive.org",
];
20 changes: 20 additions & 0 deletions src/common/ElementClickControl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export class ElementClickControl {
#controlSelector;

/**
* playSelector here optional for cases when needed different one
*/
constructor(controlSelector) {
this.#controlSelector = controlSelector;
}

evaluate() {
const element = document.querySelector(this.#controlSelector);

if (!element) {
return;
}

element.click();
}
}
13 changes: 13 additions & 0 deletions src/common/ElementExistsStatus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Status } from "../background/models/Status";

export class ElementExistsStatus {
#selector;

constructor(selector) {
this.#selector = selector;
}

getStatus() {
return document.querySelector(this.#selector) ? Status.PLAYING : Status.PAUSED;
}
}
46 changes: 24 additions & 22 deletions src/content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import { CheckTimer } from "./CheckTimer.js";
import { Actions } from "../common/Actions.js";
import { NativeMediaPlayer } from "../common/NativeMediaPlayer";
import { ElementExistsStatus } from "../common/ElementExistsStatus.js";
import { ElementClickControl } from "../common/ElementClickControl.js";

function safeGetElementTextContentByQuery(query) {
try {
const element = document.querySelector(query);

return element.textContent;
return element.textContent.trim();
} catch (e) {
return "";
}
Expand Down Expand Up @@ -247,7 +249,7 @@ class Provider {

case "radio.garden":
artistName = safeGetElementTextContentByQuery(
".channel-list-item-name-container"
"[class*='channel'] [class*='titleContainer'] [class*='title']:not([class*='subtitle'])"
);
break;

Expand All @@ -260,6 +262,11 @@ class Provider {
"[data-testid='context-item-info-title'] [data-testid='context-item-link']"
);
break;

case "freemusicarchive.org":
songName = safeGetElementTextContentByQuery(".c-player__song .c-song__artist");
artistName = safeGetElementTextContentByQuery(".c-player__song .c-song__title");
break;
}

if (artistName && songName) {
Expand Down Expand Up @@ -343,6 +350,10 @@ class Provider {
status = new NativeMediaPlayer("video").status();
break;

case "freemusicarchive.org":
status = new ElementExistsStatus(".c-player__control-button--pause:not([style*='display: none'])").getStatus();
break;

case "last.fm":
status = document
.getElementById("webRadio")
Expand Down Expand Up @@ -495,10 +506,7 @@ class Provider {
break;

case "radio.garden":
selectorQuery = ".icon-toggle.mod-mute .icon-button.mod-sound";
playerPauseButton = document.querySelector(selectorQuery);

status = playerPauseButton ? Status.PLAYING : Status.PAUSED;
status = new ElementExistsStatus("[class*='controlsContainer'] [aria-label='stop']").getStatus();
break;

case "somafm.com":
Expand Down Expand Up @@ -558,6 +566,10 @@ class Provider {
new NativeMediaPlayer("video").pause();
break;

case "freemusicarchive.org":
new ElementClickControl(".c-player__control-button--pause:not([style*='display: none'])").evaluate();
break;

case "last.fm":
document.querySelector("#radioControlPause a") &&
document.querySelector("#radioControlPause a").click();
Expand Down Expand Up @@ -700,14 +712,7 @@ class Provider {
break;

case "radio.garden":
selectorQuery = ".icon-toggle.mod-mute .icon-button.mod-sound";
playerPauseButton = document.querySelector(selectorQuery);

if (!playerPauseButton) {
return;
}

playerPauseButton.click();
new ElementClickControl("[class*='controlsContainer'] [aria-label='stop']").evaluate();
break;

case "somafm.com":
Expand Down Expand Up @@ -775,6 +780,10 @@ class Provider {
new NativeMediaPlayer("video").play();
break;

case "freemusicarchive.org":
new ElementClickControl(".c-player__control-button--play:not([style*='display: none'])").evaluate();
break;

case "last.fm":
document.querySelector("#radioControlPlay a") &&
document.querySelector("#radioControlPlay a").click();
Expand Down Expand Up @@ -915,14 +924,7 @@ class Provider {
break;

case "radio.garden":
selectorQuery = ".icon-toggle.mod-mute .icon-button.mod-muted";
playerPlayButton = document.querySelector(selectorQuery);

if (!playerPlayButton) {
return;
}

playerPlayButton.click();
new ElementClickControl("[class*='controlsContainer'] [aria-label='play']").evaluate();
break;

case "somafm.com":
Expand Down

0 comments on commit 4a27c81

Please sign in to comment.