This repository has been archived by the owner on Dec 23, 2023. It is now read-only.
forked from retorquere/zotero-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug-log.ts
198 lines (167 loc) · 7.55 KB
/
debug-log.ts
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
type ZoteroPane = {
getSelectedItems: () => any[]
}
declare const Zotero: {
DebugLogSender: DebugLogSender
Translate: any
Prefs: {
get: (name: string, global?: boolean) => string | number | boolean
}
getInstalledExtensions: () => Promise<string[]>
platform: string
oscpu: string
locale: string
Utilities: {
generateObjectKey: () => string
}
Debug: {
getConsoleViewerOutput: () => string[]
}
getErrors: (something: boolean) => string[]
Schema: {
schemaUpdatePromise: Promise<void>
}
getActiveZoteroPane: () => ZoteroPane
}
type ExportTranslator = {
setHandler: (phase: string, handler: (obj: { string: string }, success: boolean) => void) => void // eslint-disable-line id-blacklist
setTranslator: (id: string) => void
setItems: (items: any[]) => void
translate: () => void
}
declare const Components: any
declare const Services: any
import Zip from 'jszip'
type FileIO = {
success: boolean
key: string
link: string
}
class DebugLogSender { // tslint:disable-line:variable-name
private enabled = false
private plugins: Record<string, string[]> = {}
public register(plugin: string, preferences: string[]): void {
this.plugins[plugin] = preferences
this.enabled = true
let doc: Document = null
const enumerator = Services.wm.getEnumerator('navigator:browser') // eslint-disable-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
while (enumerator.hasMoreElements()) { // eslint-disable-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
const win: { ZoteroPane: boolean; document: Document } = enumerator.getNext() // eslint-disable-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
if (!win.ZoteroPane) continue
doc = win.document
}
if (!doc.querySelector('menuitem#debug-log-menu')) {
const help = doc.querySelector('menupopup#menu_HelpPopup')
const menuitem = help.appendChild(doc.createElement('menuitem'))
menuitem.setAttribute('id', 'debug-log-menu')
menuitem.setAttribute('label', 'Send debug log to file.io')
menuitem.setAttribute('oncommand', 'Zotero.DebugLogSender.send()')
}
}
private alert(title, body) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
const ps = Components.classes['@mozilla.org/embedcomp/prompt-service;1'].getService(Components.interfaces.nsIPromptService)
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
ps.alert(null, title, body)
}
private select(): string {
const plugins = Object.keys(this.plugins)
if (plugins.length === 1) return plugins[0]
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
const prompts = Components.classes['@mozilla.org/embedcomp/prompt-service;1'].getService(Components.interfaces.nsIPromptService)
const selected = { value: -1 }
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
if (!prompts.select(null, 'Plugin', 'Send debug log for', plugins.length, plugins, selected)) return null
return plugins[selected.value]
}
public send() {
this.sendAsync().catch((err: Error) => {
this.alert('Debug log submission error', `${err}`) // eslint-disable-line @typescript-eslint/restrict-template-expressions
})
}
private async sendAsync() {
await Zotero.Schema.schemaUpdatePromise
const plugin = this.select()
const zip = new Zip()
const key: string = Zotero.Utilities.generateObjectKey()
const log = [
await this.info(plugin),
this.preferences(plugin),
Zotero.getErrors(true).join('\n\n'),
Zotero.Debug.getConsoleViewerOutput().slice(-250000).join('\n'), // eslint-disable-line no-magic-numbers
].filter((txt: string) => txt).join('\n\n').trim()
zip.file(`${key}/${key}.txt`, log)
const rdf = await this.rdf()
if (rdf) zip.file(`${key}/${key}.rdf`, rdf)
const zipped = await zip.generateAsync({
type: 'uint8array',
compression: 'DEFLATE',
compressionOptions: { level: 9 },
})
const blob = new Blob([zipped], { type: 'application/zip'})
const formData = new FormData()
formData.append('file', blob, `${key}.zip`)
const response = await this.post('https://file.io', formData)
this.alert(`Debug log ID for ${plugin}`, `${response.key}-${key}`)
}
private preferences(plugin: string): Record<string, string | number | boolean> {
const prefs: Record<string, string | number | boolean> = {}
const names: string[] = []
for (const pref of this.plugins[plugin] || []) {
if (pref.endsWith('.')) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
const childkeys: string[] = Services.prefs.getBranch(pref).getChildList('', {})
for (const key of childkeys) {
names.push(pref + key)
}
}
else {
names.push(pref)
}
}
for (const pref of names.sort()) {
prefs[pref] = Zotero.Prefs.get(pref, true)
}
return prefs
}
private async post(url: string, data: FormData): Promise<FileIO> {
const response = await fetch(url, { method: 'POST', body: data })
return (await response.json()) as FileIO
}
// general state of Zotero
private async info(plugin: string) {
let info = ''
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
const appInfo: { name: string; version: string } = Components.classes['@mozilla.org/xre/app-info;1'].getService(Components.interfaces.nsIXULAppInfo)
info += `Application: ${appInfo.name} ${appInfo.version} ${Zotero.locale}\n`
info += `Platform: ${Zotero.platform} ${Zotero.oscpu}\n`
const addons: string[] = await Zotero.getInstalledExtensions()
if (addons.length) {
info += 'Addons:\n' + addons.map((addon: string) => ` ${addon}\n`).join('') // eslint-disable-line prefer-template
}
for (const [pref, value] of Object.entries(this.preferences(plugin))) {
info += `${pref} = ${JSON.stringify(value)}\n`
}
return info
}
private rdf(): Promise<string> {
return new Promise((resolve, reject) => {
const items: any[] = Zotero.getActiveZoteroPane().getSelectedItems() // eslint-disable-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
if (items.length === 0) return resolve('')
const translation: ExportTranslator = new Zotero.Translate.Export() as ExportTranslator // eslint-disable-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
translation.setItems(items)
translation.setTranslator('14763d24-8ba0-45df-8f52-b8d1108e7ac9') // rdf
translation.setHandler('done', (obj, success) => {
if (success) {
resolve(obj ? obj.string : undefined)
}
else {
reject('translation failed')
}
})
translation.translate() // eslint-disable-line @typescript-eslint/no-unsafe-call
})
}
}
Zotero.DebugLogSender = Zotero.DebugLogSender || new DebugLogSender
export const DebugLog: DebugLogSender = Zotero.DebugLogSender as unknown as DebugLogSender