-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib.ts
250 lines (218 loc) · 8.94 KB
/
lib.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
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
/* eslint-disable no-var, prefer-arrow/prefer-arrow-functions, @typescript-eslint/require-await */
// declare const dump: (msg: string) => void
declare const ChromeUtils: any
import { patch as $patch$, unpatch as $unpatch$ } from './monkey-patch'
import unshell from 'shell-quote/parse'
declare const Zotero: any
var window: Window
var document: Document
// var setInterval
// var clearInterval
// var TextDecoder
// var require
// var ErrorEvent
var Zotero_LocateMenu
var ZoteroPane
function newWindow() {
window = Zotero.getMainWindow()
ZoteroPane = Zotero.getActiveZoteroPane()
document = window.document
// setInterval = window.setInterval.bind(win)
// clearInterval = window.clearInterval.bind(win)
// TextDecoder = window.TextDecoder
// require = window.require
// ErrorEvent = window.ErrorEvent
Zotero_LocateMenu = (window as any).Zotero_LocateMenu
}
newWindow()
if (typeof Services == 'undefined') var { Services } = ChromeUtils.import('resource://gre/modules/Services.jsm') // eslint-disable-line no-var
declare const Components: any
const {
// interfaces: Ci,
// results: Cr,
utils: Cu,
// Constructor: CC,
} = Components
function log(msg) {
if (typeof msg !== 'string') msg = JSON.stringify(msg)
Zotero.debug(`AltOpen PDF: ${msg}`)
}
const windowListener = {
onOpenWindow: xulWindow => {
const win: Window = xulWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIDOMWindow)
win.addEventListener('load', function listener() { // eslint-disable-line prefer-arrow/prefer-arrow-functions
log(`opened ${win.location.href}`)
newWindow()
Zotero.AltOpenPDF?.startup()
}, false)
},
// onCloseWindow: () => { },
// onWindowTitleChange: _xulWindow => { },
}
Services.wm.addListener(windowListener)
const Openers = 'extensions.zotero.open-pdf.with.'
const NAMESPACE = {
XUL: 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul',
HTML: 'http://www.w3.org/1999/xhtml',
}
function createElement(name: string, attrs: Record<string, string> = {}, namespace = NAMESPACE.XUL): HTMLElement {
const elt: HTMLElement = document.createElementNS(namespace, name) as HTMLElement
attrs.class = `alt-open-pdf ${attrs.class || ''}`.trim()
for (const [a, v] of Object.entries(attrs)) {
elt.setAttribute(a, v)
}
return elt
}
function removeElements() {
for (const elt of Array.from(document.getElementsByClassName('alt-open-pdf'))) {
elt.remove()
}
}
if (Zotero.platformMajorVersion < 102) {
const props = ['URL', 'Blob', 'FormData'].filter(p => typeof p === 'undefined')
if (props.length) Cu.importGlobalProperties(props)
}
log('AltOpen PDF: lib loading')
function getOpener(opener: string): { label: string, cmdline: string } {
if (!opener) return { label: '', cmdline: ''}
const cmdline : string = Zotero.Prefs.get(opener, true)
if (!cmdline) return { label: '', cmdline: ''}
const m = cmdline.match(/^\[(.+?)\](.+)/)
if (m) return { label: m[1], cmdline: m[2] }
return { label: `Open PDF with ${opener.replace(Openers, '')}`, cmdline }
}
function exec(exe: string, args: string[] = []): void {
log(`running ${JSON.stringify([exe].concat(args))}`)
const cmd = Zotero.File.pathToFile(exe)
if (!cmd.exists()) {
flash('opening PDF failed', `${exe} not found`)
return
}
if (!cmd.isExecutable()) {
flash('opening PDF failed', `${exe} is not runnable`)
return
}
const proc = Components.classes['@mozilla.org/process/util;1'].createInstance(Components.interfaces.nsIProcess)
proc.init(cmd)
proc.startHidden = true
proc.runw(false, args, args.length)
}
function flash(title: string, body?: string, timeout = 8): void {
try {
log(`flash: ${JSON.stringify({title, body})}`)
const pw = new Zotero.ProgressWindow()
pw.changeHeadline(`open-pdf ${title}`)
if (!body) body = title
if (Array.isArray(body)) body = body.join('\n')
pw.addDescription(body)
pw.show()
pw.startCloseTimer(timeout * 1000)
}
catch (err) {
const msg = `${err}`
log(`flash: ${JSON.stringify({title, body, err: msg})}`)
}
}
Zotero.AltOpenPDF = Zotero.AltOpenPDF || new class ZoteroAltOpenPDF {
shutdown() {
log('shutdown')
$unpatch$()
removeElements()
}
async startup() {
log(`patching ${typeof Zotero_LocateMenu}`)
$patch$(Zotero_LocateMenu, 'buildContextMenu', original => async function Zotero_LocateMenu_buildContextMenu(menu: HTMLElement, _showIcons: boolean): Promise<void> {
await original.apply(this, arguments) // eslint-disable-line prefer-rest-params
try {
let sibling: HTMLElement
for (const mi of menu.children as unknown as HTMLElement[]) {
if (mi.getAttribute('data-open-pdf-alternate')) { // already in menu
return
}
if (mi.getAttribute('data-l10n-id') === 'item-menu-viewAttachment') { // zotero 7
sibling = mi
}
if (mi.style?.listStyleImage === 'url("chrome://zotero/skin/treeitem-attachment-pdf.png")') { // zotero 6
sibling = mi
}
}
if (!sibling) {
log('Error: sibling not found')
return
}
const copyattr = (elt: HTMLElement) => {
for (const att of Array.from(sibling.attributes)) {
if (att.nodeName.match(/^data-l10n-/)) continue
elt.setAttribute(att.nodeName, att.nodeValue)
}
}
const alternate = createElement('menuitem')
copyattr(alternate)
alternate.setAttribute('data-open-pdf-alternate', 'true')
if (Zotero.Prefs.get('fileHandler.pdf')) { // existing Open option = external
log(`adding internal opener: ${Zotero.getString('locate.internalViewer.label')}`)
alternate.setAttribute('label', Zotero.getString('locate.internalViewer.label') as string)
alternate.addEventListener('command', async event => { // eslint-disable-line @typescript-eslint/no-misused-promises
event.stopPropagation()
const items = ZoteroPane.getSelectedItems()
for (const item of items) {
const attachment = item.isAttachment() ? item : (await item.getBestAttachment())
if (attachment?.attachmentPath.match(/[.]pdf$/i)) {
await Zotero.Reader.open(attachment.itemID, false, { openInWindow: false })
}
}
}, false)
}
else { // existing Open option = internal
log(`adding external system opener: ${Zotero.getString('locate.externalViewer.label')}`)
alternate.setAttribute('label', Zotero.getString('locate.externalViewer.label') as string)
alternate.addEventListener('command', async event => { // eslint-disable-line @typescript-eslint/no-misused-promises
event.stopPropagation()
const items = ZoteroPane.getSelectedItems()
for (const item of items) {
const attachment = item.isAttachment() ? item : (await item.getBestAttachment())
if (attachment?.attachmentPath.match(/[.]pdf$/i)) {
Zotero.launchFile(attachment.getFilePath())
}
}
}, false)
}
sibling.parentNode.insertBefore(alternate, sibling.nextSibling)
for (const cmdline of (Zotero.Prefs.rootBranch.getChildList(Openers, {}, {}) as string[]).sort()) {
const opener = getOpener(cmdline)
if (!opener.label || !opener.cmdline) continue
log(`adding ${cmdline}: ${JSON.stringify(opener)}`)
const custom = createElement('menuitem')
copyattr(custom)
custom.setAttribute('label', opener.label)
custom.setAttribute('data-open-pdf', cmdline)
custom.addEventListener('command', async event => { // eslint-disable-line @typescript-eslint/no-misused-promises
event.stopPropagation()
try {
const target = event.target as HTMLSelectElement
const runtime = getOpener(target.getAttribute('data-open-pdf'))
if (!runtime.cmdline) throw new Error(`No opener for ${target.getAttribute('label')}`)
const args : string[] = unshell(runtime.cmdline)
log(`command: ${JSON.stringify(args)}`)
const cmd = args.shift()
const items = ZoteroPane.getSelectedItems()
for (const item of items) {
const attachment = item.isAttachment() ? item : (await item.getBestAttachment())
if (attachment?.attachmentPath.match(/[.]pdf$/i)) {
exec(cmd, args.map((arg : string) => arg.toLowerCase() === '@pdf' ? attachment.getFilePath() as string : arg))
}
}
}
catch (err) {
flash('Could not open attachments', (err.message as string) || 'unknown error')
}
}, false)
alternate.parentNode.insertBefore(custom, alternate.nextSibling)
}
}
catch (err) {
log(`failed to patch menu: ${err}`)
}
})
}
}