From db6010da5fc35bbc25a08c8fc665b4decb363ffa Mon Sep 17 00:00:00 2001 From: Panagiotis Date: Sat, 6 Feb 2016 21:23:02 +0200 Subject: [PATCH 1/3] add try catch we don't need direct call to oauthCallback if running on cordova browser platform --- oauthcallback.html | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/oauthcallback.html b/oauthcallback.html index 1d2e3c9..796cd04 100644 --- a/oauthcallback.html +++ b/oauthcallback.html @@ -1,8 +1,10 @@ - \ No newline at end of file + From ba9b287819c350a6ecd7f5306525c14a0a699edb Mon Sep 17 00:00:00 2001 From: Panagiotis Date: Sat, 6 Feb 2016 21:30:23 +0200 Subject: [PATCH 2/3] make it compatible with cordova browser platform There many issues on cordova browser platform with inappbrowser wich is implemented via iframe, and loadstart and loaderror events are not being fired. So whe change to loadstop as suggested from @jamesdickinson and if running on browser platform we open window on _system. --- openfb.js | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/openfb.js b/openfb.js index 864983c..8bfcf04 100644 --- a/openfb.js +++ b/openfb.js @@ -37,7 +37,10 @@ var openFB = (function () { loginCallback, // Indicates if the app is running inside Cordova - runningInCordova, + runningInCordova = false, + + // Indicates if the app is runniong on browser platform + runningCordovaBrowser = false, // Used in the exit event handler to identify if the login has already been processed elsewhere (in the oauthCallback function) loginProcessed; @@ -46,7 +49,11 @@ var openFB = (function () { // You don't need to (and should not) add the actual cordova.js file to your file system: it will be added automatically // by the Cordova build process document.addEventListener("deviceready", function () { - runningInCordova = true; + if(window.cordova.platformId=="browser"){ + runningCordovaBrowser=true; + cordovaOAuthRedirectURL = oauthRedirectURL; + } + runningInCordova = true; }, false); /** @@ -83,7 +90,7 @@ var openFB = (function () { oauthRedirectURL = params.oauthRedirectURL || oauthRedirectURL; cordovaOAuthRedirectURL = params.cordovaOAuthRedirectURL || cordovaOAuthRedirectURL; logoutRedirectURL = params.logoutRedirectURL || logoutRedirectURL; - + runningInCordova = params.runningInCordova || runningInCordova; } /** @@ -122,9 +129,13 @@ var openFB = (function () { return callback({status: 'unknown', error: 'Facebook App Id not set.'}); } - // Inappbrowser load start handler: Used when running in Cordova only - function loginWindow_loadStartHandler(event) { + // Inappbrowser load stop handler: Used when running in Cordova only + // inAppBrowser browser platform not fires loadstart event + function loginWindow_loadStopHandler(event) { var url = event.url; + //url may is Location object so check and convert to string + if (url !== null && typeof url === 'object') url = url.toString(); + if (url.indexOf("access_token=") > 0 || url.indexOf("error=") > 0) { // When we get the access token fast, the login window (inappbrowser) is still opening with animation // in the Cordova app, and trying to close it while it's animating generates an exception. Wait a little... @@ -155,12 +166,18 @@ var openFB = (function () { loginProcessed = false; startTime = new Date().getTime(); + + if (runningCordovaBrowser==true) + loginWindow = window.open(loginURL + '?client_id=' + fbAppId + '&redirect_uri=' + redirectURL + + '&response_type=token&scope=' + scope, '_system', 'location=no,clearcache=yes'); + else loginWindow = window.open(loginURL + '?client_id=' + fbAppId + '&redirect_uri=' + redirectURL + - '&response_type=token&scope=' + scope, '_blank', 'location=no,clearcache=yes'); + '&response_type=token&scope=' + scope, '_blank', 'location=no,clearcache=yes'); + // If the app is running in Cordova, listen to URL changes in the InAppBrowser until we get a URL with an access_token or an error if (runningInCordova) { - loginWindow.addEventListener('loadstart', loginWindow_loadStartHandler); + loginWindow.addEventListener('loadstop', loginWindow_loadStopHandler); loginWindow.addEventListener('exit', loginWindow_exitHandler); } // Note: if the app is running in the browser the loginWindow dialog will call back by invoking the @@ -204,8 +221,7 @@ var openFB = (function () { token = tokenStore.fbAccessToken; /* Remove token. Will fail silently if does not exist */ - tokenStore.removeItem('fbtoken'); - + tokenStore.removeItem('fbAccessToken'); if (token) { logoutWindow = window.open(logoutURL + '?access_token=' + token + '&next=' + logoutRedirectURL, '_blank', 'location=no,clearcache=yes'); if (runningInCordova) { From 7ee20762dd9dccc629974fe6c3e80720158b92c6 Mon Sep 17 00:00:00 2001 From: Panagiotis Date: Sat, 6 Feb 2016 22:23:29 +0200 Subject: [PATCH 3/3] revert to loadstart for android,ios --- openfb.js | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/openfb.js b/openfb.js index 8bfcf04..b89249f 100644 --- a/openfb.js +++ b/openfb.js @@ -38,7 +38,8 @@ var openFB = (function () { // Indicates if the app is running inside Cordova runningInCordova = false, - + + // Indicates if the app is runniong on browser platform runningCordovaBrowser = false, @@ -147,12 +148,32 @@ var openFB = (function () { } } + // Inappbrowser load start handler: Used when running in Cordova only + function loginWindow_loadStartHandler(event) { + var url = event.url; + if (url.indexOf("access_token=") > 0 || url.indexOf("error=") > 0) { + // When we get the access token fast, the login window (inappbrowser) is still opening with animation + // in the Cordova app, and trying to close it while it's animating generates an exception. Wait a little... + var timeout = 600 - (new Date().getTime() - startTime); + setTimeout(function () { + loginWindow.close(); + }, timeout > 0 ? timeout : 0); + oauthCallback(url); + } + } + + // Inappbrowser exit handler: Used when running in Cordova only function loginWindow_exitHandler() { console.log('exit and remove listeners'); // Handle the situation where the user closes the login window manually before completing the login process if (loginCallback && !loginProcessed) loginCallback({status: 'user_cancelled'}); - loginWindow.removeEventListener('loadstop', loginWindow_loadStopHandler); + + if (runningCordovaBrowser==true) + loginWindow.removeEventListener('loadstop', loginWindow_loadStopHandler); + else + loginWindow.removeEventListener('loadstart', loginWindow_loadStartHandler); + loginWindow.removeEventListener('exit', loginWindow_exitHandler); loginWindow = null; console.log('done removing listeners'); @@ -177,7 +198,11 @@ var openFB = (function () { // If the app is running in Cordova, listen to URL changes in the InAppBrowser until we get a URL with an access_token or an error if (runningInCordova) { - loginWindow.addEventListener('loadstop', loginWindow_loadStopHandler); + if (runningCordovaBrowser==true) + loginWindow.addEventListener('loadstop', loginWindow_loadStopHandler); + else + loginWindow.addEventListener('loadstart', loginWindow_loadStartHandler); + loginWindow.addEventListener('exit', loginWindow_exitHandler); } // Note: if the app is running in the browser the loginWindow dialog will call back by invoking the