-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniversal_web_panel.js
462 lines (385 loc) · 15.2 KB
/
universal_web_panel.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
(function universal_web_panel() {
'use strict';
const PANEL_ID = 'WEBPANEL_uwp00000-0000-0000-0000-000000000000';
const TITLE = 'Universal Web Panel';
const HTML = '<title>Universal Web Panel</title>';
const HTML_DATA_URL = 'data:text/html,' + encodeURIComponent(HTML);
const ICON_SVG =
'<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="padding: 4px"><path d="M4 14a1 1 0 0 1-.78-1.63l9.9-10.2a.5.5 0 0 1 .86.46l-1.92 6.02A1 1 0 0 0 13 10h7a1 1 0 0 1 .78 1.63l-9.9 10.2a.5.5 0 0 1-.86-.46l1.92-6.02A1 1 0 0 0 11 14z"/></svg>';
const ICON_SPAN = `<span class="button-icon">${ICON_SVG}</span>`;
const ICON_DATA_URL = `data:image/svg+xml, ${ICON_SVG}`;
const BOOKMARKS_FOLDER = 'UWP';
const BOOKMARKS_ICON_SVG =
'<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="padding: 2px"><path d="M19 14c1.49-1.46 3-3.21 3-5.5A5.5 5.5 0 0 0 16.5 3c-1.76 0-3 .5-4.5 2-1.5-1.5-2.74-2-4.5-2A5.5 5.5 0 0 0 2 8.5c0 2.3 1.5 4.05 3 5.5l7 7Z"/></svg>';
const STYLE = `
.UwpToolbar {
display: flex;
flex-direction: column;
gap: 2px;
padding-top: 2px;
padding-bottom: 2px;
}
.UwpInputRow {
display: flex;
gap: 2px;
}
.UwpInput {
width: 100%;
}
.UwpBookmarks {
display: flex;
flex-wrap: wrap;
gap: 5px;
}
.UwpBookmarks.Hidden {
display: none;
}
`;
const UWP_TOOLBAR_HTML = `
<div class="UwpToolbar toolbar-default full-width">
<div class="UwpInputRow">
<input class="UwpInput" type="text" placeholder="Paste your link, html or javascript">
<div class="button-toolbar">
<button class="UwpBookmarksButton button-toolbar">${BOOKMARKS_ICON_SVG}</button>
</div>
</div>
<div class="UwpBookmarks Hidden"></div>
</div>
`;
const UWP_BOOKMARK_HTML = `
<div class="UwpBookmark button-toolbar">
<button class="UwpBookmarkButton button-toolbar">
<img src="">
<span></span>
</button>
</div>
`;
const TOOLBARS = [
'vivaldi.toolbars.panel',
'vivaldi.toolbars.navigation',
'vivaldi.toolbars.status',
'vivaldi.toolbars.mail',
'vivaldi.toolbars.mail_message',
'vivaldi.toolbars.mail_composer',
];
class UWP {
#prefs = new Preferences();
#observers = [];
constructor() {
this.#addStyle();
this.#createWebPanel().then(() => {
if (this.#panel) {
this.#listenToPanelChange();
this.#register();
} else {
this.#listenToPanelStackChange();
}
});
}
#register() {
if (!this.#isVisible) return;
this.#listenToHomeButtonClick();
if (!this.#input) {
this.#listenToWebpanelButtonAppearInPanels();
this.#listenToWebpanelButtonAppearInToolbarEditor();
this.#createUwpToolbar();
this.#listenToInputEvents();
}
this.#focusInput();
}
// listeners
#listenToPanelStackChange() {
const observer = new MutationObserver((records) => {
if (this.#panel) {
this.#observers.push(this.#listenToPanelChange());
this.#register();
}
});
observer.observe(this.#panelStack, { childList: true });
this.#observers.push(observer);
}
#listenToPanelChange() {
const observer = new MutationObserver((records) => {
this.#register();
});
observer.observe(this.#panel, {
attributes: true,
attributeFilter: ['class'],
});
this.#observers.push(observer);
}
#listenToInputEvents() {
this.#input.addEventListener('input', () =>
this.#handleInput(this.#input.value.trim())
);
}
#listenToWebpanelButtonAppearInPanels() {
for (toolbar of this.#toolbars) {
const observer = new MutationObserver(() => {
const button = toolbar.querySelector(`button[name="${PANEL_ID}"]`);
if (button) button.innerHTML = ICON_SPAN;
});
observer.observe(toolbar, { childList: true });
this.#observers.push(observer);
}
}
#listenToWebpanelButtonAppearInToolbarEditor() {
const observer = new MutationObserver(() => {
if (this.#panelButtonInToolbarEditor) this.#panelButtonInToolbarEditor.innerHTML = ICON_SPAN;
});
observer.observe(this.#browser, { childList: true });
this.#observers.push(observer);
}
#listenToHomeButtonClick() {
const homeButton = this.#panel.querySelector('button.home');
homeButton.onclick = () => {
this.#title = TITLE;
setTimeout(() => this.#webview.src = HTML_DATA_URL, 100);
this.#showWebview();
};
}
// builders
#createStyle() {
const style = document.createElement('style');
style.innerHTML = STYLE;
return style;
}
async #createWebPanel() {
const webpanels = await this.#prefs.getWebpanels();
var webpanel = webpanels.find((p) => p.id == PANEL_ID);
if (!webpanel) {
webpanel = {
activeUrl: HTML_DATA_URL,
faviconUrl: ICON_DATA_URL,
faviconUrlValid: true,
floating: true,
id: PANEL_ID,
mobileMode: true,
origin: 'user',
resizable: true,
title: TITLE,
url: 'vivaldi://universal-web-panel',
width: 650,
zoom: 1,
};
webpanels.push(webpanel);
await this.#prefs.setWebpanels(webpanels);
}
const toolbars = await Promise.all(TOOLBARS.map((path) => this.#prefs.get(path)));
const hasUwp = toolbars.some((toolbar) => toolbar.some((id) => id === PANEL_ID));
if (!hasUwp) {
const panelToolbar = toolbars[0];
const panelIndex = panelToolbar.findIndex((panel) => panel.startsWith('WEBPANEL_'));
panelToolbar.splice(panelIndex, 0, PANEL_ID);
await this.#prefs.setPanelToolbar(panelToolbar);
}
setTimeout(() => this.#panelButton.innerHTML = ICON_SPAN, 1000);
}
#createUwpToolbar() {
this.#panel.insertAdjacentHTML('beforeend', UWP_TOOLBAR_HTML);
this.#createBookmarks();
}
#createBookmarks() {
this.#fillBookmarks();
this.#uwpBookmarksButton.onclick = () => {
this.#uwpBookmarks.classList.toggle('Hidden');
};
chrome.bookmarks.onCreated.addListener(() => this.#fillBookmarks());
chrome.bookmarks.onChanged.addListener(() => this.#fillBookmarks());
chrome.bookmarks.onMoved.addListener(() => this.#fillBookmarks());
chrome.bookmarks.onRemoved.addListener(() => this.#fillBookmarks());
}
#fillBookmarks() {
this.#uwpBookmarks.innerHTML = '';
chrome.bookmarks.search(BOOKMARKS_FOLDER, (results) => {
if (results.length == 0) return;
const folder = results[0];
this.#fillBookmarksFromFolder(folder.id);
});
}
#fillBookmarksFromFolder(id) {
chrome.bookmarks.getChildren(id, (results) => {
if (results.length == 0) return;
for (const bookmark of results) {
if (bookmark.url) {
const template = document.createElement('div');
template.insertAdjacentHTML('beforeend', UWP_BOOKMARK_HTML);
const uwpBookmark = template.querySelector('.UwpBookmark');
const icon = uwpBookmark.querySelector('img');
icon.src = 'chrome://favicon/' + bookmark.url;
const title = uwpBookmark.querySelector('span');
title.innerText = bookmark.title;
const button = uwpBookmark.querySelector('button');
button.onclick = () => {
this.#openUrl(bookmark.url);
};
this.#uwpBookmarks.appendChild(uwpBookmark);
} else {
this.#fillBookmarksFromFolder(bookmark.id);
}
}
});
}
#createHtmlview() {
const htmlview = document.createElement('div');
htmlview.id = 'htmlview';
htmlview.style.height = '100%';
htmlview.style.overflow = 'auto';
return htmlview;
}
// getters
get #browser() {
return document.querySelector('#browser');
}
get #head() {
return document.querySelector('head');
}
get #toolbars() {
return document.querySelectorAll('.toolbar');
}
get #panelStack() {
return document.querySelector('.webpanel-stack');
}
get #panel() {
const selector = `webview[tab_id^="${PANEL_ID}"], webview[vivaldi_view_type^="${PANEL_ID}"`;
return document.querySelector(selector)?.parentElement?.parentElement;
}
get #panelButton() {
return document.querySelector(`button[name="${PANEL_ID}"]`);
}
get #panelButtonInToolbarEditor() {
return document.querySelector(`.toolbar-editor button[name="${PANEL_ID}"]`);
}
get #content() {
return this.#panel.querySelector('.webpanel-content');
}
get #title() {
return this.#panel.querySelector('.webpanel-title').querySelector('span');
}
get #htmlview() {
return this.#panel.querySelector('#htmlview');
}
get #webview() {
return this.#panel.querySelector('webview');
}
get #input() {
return this.#panel.querySelector('.UwpInput');
}
get #uwpBookmarks() {
return this.#panel.querySelector('.UwpBookmarks');
}
get #uwpBookmarksButton() {
return this.#panel.querySelector('.UwpBookmarksButton');
}
get #isVisible() {
return this.#panel.classList.contains('visible');
}
// setters
set #title(title) {
setTimeout(() => (this.#title.innerText = title), 100);
}
// handlers
#handleInput(value) {
if (
value.startsWith('http://') ||
value.startsWith('https://') ||
value.startsWith('file://') ||
value.startsWith('vivaldi://') ||
value.startsWith('chrome://') ||
value.startsWith('chrome-extension://') ||
value.startsWith('about:')
) {
this.#openUrl(value);
} else if (value.startsWith('(()') && value.endsWith(')()')) {
this.#executeScript(value);
} else {
this.#showHtml(value);
}
this.#clearInput();
}
#openUrl(url) {
this.#showWebview();
this.#webview.src = url;
}
#executeScript(script) {
this.#showWebview();
this.#webview.executeScript({ code: script });
}
#showHtml(html) {
this.#hideWebview();
if (!this.#htmlview) {
const htmlview = this.#createHtmlview();
this.#content.appendChild(htmlview);
}
this.#htmlview.innerHTML = html;
this.#title = TITLE;
}
// actions
#addStyle() {
this.#head.appendChild(this.#createStyle());
}
#showWebview() {
if (this.#webview.style.display === 'none') {
if (this.#htmlview) this.#htmlview.remove();
this.#webview.style.display = '';
}
}
#hideWebview() {
this.#webview.style.display = 'none';
}
#clearInput() {
this.#input.value = '';
}
#focusInput() {
setTimeout(() => this.#input.focus(), 100);
}
async reset() {
var webpanels = await this.#prefs.getWebpanels();
var webpanel = webpanels.find((p) => p.id == PANEL_ID);
if (webpanel) {
webpanels = webpanels.filter((w) => w.id !== PANEL_ID);
await this.#prefs.setWebpanels(webpanels);
}
for (const path of TOOLBARS) {
var toolbar = await this.#prefs.get(path);
if (toolbar.includes(PANEL_ID)) {
toolbar = toolbar.filter((id) => id !== PANEL_ID);
await this.#prefs.set({ path: path, value: toolbar });
}
}
}
}
class Preferences {
async get(path) {
return await vivaldi.prefs.get(path);
}
async set({ path, value }) {
await vivaldi.prefs.set({ path, value });
}
async getWebpanels() {
return await vivaldi.prefs.get('vivaldi.panels.web.elements');
}
async setWebpanels(webpanels) {
await vivaldi.prefs.set({ path: 'vivaldi.panels.web.elements', value: webpanels });
}
async getPanelToolbar() {
return await vivaldi.prefs.get('vivaldi.toolbars.panel');
}
async setPanelToolbar(toolbar) {
await vivaldi.prefs.set({ path: 'vivaldi.toolbars.panel', value: toolbar });
}
async addOnWebpanelsChangedListner(cb) {
vivaldi.prefs.onChanged.addListener(async (event) => {
if (event.path === 'vivaldi.panels.web.elements') {
await cb();
}
});
}
}
var interval = setInterval(() => {
if (document.querySelector('#browser')) {
window.uwp = new UWP();
clearInterval(interval);
}
}, 100);
})();