Last active
February 14, 2018 05:51
-
-
Save rguruprakash/18a6603ab4bd440cc690b91f23154e25 to your computer and use it in GitHub Desktop.
Oauth Popup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Opens a popup with given url and this popup will be polled by parent window for 'value' property on its window object. | |
* Once the 'value' property is set to true, the parent window will close the popup and trigger the given callback function. | |
* @param {string} url - url for popup | |
* @param {number} [pollInterval] - Interval in which to poll the popup window. (Default: 1000ms) | |
*/ | |
function OauthPopup = function(url, pollInterval) { | |
this.url = url; | |
this.pollInterval = pollInterval || 1000; | |
} | |
/** | |
* @param {function} [callbackFn] - function to be triggered once the 'value' property of popup is set to true. | |
*/ | |
OauthPopup.prototype.showPopup = function(callbackFn) { | |
var st='toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,'; | |
var left = screen.width / 2 - 300, top = screen.height / 2 - 350; | |
if(this.popup) { | |
this.popup.close(); | |
} | |
this.popup = window.open(url, '', st+'top=' + top + ',left=' + left + ',width=600,height=700'); | |
if(this.popupInterval) { | |
clearInterval(this.popupInterval); | |
} | |
this.popupInterval = setInterval(function() { | |
try { | |
if (this.popup && (this.popup.closed || this.popup.value)) { | |
clearInterval(this.popupInterval); | |
if(!this.popup.closed) { | |
this.popup.close(); | |
} | |
if(this.popup.value && callbackFn) { | |
callbackFn(); | |
} | |
} | |
} catch (e) { | |
console.error(e); | |
} | |
}, this.pollInterval); | |
} | |
// Usage | |
var googleOauthPopup = new OauthPopup('https://somedomain/google/authorize/endpoint'); | |
googleOauthPopup.show(function() { | |
// callback | |
alert('Popup closed'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment