forked from MrSwitch/hello.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.chromeapp.js
126 lines (94 loc) · 2.54 KB
/
hello.chromeapp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 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;
}
})();
}