From 3779a27f6745c7dda2309237c09ebcc70f9a3a34 Mon Sep 17 00:00:00 2001 From: svetlagasheva Date: Fri, 3 Apr 2020 16:24:41 +0300 Subject: [PATCH 1/5] fb script moved to Intro --- src/index.html | 11 ----------- src/routes/Intro/Intro.js | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/index.html b/src/index.html index 38eb57d1c..67b70ad11 100755 --- a/src/index.html +++ b/src/index.html @@ -13,17 +13,6 @@
- - diff --git a/src/routes/Intro/Intro.js b/src/routes/Intro/Intro.js index 320d80988..333730e9e 100644 --- a/src/routes/Intro/Intro.js +++ b/src/routes/Intro/Intro.js @@ -272,6 +272,23 @@ const Intro = ({ queryParams }) => { core.off('Event', onEvent); }; }, [routeFocused]); + React.useEffect(() => { + var scriptElementFunction = document.createElement('script'); + var scriptElement = document.createElement('script'); + scriptElementFunction.innerHTML = `window.fbAsyncInit = function() { + FB.init({ + appId: '1537119779906825', + autoLogAppEvents: false, + xfbml: false, + version: 'v2.5' + }); + };`; + scriptElement.src = "https://connect.facebook.net/en_US/sdk.js"; + scriptElement.async = true; + scriptElement.defer = true; + document.body.appendChild(scriptElementFunction); + document.body.appendChild(scriptElement); + }, []); return (
From bdb91cb40e2cdb18830ee39001d0a32437477427 Mon Sep 17 00:00:00 2001 From: svetlagasheva Date: Fri, 3 Apr 2020 16:29:01 +0300 Subject: [PATCH 2/5] Intro scriptElement uses single quotes --- src/routes/Intro/Intro.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/Intro/Intro.js b/src/routes/Intro/Intro.js index 333730e9e..a87c72d2b 100644 --- a/src/routes/Intro/Intro.js +++ b/src/routes/Intro/Intro.js @@ -283,7 +283,7 @@ const Intro = ({ queryParams }) => { version: 'v2.5' }); };`; - scriptElement.src = "https://connect.facebook.net/en_US/sdk.js"; + scriptElement.src = 'https://connect.facebook.net/en_US/sdk.js'; scriptElement.async = true; scriptElement.defer = true; document.body.appendChild(scriptElementFunction); From b7b9f2f0594a9c11b756b005169dcddaf5f4f790 Mon Sep 17 00:00:00 2001 From: svetlagasheva Date: Fri, 3 Apr 2020 16:47:00 +0300 Subject: [PATCH 3/5] Intro script elements renamed --- src/routes/Intro/Intro.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/routes/Intro/Intro.js b/src/routes/Intro/Intro.js index a87c72d2b..01f2052ba 100644 --- a/src/routes/Intro/Intro.js +++ b/src/routes/Intro/Intro.js @@ -273,9 +273,9 @@ const Intro = ({ queryParams }) => { }; }, [routeFocused]); React.useEffect(() => { - var scriptElementFunction = document.createElement('script'); - var scriptElement = document.createElement('script'); - scriptElementFunction.innerHTML = `window.fbAsyncInit = function() { + var initScriptElement = document.createElement('script'); + var sdkScriptElement = document.createElement('script'); + initScriptElement.innerHTML = `window.fbAsyncInit = function() { FB.init({ appId: '1537119779906825', autoLogAppEvents: false, @@ -283,11 +283,11 @@ const Intro = ({ queryParams }) => { version: 'v2.5' }); };`; - scriptElement.src = 'https://connect.facebook.net/en_US/sdk.js'; - scriptElement.async = true; - scriptElement.defer = true; - document.body.appendChild(scriptElementFunction); - document.body.appendChild(scriptElement); + sdkScriptElement.src = 'https://connect.facebook.net/en_US/sdk.js'; + sdkScriptElement.async = true; + sdkScriptElement.defer = true; + document.body.appendChild(initScriptElement); + document.body.appendChild(sdkScriptElement); }, []); return (
From 7729c3ae0f9d41f5bdf12626321e1c621f847550 Mon Sep 17 00:00:00 2001 From: svetlagasheva Date: Fri, 3 Apr 2020 16:58:47 +0300 Subject: [PATCH 4/5] Intro scripts removed on unmount --- src/routes/Intro/Intro.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/routes/Intro/Intro.js b/src/routes/Intro/Intro.js index 01f2052ba..c66922272 100644 --- a/src/routes/Intro/Intro.js +++ b/src/routes/Intro/Intro.js @@ -288,6 +288,10 @@ const Intro = ({ queryParams }) => { sdkScriptElement.defer = true; document.body.appendChild(initScriptElement); document.body.appendChild(sdkScriptElement); + return () => { + document.body.removeChild(initScriptElement); + document.body.removeChild(sdkScriptElement); + }; }, []); return (
From d2dad4dd85cba1a70eff3fb326c250a52687821c Mon Sep 17 00:00:00 2001 From: svetlagasheva Date: Fri, 3 Apr 2020 17:27:02 +0300 Subject: [PATCH 5/5] check if FB is defined --- src/routes/Intro/Intro.js | 64 ++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/src/routes/Intro/Intro.js b/src/routes/Intro/Intro.js index c66922272..f9e5ebc24 100644 --- a/src/routes/Intro/Intro.js +++ b/src/routes/Intro/Intro.js @@ -75,39 +75,41 @@ const Intro = ({ queryParams }) => { } ); const loginWithFacebook = React.useCallback(() => { - FB.login((response) => { - if (response.status === 'connected') { - fetch('https://www.strem.io/fb-login-with-token/' + encodeURIComponent(response.authResponse.accessToken), { timeout: 10 * 60 * 1000 }) - .then((resp) => { - if (resp.status < 200 || resp.status >= 300) { - throw new Error('Login failed at getting token from Stremio with status ' + resp.status); - } else { - return resp.json(); - } - }) - .then(({ user }) => { - if (!user || typeof user.fbLoginToken !== 'string' || typeof user.email !== 'string') { - throw new Error('Login failed at getting token from Stremio'); - } - core.dispatch({ - action: 'Ctx', - args: { - action: 'Authenticate', - args: { - type: 'Login', - email: user.email, - password: user.fbLoginToken - } + if (typeof FB !== 'undefined') { + FB.login((response) => { + if (response.status === 'connected') { + fetch('https://www.strem.io/fb-login-with-token/' + encodeURIComponent(response.authResponse.accessToken), { timeout: 10 * 60 * 1000 }) + .then((resp) => { + if (resp.status < 200 || resp.status >= 300) { + throw new Error('Login failed at getting token from Stremio with status ' + resp.status); + } else { + return resp.json(); } + }) + .then(({ user }) => { + if (!user || typeof user.fbLoginToken !== 'string' || typeof user.email !== 'string') { + throw new Error('Login failed at getting token from Stremio'); + } + core.dispatch({ + action: 'Ctx', + args: { + action: 'Authenticate', + args: { + type: 'Login', + email: user.email, + password: user.fbLoginToken + } + } + }); + }) + .catch((err = {}) => { + dispatch({ type: 'error', error: err.message || JSON.stringify(err) }); }); - }) - .catch((err = {}) => { - dispatch({ type: 'error', error: err.message || JSON.stringify(err) }); - }); - } else { - dispatch({ type: 'error', error: 'Login failed at getting token from Facebook' }); - } - }); + } else { + dispatch({ type: 'error', error: 'Login failed at getting token from Facebook' }); + } + }); + } }, []); const loginWithEmail = React.useCallback(() => { if (typeof state.email !== 'string' || state.email.length === 0 || !emailRef.current.validity.valid) {