-
Notifications
You must be signed in to change notification settings - Fork 8
/
quick-lookup.js
612 lines (569 loc) · 22.5 KB
/
quick-lookup.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#!/usr/bin/gjs
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
imports.gi.versions.Gio = '2.0'
imports.gi.versions.Gtk = '4.0'
imports.gi.versions.WebKit = '6.0'
imports.gi.versions.Adw = '1'
const { GLib, Gio, Gtk, Gdk, WebKit, Adw } = imports.gi
const System = imports.system
const pkg = {
name: '@PACKAGE_NAME@',
version: '@PACKAGE_VERSION@',
}
GLib.set_prgname(pkg.name)
let lookupSelection = false
let settings
try {
settings = new Gio.Settings({ schema_id: pkg.name })
} catch(e) {}
const baseURL = 'https://en.wiktionary.org/'
const baseWikiRegExp = new RegExp('^https://en.wiktionary.org/wiki/')
const apiURL = 'https://en.wiktionary.org/api/rest_v1/page/definition/'
// see https://en.wiktionary.org/wiki/Wiktionary:Namespace
const wikiNamespaces = [
'Media', 'Special', 'Talk', 'User', 'Wiktionary', 'File', 'MediaWiki',
'Template', 'Help', 'Category',
'Summary', 'Appendix', 'Concordance', 'Index', 'Rhymes', 'Transwiki',
'Thesaurus', 'Citations', 'Sign',
]
// by no means a complete list; no particular order or criteria
const suggestedLangs = [
'English', 'French', 'Spanish', 'Italian', 'Portuguese', 'Romanian',
'German', 'Dutch', 'Finnish', 'Hungarian', 'Greek', 'Irish', 'Catalan',
'Swedish', 'Danish', 'Norwegian Bokmål', 'Norwegian Nynorsk', 'Icelandic',
'Russian', 'Polish', 'Czech', 'Serbo-Croatian', 'Bulgarian',
'Armenian', 'Georgian', 'Albanian', 'Lithuanian', 'Welsh', 'Zulu',
'Chinese', 'Japanese', 'Korean', 'Vietnamese', 'Thai', 'Tagalog',
'Arabic', 'Persian', 'Turkish', 'Hindi', 'Urdu', 'Indonesian',
'Latin', 'Ancient Greek', 'Sanskrit', 'Hebrew', 'Esperanto',
].sort()
const scriptRunner = webView => {
const promises = new Map()
const makePromise = token => new Promise((resolve, reject) => promises.set(token, {
resolve: value => (resolve(value), promises.delete(token)),
reject: value => (reject(value), promises.delete(token)),
}))
const handler = ({ token, ok, payload }) => {
const promise = promises.get(token)
if (ok) promise.resolve(payload)
else promise.reject(payload)
}
const manager = webView.get_user_content_manager()
manager.connect(`script-message-received::handler`, (_, result) => {
try { handler(JSON.parse(result.to_string())) }
catch (e) { log(e) }
})
const success = manager.register_script_message_handler('handler', null)
if (!success) throw new Error('failed to register message handler')
const exec = (func, params) => {
const token = Math.random().toString()
const paramsStr = typeof params === 'undefined' ? '' : JSON.stringify(params)
const exp = paramsStr ? `JSON.parse(decodeURI("${encodeURI(paramsStr)}"))` : ''
const script = `(async () => await ${func}(${exp}))()
.then(x => globalThis.webkit.messageHandlers.handler.postMessage(
JSON.stringify({ token: "${token}", ok: true, payload: x })))
.catch(e => globalThis.webkit.messageHandlers.handler.postMessage(
JSON.stringify({ token: "${token}", ok: false, payload: e.message })))`
const promise = makePromise(token)
webView.evaluate_javascript(script, -1, null, null, null, () => {})
return promise
}
const eval = exp => new Promise((resolve, reject) =>
webView.evaluate_javascript(`JSON.stringify(${exp})`, -1, null, null, null, (_, result) => {
try {
const jscValue = webView.evaluate_javascript_finish(result)
const str = jscValue.to_string()
const value = str !== 'undefined' ? JSON.parse(str) : null
resolve(value)
} catch (e) {
reject(e)
}
}))
return { exec, eval }
}
const script = `(() => {
const usurp = p => {
let last = p
for (let i = p.childNodes.length - 1; i >= 0; i--) {
let e = p.removeChild(p.childNodes[i])
p.parentNode.insertBefore(e, last)
last = e
}
p.parentNode.removeChild(p)
}
const pangoMarkupTags = ['a', 'b', 'big', 'i', 's', 'sub', 'sup', 'small', 'tt', 'u']
const toPangoMarkup = (html, baseURL = '') => {
const doc = new DOMParser().parseFromString(html, 'text/html')
Array.from(doc.body.querySelectorAll('*')).forEach(el => {
const nodeName = el.nodeName.toLowerCase()
if (pangoMarkupTags.indexOf(nodeName) === -1) usurp(el)
else Array.from(el.attributes).forEach(x => {
if (x.name === 'href') {
const href = el.getAttribute('href')
el.setAttribute('href', new URL(href, baseURL))
} else el.removeAttribute(x.name)
})
if (nodeName === 'a' && !el.hasAttribute('href')) usurp(el)
})
return doc.body.innerHTML.trim().replace(/ /g, ' ')
.replace(/&/g, '&').replace(/&/g, '&')
}
const baseURL = '${baseURL}'
const wiktionary = ({ word, language }) => fetch('${apiURL}' + word)
.then(res => res.ok ? res.json() : Promise.reject(new Error()))
.then(json => {
const results = language.length === 2
? json[language]
: Object.values(json).find(x => x[0].language === language)
?? json.other.filter(x => x.language === language)
results.forEach(el => {
el.definitions.forEach(x => {
x.definition = toPangoMarkup(x.definition, baseURL)
if (x.examples) x.examples = x.examples.map(ex =>
toPangoMarkup(ex, baseURL))
})
})
return { word: decodeURIComponent(word), results }
})
.catch(e => {
const lower = word.toLowerCase()
if (lower !== word) return wiktionary({ word: lower, language })
else throw new Error('no definitions found')
})
globalThis.wiktionary = wiktionary
})()`
const webView = new WebKit.WebView({
settings: new WebKit.Settings({
enable_javascript_markup: false,
enable_write_console_messages_to_stdout: true,
allow_universal_access_from_file_urls: true,
allow_file_access_from_file_urls: false,
enable_fullscreen: false,
enable_hyperlink_auditing: false,
}),
})
const { eval, exec } = scriptRunner(webView)
const lookup = async (word, language) => {
await eval(script)
return await exec('wiktionary', { word, language })
}
const buildDefinition = handleLink => ({ definition, examples }, i) => {
const label = new Gtk.Label({
label: `${i + 1}`,
valign: Gtk.Align.START,
halign: Gtk.Align.END,
})
label.get_style_context().add_class('dim-label')
const value = new Gtk.Label({
label: definition,
valign: Gtk.Align.START,
halign: Gtk.Align.START,
hexpand: true,
xalign: 0,
use_markup: true,
selectable: true,
wrap: true,
})
value.connect('activate-link', handleLink)
if (examples) {
const exampleBox = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
spacing: 3,
margin_start: 18,
margin_top: 6,
margin_bottom: 6,
})
examples.forEach(example => {
const exampleLabel = new Gtk.Label({
label: example,
valign: Gtk.Align.START,
halign: Gtk.Align.START,
hexpand: true,
xalign: 0,
use_markup: true,
selectable: true,
wrap: true,
})
exampleLabel.connect('activate-link', handleLink)
const ctx = exampleLabel.get_style_context()
ctx.add_class('dim-label')
ctx.add_class('caption')
exampleBox.append(exampleLabel)
})
return [[label, value], [null, exampleBox]]
} else return [[label, value]]
}
const buildPartsOfSpeech = handleLink => ({ partOfSpeech, definitions }, i) => {
const partOfSpeechBox = new Gtk.Box({ spacing: 6 })
const partOfSpeechLabel = new Gtk.Label({
label: `<i>${partOfSpeech}</i>`,
xalign: 0,
use_markup: true,
})
partOfSpeechLabel.get_style_context().add_class('dim-label')
partOfSpeechBox.append(partOfSpeechLabel)
partOfSpeechBox.append(new Gtk.Separator({ valign: Gtk.Align.CENTER, hexpand: true }))
return (i > 0 ? [[new Gtk.Label()]] : [])
.concat([[new Gtk.Separator({ valign: Gtk.Align.CENTER }), partOfSpeechBox]])
.concat(definitions.map(buildDefinition(handleLink)).flat())
}
const buildResultsPage = ({ word, results }, handleLink) => {
const displayWord = word.replace(/_/g, ' ')
const displayLanguage = results[0].language
const linkLanguage = displayLanguage.replace(/ /g, '_')
const grid = new Gtk.Grid({
column_spacing: 6,
row_spacing: 6,
margin_start: 18,
margin_end: 18,
margin_top: 18,
margin_bottom: 18,
})
const scroll = new Gtk.ScrolledWindow()
scroll.set_child(grid)
const langLabel = new Gtk.Label({
label: displayLanguage,
xalign: 0,
})
const ctx = langLabel.get_style_context()
ctx.add_class('dim-label')
ctx.add_class('caption')
const title = new Gtk.Label({
label: GLib.markup_escape_text(displayWord, -1),
xalign: 0,
use_markup: true,
selectable: true,
wrap: true,
})
title.get_style_context().add_class('title-1')
const sourceLabel = new Gtk.Label({
label: `<small>Source: <a href="${baseURL}wiki/${
GLib.markup_escape_text(word, -1)
}#${linkLanguage}">Wiktionary</a></small>`,
xalign: 1,
use_markup: true,
margin_top: 18,
})
sourceLabel.get_style_context().add_class('dim-label')
;[[null, langLabel], [null, title]]
.concat(results.map(buildPartsOfSpeech(handleLink)).flat())
.concat([[null, sourceLabel]])
.map(([a, b], i) => {
if (a) grid.attach(a, 0, i, 1, 1)
if (b) grid.attach(b, 1, i, 1, 1)
})
return scroll
}
const applicationWindowXml = `<?xml version="1.0" encoding="UTF-8"?><interface>
<object class="AdwApplicationWindow" id="application-window">
<property name="default-height">540</property>
<property name="default-width">480</property>
<property name="title">Quick Lookup</property>
<property name="content">
<object class="AdwToolbarView">
<child type="top">
<object class="AdwHeaderBar">
<property name="title-widget">
<object class="GtkSearchEntry" id="query-entry">
<property name="placeholder-text">Word or phrase</property>
<property name="tooltip-text">Word or phrase to look up</property>
</object>
</property>
<child type="start">
<object class="GtkButton">
<property name="icon-name">go-previous-symbolic</property>
<property name="tooltip-text">Back</property>
<property name="action-name">win.go-back</property>
</object>
</child>
<child type="end">
<object class="GtkMenuButton" id="primary-menu-button">
<property name="primary">true</property>
<property name="icon-name">open-menu-symbolic</property>
<property name="menu-model">primary-menu</property>
<property name="tooltip-text">Main Menu</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkScrolledWindow">
<property name="vexpand">true</property>
<child>
<object class="GtkStack" id="stack">
<child>
<object class="GtkStackPage">
<property name="name">init</property>
<property name="child">
<object class="AdwStatusPage">
<property name="icon-name">accessories-dictionary-symbolic</property>
<property name="description">“Language is a city to the building of which every human being brought a stone.”\n―Ralph Waldo Emerson</property>
</object>
</property>
</object>
</child>
<child>
<object class="GtkStackPage">
<property name="name">loading</property>
<property name="child">
<object class="GtkSpinner">
<property name="valign">center</property>
<property name="width-request">32</property>
<property name="height-request">32</property>
<property name="spinning">true</property>
</object>
</property>
</object>
</child>
<child>
<object class="GtkStackPage">
<property name="name">error</property>
<property name="child">
<object class="AdwStatusPage">
<property name="icon-name">face-uncertain-symbolic</property>
<property name="title">No definitions found</property>
<property name="description">“Impossible is a word to be found only in the dictionary of fools.”\n―Napoléon Bonaparte</property>
</object>
</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child type="bottom">
<object class="GtkActionBar">
<child type="center">
<object class="GtkComboBoxText" id="lang-combo">
<property name="hexpand">true</property>
<property name="has-entry">true</property>
<property name="has-frame">false</property>
</object>
</child>
</object>
</child>
</object>
</property>
</object>
<menu id="primary-menu">
<section>
<item>
<attribute name="label">_Keyboard Shortcuts</attribute>
<attribute name="action">win.show-help-overlay</attribute>
</item>
<item>
<attribute name="label">_About Quick Lookup</attribute>
<attribute name="action">app.about</attribute>
</item>
</section>
</menu>
<object class="GtkShortcutsWindow" id="shortcuts-window">
<property name="modal">True</property>
<child>
<object class="GtkShortcutsSection">
<child>
<object class="GtkShortcutsGroup">
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">True</property>
<property name="title">Go back</property>
<property name="action-name">win.go-back</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">True</property>
<property name="title">Focus on word entry</property>
<property name="action-name">win.query-entry</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">True</property>
<property name="title">Focus on language entry</property>
<property name="action-name">win.lang-entry</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">True</property>
<property name="title">Menu</property>
<property name="action-name">win.menu</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">True</property>
<property name="title">Close</property>
<property name="action-name">win.close</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">True</property>
<property name="title">Quit</property>
<property name="action-name">app.quit</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</interface>`
const about = application => () => {
const aboutWindow = new Adw.AboutWindow({
application_icon: pkg.name,
application_name: 'Quick Lookup',
developer_name: 'John Factotum',
issue_url: 'https://github.com/johnfactotum/quick-lookup/issues',
license_type: Gtk.License.GPL_3_0,
version: pkg.version,
website: 'https://github.com/johnfactotum/quick-lookup',
modal: true,
transient_for: application.active_window
})
aboutWindow.present()
}
const addAction = window => {
const { application } = window
return ([fullName, accels, func]) => {
const [scope, name] = fullName.split('.')
const action = new Gio.SimpleAction({ name })
action.connect('activate', func)
if (scope === 'app') application.add_action(action)
else window.add_action(action)
application.set_accels_for_action(`${scope}.${name}`, accels)
}
}
const makeApplicationWindow = application => {
const builder = Gtk.Builder.new_from_string(applicationWindowXml, -1)
const $ = builder.get_object.bind(builder)
const win = $('application-window')
win.application = application
win.set_help_overlay($('shortcuts-window'))
const menuButton = $('primary-menu-button')
win.connect('notify::is-active', () => {
if (!lookupSelection || !win.is_active) return
const display = Gdk.Display.get_default()
const clipboard = display.get_primary_clipboard()
clipboard.read_text_async(null, (_, res) => {
try {
const text = clipboard.read_text_finish(res).trim()
if (text && text !== queryEntry.text) {
queryEntry.text = text
queryEntry.grab_focus()
queryEntry.set_position(-1)
onActivate()
}
} catch (e) {}
})
})
const queryEntry = $('query-entry')
const langCombo = $('lang-combo')
suggestedLangs.forEach(lang => langCombo.append_text(lang))
const langEntry = langCombo.get_child()
langEntry.placeholder_text = 'Language'
langEntry.tooltip_text = 'Language name or ISO 639-1 code'
langEntry.set_icon_from_icon_name(Gtk.EntryIconPosition.PRIMARY,
'preferences-desktop-locale-symbolic')
const completion = new Gtk.EntryCompletion()
completion.set_text_column(0)
completion.set_model(langCombo.get_model())
langEntry.set_completion(completion)
langEntry.text = 'English'
if (settings) {
const flag = Gio.SettingsBindFlags.DEFAULT
settings.bind('language', langEntry, 'text', flag)
}
let currentPage
const history = []
const goBack = () => {
if (!history.length) return
doLookup(...history.pop(), false)
if (!history.length) win.lookup_action('go-back').enabled = false
}
const pushHistory = () => {
if (!currentPage) return
history.push(currentPage)
win.lookup_action('go-back').enabled = true
}
const stack = $('stack')
const doLookup = async (word, language, record = true) => {
if (record) pushHistory()
stack.visible_child_name = 'loading'
const oldResults = stack.get_child_by_name('results')
if (oldResults) stack.remove(oldResults)
try {
const results = await lookup(word, language || 'en')
const widget = buildResultsPage(results, (_, uri) => {
queryEntry.grab_focus()
const internalLink = uri.split(baseWikiRegExp)[1]
if (internalLink && wikiNamespaces.every(namespace =>
!internalLink.startsWith(namespace + ':')
&& !internalLink.startsWith(namespace + '_talk:'))) {
const [title, lang] = internalLink.split('#')
const word = decodeURIComponent(title).replace(/_/g, ' ')
doLookup(word, lang).catch(e => log(e))
return true
}
})
currentPage = [word, language]
stack.add_named(widget, 'results')
stack.visible_child_name = 'results'
} catch (e) {
log(e)
stack.visible_child_name = 'error'
}
}
const onActivate = () => doLookup(queryEntry.text, langEntry.text)
langEntry.connect('activate', onActivate)
queryEntry.connect('activate', onActivate)
;[
['win.go-back', ['<Alt>Left'], goBack],
['win.close', ['<Control>w'], () => win.close()],
['win.query-entry', ['<Control>f', 'F6'], () => queryEntry.grab_focus()],
['win.lang-entry', ['<Control>l'], () => langCombo.grab_focus()],
['app.quit', ['<Control>q'], () => application.quit()],
['app.about', [], about(application)],
].map(addAction(win))
application.set_accels_for_action('win.show-help-overlay', ['<ctrl>question'])
win.lookup_action('go-back').enabled = false
return win
}
const app = new Adw.Application({
application_id: pkg.name,
flags: Gio.ApplicationFlags.FLAGS_NONE,
})
app.connect('activate', app =>
(app.activeWindow ?? makeApplicationWindow(app)).present())
app.add_main_option('version',
'v'.charCodeAt(0), GLib.OptionFlags.NONE, GLib.OptionArg.NONE,
'Show version', null)
app.add_main_option('selection',
0, GLib.OptionFlags.NONE, GLib.OptionArg.NONE,
'Look up primary selection text', null)
app.connect('handle-local-options', (app, options) => {
if (options.contains('version')) {
print(pkg.version)
return 0
}
if (options.contains('selection')) lookupSelection = true
return -1
})
// see https://stackoverflow.com/a/35237684
ARGV.unshift(System.programInvocationName)
app.run(ARGV)