UNPKG

hellojs-xiaotian

Version:

A clientside Javascript library for standardizing requests to OAuth2 web services (and OAuth1 - with a shim)

127 lines (94 loc) 2.6 kB
// Script to support ChromeApps // This overides the hello.utils.popup method to support chrome.identity.launchWebAuthFlow // See https://developer.chrome.com/apps/app_identity#non // Is this a chrome app? if (typeof chrome === 'object' && typeof chrome.identity === 'object' && chrome.identity.launchWebAuthFlow) { (function() { // Swap the popup method hello.utils.popup = function(url) { return _open(url, true); }; // Swap the hidden iframe method hello.utils.iframe = function(url) { _open(url, false); }; // Swap the request_cors method hello.utils.request_cors = function(callback) { callback(); // Always run as CORS return true; }; // Swap the storage method var _cache = {}; chrome.storage.local.get('hello', function(r) { // Update the cache _cache = r.hello || {}; }); hello.utils.store = function(name, value) { // Get all if (arguments.length === 0) { return _cache; } // Get if (arguments.length === 1) { return _cache[name] || null; } // Set if (value) { _cache[name] = value; chrome.storage.local.set({hello: _cache}); return value; } // Delete if (value === null) { delete _cache[name]; chrome.storage.local.set({hello: _cache}); return null; } }; // Open function function _open(url, interactive) { // Launch var ref = { closed: false }; // Launch the webAuthFlow chrome.identity.launchWebAuthFlow({ url: url, interactive: interactive }, function(responseUrl) { // Did the user cancel this prematurely if (responseUrl === undefined) { ref.closed = true; return; } // Split appart the URL var a = hello.utils.url(responseUrl); // The location can be augmented in to a location object like so... // We dont have window operations on the popup so lets create some var _popup = { location: { // Change the location of the popup assign: function(url) { // If there is a secondary reassign // In the case of OAuth1 // Trigger this in non-interactive mode. _open(url, false); }, search: a.search, hash: a.hash, href: a.href }, close: function() {} }; // Then this URL contains information which HelloJS must process // URL string // Window - any action such as window relocation goes here // Opener - the parent window which opened this, aka this script hello.utils.responseHandler(_popup, window); }); // Return the reference return ref; } })(); }