-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
225 lines (191 loc) · 6 KB
/
popup.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
new Vue({
el: '#app',
data() {
return {
settings: {},
vm: this,
window,
console,
apiTemplates,
settingsLoaded: false,
}
},
mounted() {
chrome.storage.sync.get('settings', ({ settings }) => {
console.log('Settings:', settings)
this.settings = settings || {}
// If the settings don't have at least one of the keys of defaultSettings, set them to the default
for ( let key in defaultSettings || {} )
if ( !this.settings[key] || key == 'apis' && !this.settings[key].length )
this.$set(this.settings, key, defaultSettings[key])
window.settings = this.settings
this.settingsLoaded = true
})
Object.assign(window, {
chrome,
vm: this,
})
setTimeout(() => {
document.getElementById('api-list')?.focus()
}, 0)
},
computed: {
activeTab() {
// window.alert(`activeTab: ${this.settings.activeTab}`)
return this.settings.activeTab || 'api'
},
subTab() {
return this.settings.subTab || 'general'
},
allRequiredApiSettingsSet() {
return [ 'endpoint', 'auth', 'promptKey', 'resultKey' ].every(key => this.api[key])
},
hideAllButBody() {
// Check if all required api settings are set. If yes, return settings.hideAllButBody
if (
this.settings.hideAllButBody &&
this.allRequiredApiSettingsSet
)
return true
else
return false
},
api: {
get() {
console.log('Getting API')
console.log(this.settings)
return this.settings?.apis?.find(api => api.name === this.settings.currentApiName) || this.settings.apis?.[0]
},
set(api) {
this.settings.currentApiName = api.name
}
},
stringifiedApiParams: {
get() {
return JSON.stringify(this.api.otherBodyParams, null, 2)
},
set(text) {
try {
this.api.otherBodyParams = JSON.parse(text)
} catch (e) {
this.$bvToast.toast('Invalid JSON', {
title: 'Error',
variant: 'danger',
solid: true
})
}
}
},
},
watch: {
settings: {
handler(settings) {
// Store in chrome.storage.sync and send to all tabs' content scripts
chrome.storage.sync.set({ settings })
chrome.tabs.query({}, tabs => {
tabs.forEach(tab => {
chrome.tabs.sendMessage(tab.id, { settings })
})
})
console.log('Settings updated:', settings)
},
deep: true,
},
'api.auth'(auth) {
// If auth doesn't start with a "Bearer" or "Basic" or "JWT", add it after confirming
if (
auth && !auth.match(/^(Bearer|Basic|JWT) /) &&
confirm(`Seems like you entered just the API key, not the entire auth header.\n\nDo you want to set to 'Bearer ${auth}'?`)
)
this.api.auth = `Bearer ${auth}`
}
},
methods: {
currentApiIndex() {
return this.settings?.apis?.indexOf(this.api)
},
changeApiName(name) {
this.api.name = name
this.settings.currentApiName = name
},
nudge(direction) {
// Reorder the APIs array, moving the current API either up or down depending on the direction (1 or -1)
let { settings: { apis }, api } = this
let currentApiIndex = apis.indexOf(api)
let newIndex = currentApiIndex + direction
if ( newIndex >= 0 && newIndex < apis.length ) {
apis.splice(currentApiIndex, 1)
apis.splice(newIndex, 0, api)
this.api = api
}
},
pickName(baseName) {
// If there's already an API with that name, add a number to the name until there's no conflict
let name = baseName
for ( let i = 2; this.settings.apis.find(api => api.name === name); i++ ) {
name = `${baseName} (${i})`
}
return name
},
addApi() {
// Add a new API to the list
let newApiName = this.pickName('New API')
this.settings.apis = [...this.settings.apis,
{
...JSON.parse(JSON.stringify(defaultApi)),
name: newApiName,
empty: true,
}
]
this.api = this.settings.apis[this.settings.apis.length - 1]
},
deleteApi({ doNotPrompt } = {}) {
// Prompt first if doNotPrompt is not set
if ( !doNotPrompt && !confirm(`Are you sure you want to delete the API "${this.api.name}"? There is no undo!`) )
return
// Delete the current API
let { settings: { apis }, api } = this
let currentApiIndex = apis.indexOf(api)
apis.splice(currentApiIndex, 1)
// If none left, create a new one
if ( apis.length === 0 )
this.addApi()
this.api = apis[currentApiIndex - 1] || apis[0]
},
cloneApi() {
// Clone the current API
let { settings: { apis }, api } = this
let currentApiIndex = apis.indexOf(api)
let newApi = JSON.parse(JSON.stringify(api))
newApi.name = this.pickName(newApi.name)
for ( let i = 2; this.settings.apis.find(api => api.name === newApi.name); i++ ) {
newApi.name = `${api.name} (copy ${i})`
}
apis.splice(currentApiIndex + 1, 0, newApi)
this.api = apis[currentApiIndex + 1]
},
downloadSettings() {
// Download the settings as a JSON file
let data = JSON.stringify(this.settings, null, 2)
let blob = new Blob([data], { type: 'application/json' })
let link = document.createElement('a')
link.id = 'download-settings'
link.href = URL.createObjectURL(blob)
link.download = 'settings.json'
link.click()
// Clean up
setTimeout(() => {
document.getElementById('download-settings').remove()
}, 0)
},
resetSettings() {
// Prompt first
if ( !confirm(`Are you sure you want to reset all settings? There is no undo!`) )
return
// Reset the settings
this.settings = JSON.parse(JSON.stringify(defaultSettings))
this.api = this.settings.apis[0]
}
}
})
console.log('popup.js loaded')