Skip to content
This repository has been archived by the owner on May 3, 2019. It is now read-only.

Commit

Permalink
Fix most mixcloud plugin issues
Browse files Browse the repository at this point in the history
SeekToPosition does not work anymore but the rest is OK

See #108
  • Loading branch information
ColinDuquesnoy committed Aug 28, 2017
1 parent e2261a0 commit 258e548
Showing 1 changed file with 200 additions and 65 deletions.
265 changes: 200 additions & 65 deletions plugins/mixcloud/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,104 +16,239 @@
// along with MellowPlayer. If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
// Updates service information
function getMixcloudPlayer() {
return $(document.querySelector('.ng-scope[ng-controller="PlayerQueueCtrl"]')).scope();
function getHashCode(s) {
return s.split("").reduce(function(a, b) {
a = ((a << 5) - a) + b.charCodeAt(0);
return a & a
}, 0);
}

function update() {
// hide header ad
$("body > div.cf > div:nth-child(1)").hide();
function getButtons() {
return {
'play': document.querySelector('div.player-control > span.playing'),
'pause': document.querySelector('div.player-control > span.pause'),
'loading': document.querySelector('div.player-control > span.loading')
};
}

function isElementVisible(element) {
try {
return element.offsetWidth > 0 && element.offsetHeight > 0;
}
catch (e) {
return false;
}
}

// Get a reference to the mixcloud's player queue controller
var M = getMixcloudPlayer();
function getCloudcastQueue() {
var queue = [].slice.call(document.querySelector("div.up-next-area").children);
// first item is not a cloudcast
queue.shift();

// Get next/previous cloudcasts
var currentIndex = M.playerQueue.queue.getNowPlayingIndex();
var previous = M.playerQueue.queue.cloudcastList.get(currentIndex - 1);
var cloudcast = M.player.currentCloudcast;
return queue;
}

var playback_status = mellowplayer.PlaybackStatus.STOPPED;
function getCurrentCloudcastIndex() {
var cloudcastQueue = getCloudcastQueue();

if (M.player.buffering) {
playback_status = mellowplayer.PlaybackStatus.BUFFERING;
} else if (M.player.playing) {
playback_status = mellowplayer.PlaybackStatus.PLAYING;
} else if (cloudcast.title) {
playback_status = mellowplayer.PlaybackStatus.PAUSED;
for (var index = 0; index < cloudcastQueue.length; index++) {
if (cloudcastQueue[index].className.endsWith("now-playing"))
return index;
}

return -1;
}

function getNextCloudcast() {
var cloudcastQueue = getCloudcastQueue();
var currentIndex = getCurrentCloudcastIndex();
var nextIndex = currentIndex + 1;

if (nextIndex < cloudcastQueue.length)
return cloudcastQueue[nextIndex];
return null;
}

function getPrecedingCloudcast() {
var cloudcastQueue = getCloudcastQueue();
var precedingIndex = getCurrentCloudcastIndex() - 1;

if (precedingIndex >= 0)
return cloudcastQueue[precedingIndex];
return null;
}

function toMilliseconds(string) {
try {
var dtimes = string.split(":");

if (dtimes.length === 3) {
var dhours = dtimes[0];
var dminutes = dtimes[1];
var dseconds = dtimes[2];
var duration = parseInt(dseconds, 10) + (parseInt(dminutes, 10) * 60) + (parseInt(dhours, 10) * 60 * 60);
}
else {
var dminutes = dtimes[0];
var dseconds = dtimes[1];
var duration = parseInt(dseconds, 10) + (parseInt(dminutes, 10) * 60);
}

} catch (e) {
var duration = 0;
}

return duration
}

function getPlaybackStatus() {
var buffering = isElementVisible(getButtons().loading);
var paused = isElementVisible(getButtons().play);
var playing = isElementVisible(getButtons().pause);
var playbackStatus = mellowplayer.PlaybackStatus.STOPPED;
if (buffering) {
playbackStatus = mellowplayer.PlaybackStatus.BUFFERING;
}
else if (playing) {
playbackStatus = mellowplayer.PlaybackStatus.PLAYING;
}
else if (paused) {
playbackStatus = mellowplayer.PlaybackStatus.PAUSED;
}
return playbackStatus;
}

function getCurrentCloudcastTitle() {
try {
return document.querySelector("a.player-cloudcast-title").innerText
}
catch(e) {
return ""
}
}

function getArtistName() {
try {
return document.querySelector("a.player-cloudcast-author-link").innerText;
}
catch (e) {
return "";
}
}

function getPosition() {
try {
return toMilliseconds(document.querySelector("div.player-time").innerText);
}
catch(e) {
return 0;
}
}

function getDuration() {
var remaining = 0;
try {
var remainString = document.querySelector("div.end-time").innerText.replace("-", "");
remaining = toMilliseconds(remainString);
}
catch(e) {
}

return getPosition() + remaining;
}

function getAlbumArt() {
try {
return document.querySelector("div.player-cloudcast-image").children[0].src;
}
catch (e) {
return "";
}
}

function isFavorite() {
var favoriteToolTip = document.querySelector("span.player-icons.favorite").children[0];
var tooltipValue = favoriteToolTip.attributes["data-tooltip"].value;
return tooltipValue !== "Favorite";
}

function update() {
return {
"playbackStatus": playback_status,
"playbackStatus": getPlaybackStatus(),
"canSeek": true,
"canGoNext": true,
"canGoPrevious": typeof previous !== "undefined",
"canAddToFavorites": false,
"volume": M.player.volume,
"duration": M.player.audioLength,
"position": M.player.audioPosition,
"songId": cloudcast.id,
"songTitle": cloudcast.title,
"artistName": cloudcast.owner,
"canGoNext": getNextCloudcast() !== null,
"canGoPrevious": getPrecedingCloudcast() !== null,
"canAddToFavorites": true,
"volume": 1,
"duration": getDuration(),
"position": getPosition(),
"songId": getHashCode(getCurrentCloudcastTitle()),
"songTitle": getCurrentCloudcastTitle(),
"artistName": getArtistName(),
"albumTitle": '',
"artUrl": M.player.currentCloudcast.widgetImage,
"isFavorite": false
"artUrl": getAlbumArt(),
"isFavorite": isFavorite()
}
}

function play() {
// Get a reference to the mixcloud's player queue controller
var M = getMixcloudPlayer();
M.player.togglePlayClick();
getButtons().play.click();
}

function pause() {
// Get a reference to the mixcloud's player queue controller
var M = $(document.querySelector(
'.ng-scope[ng-controller="PlayerQueueCtrl"]')).scope();
M.player.togglePlayClick();
getButtons().pause.click();
}

function goNext() {
// Get a reference to the mixcloud's player queue controller
var M = getMixcloudPlayer();
var currentIndex = M.playerQueue.queue.getNowPlayingIndex();
var next = M.playerQueue.queue.cloudcastList.get(currentIndex + 1);
if (typeof next !== "undefined") {
M.playerQueue.playFromQueue(next);
} else {
M.playerQueue.playUpNext();
}
getNextCloudcast().children[0].click();
}

function goPrevious() {
// Get a reference to the mixcloud's player queue controller
var M = getMixcloudPlayer();
var currentIndex = M.playerQueue.queue.getNowPlayingIndex();
var previous = M.playerQueue.queue.cloudcastList.get(currentIndex + -1);
if (typeof previous !== "undefined") {
M.playerQueue.playFromQueue(previous);
}
getPrecedingCloudcast().children[0].click();
}

function setVolume(volume) {
// Get a reference to the mixcloud's player queue controller
var M = getMixcloudPlayer();
M.player.volume = volume;

}

function addToFavorites() {
// simulate a click on favorite button
$('.icon-favorite-inner').get(0).click()
document.querySelector("span.player-icons.favorite").click();
}

function removeFromFavorites() {
// simulate a click on favorite button
$('.icon-favorite-inner').get(0).click()
document.querySelector("span.player-icons.favorite").click();
}

function click(x, y) {
// var mouseDownEvent = new MouseEvent('mousedown', {
// 'view': window,
// 'bubbles': true,
// 'cancelable': true,
// 'screenX': x,
// 'screenY': y
// });
//
// var mouseUpEvent = new MouseEvent('mouseup', {
// 'view': window,
// 'bubbles': true,
// 'cancelable': true,
// 'screenX': x,
// 'screenY': y
// });
//
// var el = document.elementFromPoint(x, y);
// console.warn(el.className);
//
// el.dispatchEvent(mouseDownEvent);
// el.dispatchEvent(mouseUpEvent);
}

function seekToPosition(position) {
// Get a reference to the mixcloud's player queue controller
var M = getMixcloudPlayer();
M.$emit("slider:stop", position)
}
// var seekArea = document.querySelector("div.player-scrubber-buffered");
// var seekAreaRect = seekArea.getBoundingClientRect();
//
// console.warn(seekAreaRect.top + seekAreaRect.height / 2);
// console.warn(seekAreaRect.left + seekAreaRect.width / 2);
//
// click(seekAreaRect.left + seekAreaRect.width / 2, seekAreaRect.top + seekAreaRect.height / 2)
}

0 comments on commit 258e548

Please sign in to comment.