-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbookmarklet-app.js
264 lines (253 loc) · 8.42 KB
/
bookmarklet-app.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
function getDefaultData() {
// Initialize DB
db = new Dexie("bookmarklets")
db.version(1).stores({
services: 'id,value'
})
// Modal for adding a bookmarklet
modal = {show: false}
// Array to hold stuff read from DB
services = []
// Temporary object to put to DB
servicesTemp = {}
// Templates for
// A: Render input form
// B: Keep data to put to serviceTemp
serviceTemplates = [
{
serviceName: 'JSONbin.io',
name: '',
show: false,
content: [
{
type: 'secretKey',
value: '',
label: 'Secret key'
},
{
type: 'collectionID',
value: '',
label: 'Collection ID'
}
]
},
{
serviceName: 'Sanity.io',
name: '',
show: false,
content: [
{
type: 'projectID',
value: '',
label: 'Project ID'
},
{
type: 'datasetName',
value: '',
label: 'Dataset name'
},
{
type: 'tokenWithWriteAccess',
value: '',
label: 'Token with write access'
}
]
},
{
serviceName: 'Zapier.com',
name: '',
show: false,
content: [
{
type: 'webhook',
value: '',
label: 'Webhook'
}
]
}
]
return {
db,
modal,
services,
servicesTemp,
serviceTemplates
}
}
let bookmarklets = new Vue({
el: '#bookmarklets',
data: getDefaultData(),
methods: {
// READ from db
readFromDB: function() {
db.services.toArray().then (function(){
return db.services.toArray()
}).then(function(services) {
bookmarklets.services = stringParse(services)
console.log(JSON.stringify(bookmarklets.services))
}).catch(function(error) {
console.error(error)
})
},
// Bookmarklet JavaScript generating
// Stitching together embedded scripts for the bookmarklets
generateScript: function(index, id, serviceName, content) {
console.log('index: ' + index + ' ID: ' + id + ' Service name: ' + serviceName + ' content: ' + JSON.stringify(content))
// JSONbin.io endpoint script
if (serviceName === 'JSONbin.io') {
let script = 'javascript: (' + function(collectionID, secretKey) {
var url = window.location.href;
var title = document.title;
var body = document.body.innerText;
var endpointUrl = 'https://api.jsonbin.io/b';
var sendObj = JSON.stringify({'url': url, 'title': title, 'body': body});
fetch(endpointUrl, {
method: 'post',
headers: {
'Content-type': 'application/json',
'secret-key': secretKey,
'collection-id': collectionID
},
body: sendObj
})
.then(response => response.json())
.then(result => {
if(result.success === false){
alert('Adding content failed.\nIs the bookmarklet set up right?\n\n' + JSON.stringify(result))
};
if(result.success === true) {
alert('Content added to JSONbin.io\nCollection: ' + collectionID + '\n\n' + JSON.stringify(result))
};
})
.catch(error => alert('Adding content failed.\nIs the bookmarklet set up right?\n\n' + JSON.stringify(error)));
} + ')(' + JSON.stringify(content.collectionID) + ',' + JSON.stringify(content.secretKey) + ')';
return script
}
// Sanity.io endpoint script
else if (serviceName === 'Sanity.io') {
let script = 'javascript: (' + function(projectID, datasetName, tokenWithWriteAccess) {
let url = window.location.href;
let id = url.split('').reduce((prevHash, currVal) =>
(((prevHash << 5) - prevHash) + currVal.charCodeAt(0))|0, 0);
let title = document.title;
let body = document.body.innerText;
let endpointUrl = 'https://' + projectID + '.api.sanity.io/v1/data/mutate/' + datasetName;
let bearerToken = 'Bearer ' + tokenWithWriteAccess;
const sendObj = JSON.stringify({mutations: [{
createOrReplace: {
_id: id,
_type: 'document',
url: url,
title: title,
body: body
}
}]});
fetch(endpointUrl, {
method: 'post',
headers: {
'Content-type': 'application/json',
'Authorization': bearerToken
},
body: sendObj
})
.then(response => response.json())
.then(result => alert('Content added to Sanity.io\nProject: ' + projectID + ' - Dataset: ' + datasetName + '\n\n' + JSON.stringify(result)))
.catch(error => alert('Adding content to Sanity.io failed.\nIs the bookmarklet set up right?\n\n' + error));
} + ')(' + JSON.stringify(content.projectID) + ',' + JSON.stringify(content.datasetName) + ',' + JSON.stringify(content.tokenWithWriteAccess) + ')';
return script
}
// Zapier.com endpoint script
else if (serviceName === 'Zapier.com') {
let script = 'javascript: (' + function(webhook) {
var url = window.location.href;
var title = document.title;
var body = document.body.innerText;
var sendObj = JSON.stringify({'url': url, 'title': title, 'body': body});
fetch(webhook, {
method: 'post',
body: sendObj,
})
.then(response => response.json())
.then(result => alert('Content added to Zapier.com\nWebhook: ' + webhook + '\n\n' + JSON.stringify(result)))
.catch(error => alert('Adding content failed.\nIs the bookmarklet set up right?\n\n' + error));
} + ')(' + JSON.stringify(content.webhook) + ')';
return script
}
},
// WRITE when user wants to create new or edit old
// If ID not set, generate ID
// Fire off a READ at the end
addBookmarklet: function(index) {
console.log('Writing to DB')
// Transforming input into object to write to DB
// This stuff can be done better !!!
let id = ''
let contentObj = {}
this.servicesTemp = {name: serviceTemplates[index].name, serviceName: serviceTemplates[index].serviceName}
for (let i = 0; i < serviceTemplates[index].content.length; i++) {
delete serviceTemplates[index].content[i].label
let key = serviceTemplates[index].content[i].type
let value = serviceTemplates[index].content[i].value
contentObj = {[key]: value}
this.servicesTemp = {...this.servicesTemp, ...contentObj}
}
hashedID = hashCode(JSON.stringify(this.servicesTemp))
this.servicesTemp = {id: hashedID, content: JSON.stringify(this.servicesTemp)}
// Put stuff to indexedDB
db.services.put(this.servicesTemp).then (function(){
return db.services.toArray()
}).then(function (service) {
bookmarklets.services = service
}).catch(function(error) {
console.error(error)
})
// Clean up data
this.resetData()
},
// Delete ID
// Fire off a READ at the end
deleteFromDB: function(primaryKey) {
console.log('Deleting from DB')
db.services.delete(primaryKey).then(() => {
console.log("Bookmarkleet successfully deleted");
}).catch((err) => {
console.error("Could not delete bookmarklet");
}).finally(() => {
this.resetData()
});
},
resetData: function() {
var def = getDefaultData()
Object.assign(this.$data, def)
this.readFromDB()
},
showService: function(index) {
for (let i = 0; i < serviceTemplates.length; i++) {
serviceTemplates[i].show = false
}
serviceTemplates[index].show = true
},
showModal: function(show) {
modal.show = show
}
},
mounted: function() {
this.readFromDB()
}
})
// Helper functions:
// - ID hashing
const hashCode = function(str){
return str.split('').reduce((prevHash, currVal) =>
(((prevHash << 5) - prevHash) + currVal.charCodeAt(0))|0, 0);
}
// - Stringify objects before storing
// - Parse strings to objects before populating data model
// loop through stuff
const stringParse = function(array){
for (var i = 0; i < array.length; i++) {
// JSON.parse strings of content
array[i].content = JSON.parse(array[i].content)
}
return array
}