-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
99 lines (88 loc) · 2.82 KB
/
storage.js
File metadata and controls
99 lines (88 loc) · 2.82 KB
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
String.prototype.hashCode = function () {
var hash = 0;
if (this.length == 0) {
return hash;
}
for (var i = 0; i < this.length; i++) {
var char = this.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
dictForUser = []
baseUrl = 'https://tomato-ext-backend.herokuapp.com/'
async function displayWebsites() {
if (localStorage.getItem('token') && localStorage.getItem('user')) {
const res = await fetch(baseUrl + 'api/sites/', {
method: 'GET',
headers: {
'Authorization': `bearer ${JSON.parse(localStorage.getItem('token'))}`
},
})
const json = await res.json()
console.log(json)
const sites = json.sites
let string = ''
chrome.storage.local.clear()
for (let index in sites) {
dictForUser.push(sites[index])
const givenWebsite = sites[index].website
const key = givenWebsite.hashCode().toString()
string += givenWebsite + '\n'
chrome.storage.local.set({ [key]: givenWebsite })
}
document.getElementById('websiteTextArea').value = string
} else {
chrome.storage.local.get(null, function (items) {
let str = ""
for (let property in items) {
str += items[property] + '\n'
}
document.getElementById('websiteTextArea').value = str
// console.log(items)
})
}
}
async function saveChanges() {
var givenWebsite = document.getElementById('websiteText').value
var elements = document.getElementsByName('addOrRemove')
if (!givenWebsite) {
alert('Error: No value specified')
return;
}
var flag = elements[0].checked
var key = givenWebsite.hashCode().toString()
console.log(key)
// add
if (flag) {
chrome.storage.local.set({ [key]: givenWebsite }, function () {
alert('Settings saved')
})
// logon
// i still need chrome.storage for the event listener! / offline option
if (localStorage.getItem('token') && localStorage.getItem('user')) {
const res = await fetch(baseUrl + 'api/sites', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `bearer ${JSON.parse(localStorage.getItem('token'))}`
},
body: JSON.stringify({ 'website': givenWebsite })
})
console.log(res)
}
} else {
chrome.storage.local.remove(key)
if (localStorage.getItem('token') && localStorage.getItem('user')) {
const foundObj = dictForUser.find(obj => obj.website === givenWebsite)
const res = await fetch(baseUrl + `api/sites/${foundObj.id}`, {
method: 'DELETE'
})
console.log(res)
}
}
return false
}
document.getElementById('form1').onsubmit = saveChanges
displayWebsites()