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

Commit

Permalink
Fix for Chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelgssa committed Sep 28, 2019
1 parent 7f58e6a commit f20afd7
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 30 deletions.
6 changes: 3 additions & 3 deletions src/class/ItemParser.js
Expand Up @@ -10,10 +10,10 @@ class ItemParser {
return location.href;
}

isReady() {
async isReady() {
let isReady = false;

const session = NetflixApiUtils.getSession();
const session = await NetflixApiUtils.getSession();

if (session) {
this.id = session.videoId.toString();
Expand All @@ -33,7 +33,7 @@ class ItemParser {
}

async checkIsReady(resolve) {
if (this.isReady()) {
if (await this.isReady()) {
const data = await NetflixApiUtils.getMetadata(this.id);
resolve(data);
} else {
Expand Down
46 changes: 33 additions & 13 deletions src/class/NetflixApiUtils.js
Expand Up @@ -255,25 +255,45 @@ const netflixApiUtils = {
}
},

_getSession(resolve, event) {
window.removeEventListener('traktflix-getSession-fromPage', this.sessionListener);

const session = JSON.parse(event.detail.session);

resolve(session);
},

getSession() {
const netflix = window.netflix || (window.wrappedJSObject && window.wrappedJSObject.netflix);
return new Promise(resolve => {
if (window.wrappedJSObject) {
const netflix = window.wrappedJSObject.netflix;

if (netflix) {
const sessions = netflix.appContext.state.playerApp.getState().videoPlayer.playbackStateBySessionId;
const key = Object.keys(sessions).filter(key => key.match(/^watch/))[0];
if (netflix) {
const sessions = netflix.appContext.state.playerApp.getState().videoPlayer.playbackStateBySessionId;
const key = Object.keys(sessions).filter(key => key.match(/^watch/))[0];

let session = null;
let session = null;

if (key) {
session = Object.assign({}, sessions[key]);
}
if (key) {
session = Object.assign({}, sessions[key]);
}

if (window.wrappedJSObject) {
XPCNativeWrapper(window.wrappedJSObject.netflix);
}
XPCNativeWrapper(window.wrappedJSObject.netflix);

return session;
}
resolve(session);
} else {
resolve();
}
} else {
this.sessionListener = this._getSession.bind(this, resolve);

window.addEventListener('traktflix-getSession-fromPage', this.sessionListener, false);

const event = new CustomEvent('traktflix-getSession-toPage');

window.dispatchEvent(event);
}
});
},
};

Expand Down
2 changes: 1 addition & 1 deletion src/class/content/EventWatcher.js
Expand Up @@ -30,7 +30,7 @@ export default class EventWatcher {
}

async checkForChanges() {
const session = NetflixApiUtils.getSession();
const session = await NetflixApiUtils.getSession();

if (typeof session !== 'undefined') {
if (session) {
Expand Down
4 changes: 2 additions & 2 deletions src/class/content/Scrobble.js
Expand Up @@ -81,8 +81,8 @@ export default class Scrobble {
});
}

checkForChanges() {
const session = NetflixApiUtils.getSession();
async checkForChanges() {
const session = await NetflixApiUtils.getSession();

if (session) {
this.progress = Math.round((session.currentTime / session.duration) * 10000) / 100;
Expand Down
17 changes: 12 additions & 5 deletions src/class/popup/Header.js
Expand Up @@ -12,11 +12,18 @@ class Header extends React.Component {
const tabs = await browser.tabs.query({url: `*://*.netflix.com/*`, active: true});

if (tabs.length > 0) {
browser.tabs.create({
cookieStoreId: tabs[0].cookieStoreId,
index: tabs[0].index + 1,
url: browser.runtime.getURL('html/history-sync.html'),
});
if (browser.cookies) {
browser.tabs.create({
cookieStoreId: tabs[0].cookieStoreId,
index: tabs[0].index + 1,
url: browser.runtime.getURL('html/history-sync.html'),
});
} else {
browser.tabs.create({
index: tabs[0].index + 1,
url: browser.runtime.getURL('html/history-sync.html'),
});
}
}
}

Expand Down
71 changes: 65 additions & 6 deletions src/modules/content/index.js
Expand Up @@ -36,17 +36,76 @@ async function init() {
}
}

function _getApiDefs(event) {
window.removeEventListener('traktflix-getApiDefs-fromPage', getApiDefs);

const { authUrl, buildIdentifier } = event.detail;

if (authUrl && buildIdentifier) {
browser.runtime.sendMessage({ type: 'setApiDefs', authUrl, buildIdentifier });
}
}

function getApiDefs() {
const netflix = window.netflix || (window.wrappedJSObject && window.wrappedJSObject.netflix);
if (window.wrappedJSObject) {
const netflix = window.wrappedJSObject.netflix;

if (netflix) {
const authUrl = netflix.reactContext.models.userInfo.data.authURL;
const buildIdentifier = netflix.reactContext.models.serverDefs.data.BUILD_IDENTIFIER;
if (netflix) {
const authUrl = netflix.reactContext.models.userInfo.data.authURL;
const buildIdentifier = netflix.reactContext.models.serverDefs.data.BUILD_IDENTIFIER;

browser.runtime.sendMessage({type: `setApiDefs`, authUrl, buildIdentifier});
browser.runtime.sendMessage({ type: 'setApiDefs', authUrl, buildIdentifier });

if (window.wrappedJSObject) {
XPCNativeWrapper(window.wrappedJSObject.netflix);
}
} else {
const script = document.createElement('script');

script.textContent = `
window.addEventListener('traktflix-getApiDefs-toPage', () => {
let authUrl = '';
let buildIdentifier = '';
if (netflix) {
authUrl = netflix.reactContext.models.userInfo.data.authURL;
buildIdentifier = netflix.reactContext.models.serverDefs.data.BUILD_IDENTIFIER;
}
const event = new CustomEvent('traktflix-getApiDefs-fromPage', {
detail: { authUrl, buildIdentifier },
});
window.dispatchEvent(event);
});
window.addEventListener('traktflix-getSession-toPage', () => {
let session;
if (netflix) {
const sessions = netflix.appContext.state.playerApp.getState().videoPlayer.playbackStateBySessionId;
const key = Object.keys(sessions).filter(key => key.match(/^watch/))[0];
session = null;
if (key) {
session = sessions[key];
}
}
const event = new CustomEvent('traktflix-getSession-fromPage', {
detail: { session: JSON.stringify(session) },
});
window.dispatchEvent(event);
});
`;

document.body.appendChild(script);

window.addEventListener('traktflix-getApiDefs-fromPage', _getApiDefs, false);

const event = new CustomEvent('traktflix-getApiDefs-toPage');

window.dispatchEvent(event);
}
}

0 comments on commit f20afd7

Please sign in to comment.