Skip to content

Commit

Permalink
Fix Youtube connector issues. Closes #757
Browse files Browse the repository at this point in the history
  • Loading branch information
david-sabata committed Oct 12, 2015
1 parent bc8f62b commit c469ad4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
40 changes: 38 additions & 2 deletions connectors/v2/youtube.js
Expand Up @@ -18,9 +18,31 @@ Connector.playerSelector = '#page';

Connector.artistTrackSelector = '#eow-title';

Connector.currentTimeSelector = '#player-api .ytp-time-current';
/**
* Because player can be still present in the page, we need to detect that it's invisible
* and don't return current time. Otherwise resulting state may not be considered empty.
*/
Connector.getCurrentTime = function() {
if (isPlayerOffscreen()) {
return null;
}

Connector.durationSelector = '#player-api .ytp-time-duration';
var $time = $('#player-api .ytp-time-current');
return this.stringToSeconds($time.text());
};

/**
* Because player can be still present in the page, we need to detect that it's invisible
* and don't return duration. Otherwise resulting state may not be considered empty.
*/
Connector.getDuration = function() {
if (isPlayerOffscreen()) {
return 0;
}

var $duration = $('#player-api .ytp-time-duration');
return this.stringToSeconds($duration.text());
};

Connector.getUniqueID = function() {
var url = window.location.href;
Expand Down Expand Up @@ -85,3 +107,17 @@ Connector.getArtistTrack = function () {

return {artist: artist, track: track};
};

/**
* YouTube doesn't really unload the player. It simply moves it outside viewport.
* That has to be checked, because our selectors are still able to detect it.
*/
function isPlayerOffscreen() {
var $player = $('#player-api');
if ($player.length === 0) {
return false;
}

var offset = $player.offset();
return offset.left < 0 || offset.top < 0;
}
2 changes: 1 addition & 1 deletion core/connectors.js
Expand Up @@ -37,7 +37,7 @@ define(function() {

{
label: 'YouTube',
matches: ['*://www.youtube.com/watch*', '*://www.youtube.com/user/*'],
matches: ['*://www.youtube.com/*'],
js: ['connectors/v2/youtube.js'],
version: 2
},
Expand Down

0 comments on commit c469ad4

Please sign in to comment.