Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Commit

Permalink
Fix push state and session issues
Browse files Browse the repository at this point in the history
  • Loading branch information
maherbeg committed Jul 20, 2014
1 parent 145ec48 commit 2c104ba
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions static/js/chromecast.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
var noop = function() {};

var currentSession;
var mediaUpdateIntervalId;

var extractMediaFileNameFromPath = function(streamingPath) {
if (!streamingPath) { return; }

var streamStringStartIndex = streamingPath.lastIndexOf('/');
if (streamStringStartIndex !== -1) {
streamingPath = streamingPath.slice(streamStringStartIndex + 1);
Expand Down Expand Up @@ -30,9 +33,9 @@ var loadMedia = function(target, session) {
document.getElementById('now-playing').innerText = 'Loading ' + mediaName;
mediaUpdateIntervalId = setInterval(onMediaUpdate.bind(null, session), 1000);

var currentLocation = window.location;
var currentLocation = window.location.href;
var currentTitle = window.title;
window.history.pushState(null, mediaName, mediaPath);
window.history.replaceState(null, mediaName, mediaPath);
window.history.replaceState(null, currentTitle, currentLocation);
};

Expand Down Expand Up @@ -63,22 +66,35 @@ var onMediaUpdate = function(joinedSession) {
};

var streamingMediaHandler = function(e) {
var target = e.target.parentElement;
var target = e.target;

if (!target || target.tagName.toUpperCase() !== 'A') {
target = target.parentElement;

if (!target || target.tagName.toUpperCase() !== 'A') { return; }
if (!target || target.tagName.toUpperCase() !== 'A') {
return;
}
}

if (target.href.indexOf('/stream/') === -1) { return; }

chrome.cast.requestSession(loadMedia.bind(null, target), noop);
if (currentSession) {
loadMedia(target, currentSession);
} else {
chrome.cast.requestSession(loadMedia.bind(null, target), noop);
}

e.preventDefault();
};


var onSessionJoined = function(joinedSession) {
currentSession = joinedSession
mediaUpdateIntervalId = setInterval(onMediaUpdate.bind(null, joinedSession), 1000);
};

document.addEventListener('click', noop);

window['__onGCastApiAvailable'] = function(loaded, errorInfo) {
if (!loaded) { return console.log(errorInfo); }

Expand All @@ -88,5 +104,6 @@ window['__onGCastApiAvailable'] = function(loaded, errorInfo) {

chrome.cast.initialize(apiConfig, noop, noop);

document.removeEventListener('click', noop);
document.addEventListener('click', streamingMediaHandler, false);
};

0 comments on commit 2c104ba

Please sign in to comment.