@@ -6,76 +6,60 @@ export async function init() {
66 } else if ( location . hostname === "localhost" || location . hostname === "127.0.0.1" ) {
77 return Promise . resolve ( ) ;
88 }
9- // return Chromecast.init();
9+ return Chromecast . init ( ) ;
1010}
1111
12- // import { Session } from "./session";
13- // import { currentShare, objectGet } from "../helpers/";
12+ export const Chromecast = new class ChromecastManager {
13+ init ( ) {
14+ return new Promise ( ( resolve ) => {
15+ const script = document . createElement ( "script" ) ;
16+ script . src = "https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1" ;
17+ script . onerror = ( ) => resolve ( null ) ;
18+ window [ "__onGCastApiAvailable" ] = function ( isAvailable ) {
19+ if ( isAvailable ) window . cast . framework . CastContext . getInstance ( ) . setOptions ( {
20+ receiverApplicationId : window . chrome . cast . media . DEFAULT_MEDIA_RECEIVER_APP_ID ,
21+ autoJoinPolicy : window . chrome . cast . AutoJoinPolicy . ORIGIN_SCOPED ,
22+ } ) ;
23+ resolve ( null ) ;
24+ } ;
25+ document . head . appendChild ( script ) ;
26+ } ) ;
27+ }
1428
15- // class ChromecastManager {
16- // init() {
17- // return new Promise((done) => {
18- // const script = document.createElement("script");
19- // script.src = "https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1";
20- // script.onerror = () => done()
21- // window["__onGCastApiAvailable"] = function(isAvailable) {
22- // if (isAvailable) cast.framework.CastContext.getInstance().setOptions({
23- // receiverApplicationId: chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID,
24- // autoJoinPolicy: chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED,
25- // });
26- // done();
27- // };
28- // document.head.appendChild(script)
29- // });
30- // }
29+ createLink ( apiPath ) {
30+ const target = new URL ( location . origin + apiPath ) ;
31+ const shareID = new URLSearchParams ( location . search ) . get ( "search" ) ;
32+ if ( shareID ) target . searchParams . append ( "share" , shareID ) ;
33+ return target . toString ( ) ;
34+ }
3135
32- // origin() {
33- // return location.origin;
34- // };
36+ createRequest ( mediaInfo ) {
37+ if ( ! window . BEARER_TOKEN ) throw new Error ( "Invalid account" ) ;
38+ // TODO: it would be much much nicer to set the authorization from an HTTP header
39+ // but this would require to create a custom web receiver app, setup accounts on
40+ // google, etc,... Until that happens, we're setting the authorization within the
41+ // url. Once we have that app, the authorisation will come from a customData field
42+ // of a chrome.cast.media.LoadRequest
43+ const target = new URL ( mediaInfo . contentId ) ;
44+ target . searchParams . append ( "authorization" , window . BEARER_TOKEN ) ;
45+ mediaInfo . contentId = target . toString ( ) ;
46+ return new window . chrome . cast . media . LoadRequest ( mediaInfo ) ;
47+ }
3548
36- // createLink(apiPath) {
37- // const shareID = currentShare();
38- // if (shareID) {
39- // const target = new URL(this.origin() + apiPath);
40- // target.searchParams.append("share", shareID);
41- // return target.toString();
42- // }
43- // const target = new URL(this.origin() + apiPath)
44- // return target.toString();
45- // }
49+ context ( ) {
50+ if ( ! window . chrome ?. cast ?. isAvailable ) return ;
51+ return window . cast . framework . CastContext . getInstance ( ) ;
52+ }
4653
47- // createRequest(mediaInfo) {
48- // let prior = Promise.resolve();
49- // if (!Session.authorization) prior = Session.currentUser();
50- // return prior.then(() => {
51- // if (!Session.authorization) throw new Error("Invalid account");
52- // // TODO: it would be much much nicer to set the authorization from an HTTP header
53- // // but this would require to create a custom web receiver app, setup accounts on
54- // // google, etc,... Until that happens, we're setting the authorization within the
55- // // url. Once we have that app, the authorisation will come from a customData field
56- // // of a chrome.cast.media.LoadRequest
57- // const target = new URL(mediaInfo.contentId);
58- // target.searchParams.append("authorization", Session.authorization);
59- // mediaInfo.contentId = target.toString();
60- // return new chrome.cast.media.LoadRequest(mediaInfo);
61- // });
62- // }
54+ session ( ) {
55+ const context = this . context ( ) ;
56+ if ( ! context ) return ;
57+ return context . getCurrentSession ( ) ;
58+ }
6359
64- // context() {
65- // if (!objectGet(window.chrome, ["cast", "isAvailable"])) {
66- // return;
67- // }
68- // return cast.framework.CastContext.getInstance();
69- // }
70- // session() {
71- // const context = this.context();
72- // if (!context) return;
73- // return context.getCurrentSession();
74- // }
75- // media() {
76- // const session = this.session();
77- // if (!session) return;
78- // return session.getMediaSession();
79- // }
80- // }
81- // export const Chromecast = new ChromecastManager();
60+ media ( ) {
61+ const session = this . session ( ) ;
62+ if ( ! session ) return ;
63+ return session . getMediaSession ( ) ;
64+ }
65+ } ( ) ;
0 commit comments