-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.js
116 lines (106 loc) · 3.69 KB
/
tools.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
function getBrowserType() { // TODO: more browsers? // TODO: handle it by splitted codes for each browser
if (typeof InstallTrigger !== 'undefined') return 'firefox'; // Firefox 1.0+
else if (!!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime)) return 'chrome'; // Chrome 1 - 71
throw new Error('Undefined browser!');
}
// function getBrowserObject() {
// switch (getBrowserType()) {
// case 'firefox':
// return browser;
// case 'chrome':
// return chrome;
// }
// }
/**
*
* @param mode can be chosen from ['celebrity', 'custom', 'both']
*/
function importVictimsEmbedding(mode='celebrity') {
return new Promise(async resolve => {
switch(mode){
case 'celebrity':
resolve(await importCelebritiesVictimsEmbedding());
break;
case 'custom':
getStorageValue('custom_victims', (victimsDict) => {
resolve(victimsDict);
});
break;
default:
getStorageValue('custom_victims', async (victimsDict) => {
let celebritiesVictimsEmbedding = await importCelebritiesVictimsEmbedding();
for(let name in victimsDict){
celebritiesVictimsEmbedding[name] = victimsDict[name];
}
resolve(celebritiesVictimsEmbedding);
});
}
});
}
function importCelebritiesVictimsEmbedding(data = 'datasets/victims.json') {
return new Promise((resolve, reject) => {
const url = getUrlOfData(data);
fetch(url)
.then((response) => response.json())
.then((json) => {
resolve(json);
})
.catch((err) => {
reject(err);
});
});
}
function getUrlOfData(localAddress) {
// switch(getBrowserType()) {
// case 'firefox':
// return browser.runtime.getURL(localAddress);
// case 'chrome':
// return chrome.runtime.getURL(localAddress);
// }
return chrome.runtime.getURL(localAddress); // both of chrome and firefox know the chrome object
}
function getStorageValue(key, callback){
switch(getBrowserType()){
case 'firefox':
browser.storage.local.get(key).then(result => callback(result[key]), error => console.log(`Error on getting storage value: ${error}`));
break;
case 'chrome':
chrome.storage.local.get([key], result => callback(result[key]));
break;
}
}
function setStorageValue(key, value){
switch(getBrowserType()){
case 'firefox':
browser.storage.local.set({[key]: value});
break;
case 'chrome':
chrome.storage.local.set({[key]: value});
break;
}
}
function matrixMultiply(a, b) {
let aNumRows = a.length, aNumCols = a[0].length,
bNumRows = b.length, bNumCols = b[0].length,
m = new Array(aNumRows); // initialize array of rows
assert(aNumCols === bNumRows, 'can not multiply these matrices');
for (let r = 0; r < aNumRows; ++r) {
m[r] = new Array(bNumCols); // initialize the current row
for (let c = 0; c < bNumCols; ++c) {
m[r][c] = 0; // initialize the current cell
for (let i = 0; i < aNumCols; ++i) {
m[r][c] += a[r][i] * b[i][c];
}
}
}
return m;
}
function assert(condition, message) {
if (!condition) {
message = message || "Assertion failed";
if (typeof Error !== "undefined") {
throw new Error(message);
}
throw message; // Fallback
}
}