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
|
"use strict";
(function(exports) {
var VERB = 1;
var DBUG = 2;
var INFO = 3;
var NOTE = 4;
var WARN = 5;
// FYI: Logging everything is /very/ slow. Chrome will log & buffer
// these console logs even when the debug tools are closed. :(
// TODO: Add an easy UI to change the log level.
// (Developers can just type DEFAULT_LOG_LEVEL=VERB in the console)
var DEFAULT_LOG_LEVEL = NOTE;
console.log("Hey developer! Want to see more verbose logging?");
console.log("Type this into the console: let util = require('./util'); util.setDefaultLogLevel(util.VERB);");
console.log("Accepted levels are VERB, DBUG, INFO, NOTE and WARN, default is NOTE");
function log(level, str) {
if (level >= DEFAULT_LOG_LEVEL) {
if (level === WARN) {
// Show warning with a little yellow icon in Chrome.
console.warn(str);
} else {
console.log(str);
}
}
}
function setDefaultLogLevel(level) {
DEFAULT_LOG_LEVEL = level;
}
function getDefaultLogLevel() {
return DEFAULT_LOG_LEVEL;
}
/**
* Load a file packaged with the extension
*
* @param url: a relative URL to local file
*/
function loadExtensionFile(url, returnType) {
var xhr = new XMLHttpRequest();
// Use blocking XHR to ensure everything is loaded by the time
// we return.
xhr.open("GET", chrome.runtime.getURL(url), false);
xhr.send(null);
// Get file contents
if (xhr.readyState !== 4) {
return;
}
if (returnType === 'xml') {
return xhr.responseXML;
}
if (returnType === 'json') {
return JSON.parse(xhr.responseText);
}
return xhr.responseText;
}
/**
* Convert an ArrayBuffer to string
*
* @param array: an ArrayBuffer to convert
*/
function ArrayBufferToString(ab) {
let array = new Uint8Array(ab);
let string = "";
for (let byte of array) {
string += String.fromCharCode(byte);
}
return string;
}
Object.assign(exports, {
VERB,
DBUG,
INFO,
NOTE,
WARN,
log,
setDefaultLogLevel,
getDefaultLogLevel,
loadExtensionFile,
ArrayBufferToString
});
})(typeof exports == 'undefined' ? require.scopes.util = {} : exports);
|