forked from awamper/web-search-dialog
-
Notifications
You must be signed in to change notification settings - Fork 3
/
helper.js
249 lines (206 loc) · 6.73 KB
/
helper.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
const St = imports.gi.St;
const Lang = imports.lang;
const PopupMenu = imports.ui.popupMenu;
const Params = imports.misc.params;
const Tweener = imports.tweener.tweener;
const Soup = imports.gi.Soup;
const Gio = imports.gi.Gio;
const GObject = imports.gi.GObject;
const Clutter = imports.gi.Clutter;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
const Prefs = Me.imports.prefs;
const DUCKDUCKGO_API_URL =
"https://api.duckduckgo.com/?format=json&no_redirect=1"+
"&skip_disambig=1&q=";
var HelperSpinnerMenuItem = GObject.registerClass(class HelperSpinnerMenuItem extends PopupMenu.PopupBaseMenuItem {
_init(text) {
super._init({
reactive: false,
activate: false,
hover: false,
can_focus: false
});
this._type = 'HELPER';
let label = new St.Label({
text: Utils.is_blank(text) ? 'Checking helper...' : text
});
let box = new St.BoxLayout({
style_class: 'helper-title'
});
box.add(label);
this.add_child(box);
}
});
var DuckDuckGoHelperMenuItem = GObject.registerClass(class DuckDuckGoHelperMenuItem extends PopupMenu.PopupBaseMenuItem {
_init(data) {
super._init({
reactive: false,
activate: false,
hover: false,
can_focus: false
});
this._type = 'HELPER';
data = Params.parse(data, {
heading: '',
definition: '',
abstract: '',
icon: ''
});
if(Utils.is_blank(data.abstract) && Utils.is_blank(data.definition)) {
return false;
}
let icon = this._get_icon(data.icon);
let grid_layout = new Clutter.GridLayout();
let grid = new St.Widget({
name: 'helper_table',
style_class: 'helper-box',
layout_manager: grid_layout,
visible: true
});
let max_length = 80;
if(icon) {
grid_layout.attach(icon, 0, 0, 1, 1);
}
else {
max_length = 110;
}
let text = '';
if(data.definition) {text += '<i>'+data.definition.trim()+'</i>\n';}
if(data.abstract) {text += data.abstract.trim();}
let label = this._get_label(text, 'helper-abstract', max_length);
grid_layout.attach(label, 1, 0, 1, 1);
this.add_child(grid);
}
_get_icon(icon_info) {
let info = Params.parse(icon_info, {
url: false,
width: 120,
height: 100
});
if(!info.url) {
return false;
}
let scale_factor = St.ThemeContext.get_for_stage(global.stage).scale_factor;
let textureCache = St.TextureCache.get_default();
let image_file = Gio.file_new_for_uri(info.url);
let icon = textureCache.load_file_async(
image_file,
info.width,
info.height,
scale_factor,
1.0 // TODO: resource scale - is it correct?
);
this.icon_box = new St.BoxLayout({
style_class: 'helper-icon-box',
opacity: 0
});
this.icon_box.add(icon);
this.icon_box.connect('notify::allocation', Lang.bind(this, function() {
let natural_width = this.icon_box.get_preferred_width(-1)[1];
if(natural_width > 10) {
Tweener.addTween(this.icon_box, {
transition: 'easeOutQuad',
time: 1,
opacity: 255
});
}
}));
return this.icon_box;
}
_get_label(text, class_name, max_length) {
if(Utils.is_blank(text)) {
return false;
}
text = Utils.wordwrap(text.trim(), max_length);
let label = new St.Label({
text: text,
style_class: class_name
});
label.clutter_text.use_markup = true;
label.clutter_text.line_wrap = true;
return label;
}
});
var DuckDuckGoHelper = class DuckDuckGoHelper {
constructor() {
this._settings = Utils.getSettings();
this._http_session = this._create_session();
}
_create_session() {
let http_session = new Soup.Session({
user_agent: Utils.DEFAULT_USER_AGENT,
timeout: 5,
accept_language: 'en'
});
Soup.Session.prototype.add_feature.call(
http_session,
new Soup.ProxyResolverDefault()
);
return http_session;
}
_get_data_async(url, callback) {
let request = Soup.Message.new('GET', url);
this._http_session.accept_language = this._settings.get_string(Prefs.LANGUAGE_CODE);
this._http_session.queue_message(request,
Lang.bind(this, function(http_session, message) {
if(message.status_code === 200) {
callback.call(this, request.response_body.data);
}
else {
callback.call(this, false);
}
})
);
}
_parse_response(response) {
response = JSON.parse(response);
let result = {
heading: Utils.is_blank(response.Heading)
? false
: response.Heading.trim().replace(/<[^>]+>/g, ""),
abstract: Utils.is_blank(response.Abstract)
? false
: response.AbstractText.trim().replace(/<[^>]+>/g, ""),
definition:
Utils.is_blank(response.Definition) ||
response.Definition == response.Abstract
? false
: response.Definition.trim().replace(/<[^>]+>/g, ""),
image: Utils.is_blank(response.Image)
? false
: response.Image.trim()
};
return result;
}
get_info(query, callback) {
query = query.trim();
if(Utils.is_blank(query)) {
return false;
}
let url = DUCKDUCKGO_API_URL+encodeURIComponent(query);
this._get_data_async(url, Lang.bind(this, function(result) {
if(!result) {
callback.call(this, false);
}
let info = this._parse_response(result);
callback.call(this, info);
}));
return true;
}
get_menu_item(data) {
data = Params.parse(data, {
heading: '',
definition: '',
abstract: '',
icon: false
});
if(Utils.is_blank(data.abstract) && Utils.is_blank(data.definition)) {
return false;
}
else {
let menu_item = new DuckDuckGoHelperMenuItem(data);
return menu_item;
}
}
};