-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtils.js
105 lines (87 loc) · 2.49 KB
/
Utils.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
class Utils{
static get PIPEDRIVE_TOKEN_KEY(){
return "PIPEDRIVE_TOKEN";
}
static get AIRTABLE_TOKEN_KEY(){
return "AIRTABLE_TOKEN";
}
static get PIPEDRIVE_TEMP_TOKEN_KEY(){
return "PIPEDRIVE_TEMP_TOKEN";
}
static get AIRTABLE_TEMP_TOKEN_KEY(){
return "AIRTABLE_TEMP_TOKEN";
}
static get STATE_KEY(){
return "STATE";
}
static getPipedriveToken(callback){
Utils.getValue(Utils.PIPEDRIVE_TOKEN_KEY, callback);
}
static getAirtableToken(callback){
Utils.getValue(Utils.AIRTABLE_TOKEN_KEY, callback);
}
static getTempPipedriveToken(callback){
Utils.getValue(Utils.PIPEDRIVE_TEMP_TOKEN_KEY, callback);
}
static getTempAirtableToken(callback){
Utils.getValue(Utils.AIRTABLE_TEMP_TOKEN_KEY, callback);
}
static getState(callback){
Utils.getValue(Utils.STATE_KEY, callback);
}
static setPipedriveToken(token){
Utils.setValue(Utils.PIPEDRIVE_TOKEN_KEY, token);
}
static setAirtableToken(token){
Utils.setValue(Utils.AIRTABLE_TOKEN_KEY, token);
}
static setTempPipedriveToken(token){
Utils.setValue(Utils.PIPEDRIVE_TEMP_TOKEN_KEY, token);
}
static setTempAirtableToken(token){
Utils.setValue(Utils.AIRTABLE_TEMP_TOKEN_KEY, token);
}
static setState(state){
Utils.setValue(Utils.STATE_KEY, state);
}
static setValue(key, value){
if(window.chrome && window.chrome.storage && window.chrome.storage.local && window.chrome.storage.local.set){
window.chrome.storage.local.set({[key]: value});
}else if(window.localStorage){
window.localStorage[key] = typeof(value) === "object" ? JSON.stringify(value) : value;
}
}
static getValue(key, callback){
if(window.chrome && window.chrome.storage && window.chrome.storage.local && window.chrome.storage.local.get){
window.chrome.storage.local.get(key, values => {
callback(values && values[key]);
});
}else if(window.localStorage){
if(key === Utils.STATE_KEY){
callback(JSON.parse(window.localStorage[key]));
}else{
callback(window.localStorage[key]);
}
}else{
callback();
}
}
static getDealId(callback){
if(window.chrome && window.chrome.tabs){
chrome.tabs.query({'active': true}, tabs => {
if(tabs && tabs.length && tabs[0].url && /starclinch\.pipedrive\.com\/deal\/\d+/.test(tabs[0].url)){
callback(tabs[0].url.match(/deal\/(\d+)/)[1]);
}else{
callback();
}
});
}else{
callback();
}
}
static getManifestVersion(callback) {
var manifestVersion = chrome.runtime.getManifest().version;
console.log(manifestVersion);
callback(manifestVersion);
}
}