-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.js
228 lines (201 loc) · 6.27 KB
/
common.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
'use strict'
const _guideLoaded = new Promise((resolve,reject) => {
if (document.getElementById('guide-subscriptions-section') !== null) {
resolve(document.getElementById('guide-container'))
} else {
var _guide_element = document.querySelector('#guide')
var observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.type === 'childList' && mutation.target === _guide_element) {
observer.disconnect()
observer.takeRecords()
resolve(document.getElementById('guide-container'))
}
})
})
observer.observe(_guide_element, {
childList: true,
subtree: true,
attributes: true,
characterData: true
})
}
})
const _tokenId = _pageRequest('GET_TOKEN_ID')
const _likePlaylistId = _guideLoaded.then(node => {
function getLikePlaylistElement() {
return node.querySelector('.guide-likes-playlist-icon')
}
var id = null
if(getLikePlaylistElement() !== null) {
id = getLikePlaylistElement().closest('a')
.getAttribute('href').split('=').pop()
}
return id
})
const _channelId = _guideLoaded.then(node => {
function getMyChannelElement() {
return node.querySelector('.guide-my-channel-icon')
}
var id = null
if(getMyChannelElement() !== null) {
id = getMyChannelElement().closest('a')
.getAttribute('href').split('/').pop()
}
return id
})
const _userId = Promise.all([
_tokenId,
_likePlaylistId,
_channelId
])
.then(arr => {
var keys = []
arr[0] !== null && keys.push('idToken:'+arr[0])
arr[1] !== null && keys.push('idLikePlaylist:'+arr[1])
arr[2] !== null && keys.push('idChannel:'+arr[2])
if(keys.length === 0) {
throw('Nenhuma chave encontrada para diferenciar o usuario')
}
return storage.get([
'idToken:'+arr[0],
'idLikePlaylist:'+arr[1],
'idChannel:'+arr[2]
])
.then(values => {
var savedKeys = Object.keys(values)
var keysToSave = []
var id
if(savedKeys.length === 0) {
id = HashID.generate()
keysToSave = keys
}
else {
if(savedKeys.length < keys) {
keysToSave = keys.filter(k => !savedKeys,includes(k))
}
id = values[savedKeys[0]]
}
return storage.set(keysToSave.map(k => ({ [k]: id })).reduce((p,c) => Object.assign(p,c),{})).then(() => id)
})
})
function _getCollectionKey(collectionId) {
return _userId.then(userId => userId+':collection:'+collectionId)
}
function _getSubscriptionKey(subscriptionId) {
return _userId.then(userId => userId+':subscription:'+subscriptionId)
}
function _getCollections() {
return _userId.then(prefix => {
return storage.get(null).then(items => {
return Object.keys(items).filter(k => k.indexOf(prefix+':collection') === 0).map(k => {
return { [k.substring((prefix+':collection:').length)]: items[k] }
}).sort((a,b) => {
var id1 = Object.keys(a)[0]
var id2 = Object.keys(b)[0]
return (a[id1].name<b[id2].name?-1:(a[id1].name>b[id2].name?1:0))
})
.reduce((collections,collection) => Object.assign(collections,collection),{})
})
})
}
function _getSubscriptions() {
return _userId.then(prefix => {
return storage.get(null).then(items => {
return Object.keys(items).filter(k => k.indexOf(prefix+':subscription') === 0).map(k => {
return { [k.substring((prefix+':subscription:').length)]: items[k] }
}).reduce((subscriptions,sub) => Object.assign(subscriptions,sub),{})
})
})
}
function _addCollection(name) {
return _getCollections().then(collections => {
var newId = HashID.generate()
while(collections[newId] !== undefined) {
newId = HashID.generate()
}
_getCollectionKey(newId).then(key => {
return storage.set({
[key]: {
name: name
}
})
})
})
}
function _removeCollection(id) {
return _getSubscriptions().then(s => {
return Promise.all(
Object.keys(s).filter(k => s[k] === id).map(k => {
return _getSubscriptionKey(k)
}).concat(_getCollectionKey(id))
)
.then(valueArr => {
return storage.remove(valueArr)
})
})
}
function _addSubscription(subId,collectionId) {
return _getSubscriptionKey(subId).then(key => storage.set({ [key]: collectionId }))
}
function _removeSubscription(id) {
return _getSubscriptionKey(id).then(key => storage.remove(key))
}
function _getGuideSection() {
return _guideLoaded.then(guide => {
return document.getElementById('guide-subscriptions-section')
})
}
_userId.then(userId => {
var collectionPrefix = userId+':collection:'
var subscriptionPrefix = userId+':subscription:'
chrome.storage.onChanged.addListener((changes,area) => {
var postEvent = (type,data) => {
window.postMessage(Object.assign(data,{ type: type }), '*')
}
Object.keys(changes).map(k => {
if(changes[k].newValue === undefined) {
// Removal events
if(k.indexOf(collectionPrefix) === 0) {
postEvent('COLLECTION_REMOVED',{
id: k.substring((collectionPrefix).length),
name: changes[k].oldValue.name
})
} else if(k.indexOf(subscriptionPrefix) === 0) {
postEvent('SUBSCRIPTION_REMOVED',{
subscriptionId: k.substring((subscriptionPrefix).length),
collectionId: changes[k].oldValue
})
}
} else if(changes[k].oldValue === undefined) {
// Add events
if(k.indexOf(collectionPrefix) === 0) {
postEvent('COLLECTION_ADDED',{
id: k.substring((collectionPrefix).length),
name: changes[k].newValue.name
})
} else if(k.indexOf(subscriptionPrefix) === 0) {
postEvent('SUBSCRIPTION_ADDED',{
subscriptionId: k.substring((subscriptionPrefix).length),
collectionId: changes[k].newValue
})
}
} else {
// Update events
if(k.indexOf(collectionPrefix) === 0) {
postEvent('COLLECTION_UPDATED',{
collectionId: k.substring((collectionPrefix).length),
oldValue: changes[k].oldValue,
newValue: changes[k].newValue
})
} else if(k.indexOf(subscriptionPrefix) === 0) {
postEvent('SUBSCRIPTION_UPDATED',{
subscriptionId: k.substring((subscriptionPrefix).length),
oldValue: changes[k].oldValue,
newValue: changes[k].newValue
})
}
}
})
})
})