-
Notifications
You must be signed in to change notification settings - Fork 4
/
background.js
242 lines (204 loc) · 6.05 KB
/
background.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
const defaultConfigUrl = 'https://config.fakenewsfitness.org/config.json';
var config = null;
function getConfig() {
var url;
if ( !config ) {
return getConfigUrl()
.then( configUrl => {
url = configUrl;
console.log( 'Attempting config fetch', url );
return fetch( configUrl );
})
.then( response => response.json() )
.catch( err => {
if ( url != defaultConfigUrl ) {
return setConfigUrl( defaultConfigUrl )
.then( () => getConfig() );
}
})
.then( cfg => {
// Store in global
config = cfg;
return config;
});
} else {
return new Promise( resolve => resolve( config ) );
}
}
function getConfigUrl() {
return chrome.storage.sync.get( ['configurl'] )
.then( items => {
return items.configurl || defaultConfigUrl;
});
}
function setConfigUrl( url ) {
resetConfig();
return chrome.storage.sync.set( { configurl: url } );
}
function resetConfig() {
config = null;
}
// Now users updated function signature as of 25 Feb 2019
// See: https://developer.chrome.com/extensions/browserAction#event-onClicked
chrome.action.onClicked.addListener(function(tab) {
openFrame( tab.id );
});
function openFrame( tabid ) {
chrome.tabs.sendMessage( tabid, {
text: 'open_frame',
src: chrome.runtime.getURL( '/panel.html' )
});
}
chrome.runtime.onMessage.addListener(function (msg, sender, response) {
var action = msg.text || '';
switch (action) {
case 'create_doc':
const drive = new GoogleDrive();
drive.getToken( true )
.then( () => drive.createFile( msg.data, msg.filename ) )
.then( data => drive.getFileById( data.id ) )
.then( data => {
// Create new tab. Additional info sent to callback via response() below.
if ( data.webViewLink ) {
chrome.tabs.create({ url: data.webViewLink });
}
response( data );
});
return true; // Enables response callback when used asynchronously.
break;
case 'set_config':
setConfigUrl( msg.url ).then( () => response( msg.url ) );
return true;
break;
case 'open_frame':
openFrame( sender.tab.id );
break;
case 'frame_loaded':
getConfig().then( config => {
chrome.tabs.sendMessage( sender.tab.id, { text: 'gather_values' }, values => response( { config, values } ) );
});
return true;
break;
// Relay close_frame request from iFrame to content script.
case 'close_frame':
chrome.tabs.sendMessage( sender.tab.id, msg, closed => {
if ( msg.reopen ) {
openFrame( sender.tab.id );
}
} );
return true;
break;
case 'whois':
// Config needed for WHOIS API
getConfig().then( () => {
whoisLookup( msg.domain ).then( data => response(data) );
});
return true;
break;
}
});
function whoisLookup( hostname ) {
var esc_domain = encodeURIComponent( hostname );
var lookup_url = config.whois_base_url + esc_domain;
return fetch( lookup_url )
.then( response => response.json() )
.then( data => formatWhois( data ) )
.catch( err => {
console.log("There was an error with the whois fetch: " + err);
});
}
function formatWhois( data ) {
var output = [];
var val = '';
var fields = {
'registrant': 'Registrant Contact',
'registration_date': 'Registration Date',
'nameservers': 'Domain Name Servers'
};
Object.keys( fields ).forEach( function(f ) {
if ( data[f] ) {
if (f === 'registration_date') {
val = formatDate(data[f]);
} else {
val = Array.isArray( data[f] ) ? data[f].join(", ") : data[f];
}
output.push( fields[f] + ': ' + val );
}
});
return output;
}
function formatDate (dateString) {
var dateArray = dateString.split("-");
return dateArray[1]+ "/" +dateArray[2]+ "/" +dateArray[0];
}
// Object/Constructor
function GoogleDrive() {}
GoogleDrive.prototype = {
// MIME boundary
boundary: '981273423198471923847',
getToken: async function( interactive ) {
return new Promise( ( resolve, reject ) => {
chrome.identity.getAuthToken( { interactive }, ( token, scopes ) => {
if ( token ) {
this.token = token;
resolve( { token, scopes } );
} else {
reject();
}
});
});
},
request: async function( method, url, body, headers ) {
const { token } = await this.getToken();
if ( !token ) {
throw 'Missing token';
return false;
}
headers = headers || {};
Object.assign( headers, {
'Content-Type': 'multipart/related; boundary=' + this.boundary,
'Accept': 'text/html',
'Authorization': 'Bearer ' + token
});
const params = {
method,
headers,
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
redirect: 'follow',
referrerPolicy: 'no-referrer'
};
if ( body ) {
params.body = body;
headers['Content-Length'] = body.length;
}
return fetch( url, params );
},
createMultipart: function( sections ) {
let body = '--' + this.boundary + '\n';
sections.forEach( sec => {
body += sec.join("\n\n");
body += "\n--" + this.boundary + "\n";
});
body += '--';
return body;
},
createFile: async function( fileData, fileName ) {
const metaData = {'name': fileName};
const bodyparts = [
[ 'Content-Type: application/json', JSON.stringify(metaData) ],
[ 'Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document', fileData ],
];
const body = this.createMultipart( bodyparts );
const url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart";
const response = await this.request( 'POST', url, body );
return response.json();
},
getFileById: async function( fileId, fields ) {
fields = fields || 'webViewLink';
const url = "https://www.googleapis.com/drive/v3/files/" + fileId + '?fields=' + encodeURIComponent( fields );
const response = await this.request( 'GET', url );
return response.json();
}
}