64 lines
3.4 KiB
JavaScript
64 lines
3.4 KiB
JavaScript
import {InputEditor} from 'resource://vulpes/services/input-editor.mjs';
|
|
|
|
// Web pages may edit their focused field, but receive no Gaia service bridge.
|
|
export class WebInputChild extends JSWindowActorChild {
|
|
handleEvent(event) {
|
|
if (this.editor?.editing) return;
|
|
const contexts = Services.cpmm.sharedData.get('vulpes:host-contexts') || [];
|
|
if (!contexts.includes(this.browsingContext.top.id)) return;
|
|
// The standalone host has no Firefox tab strip for target=_blank. Use
|
|
// the current web view so Gecko retains its normal link security checks.
|
|
if (event.type === 'click' && event.isTrusted && this.browsingContext === this.browsingContext.top) {
|
|
const link = event.composedPath().find(node => node.localName === 'a' && node.href);
|
|
const target = link?.getAttribute('target') ?? this.document.querySelector('base[target]')?.target;
|
|
if (link && target?.toLowerCase() === '_blank' && /^https?:/.test(link.href) && !link.hasAttribute('download')) {
|
|
const original = link.getAttribute('target');
|
|
link.target = '_self';
|
|
this.contentWindow.setTimeout(() => {
|
|
if (link.target !== '_self') return;
|
|
if (original === null) link.removeAttribute('target');
|
|
else link.target = original;
|
|
}, 0);
|
|
}
|
|
}
|
|
if (!Services.cpmm.sharedData.get('vulpes:native-input')) return;
|
|
// Standalone web views have no Firefox tabbrowser to focus their embedder.
|
|
// Keep Gecko's picker focus checks intact by activating only a touched view.
|
|
if (event.isTrusted && ['pointerdown','mousedown','click'].includes(event.type))
|
|
this.sendAsyncMessage('Vulpes:WebActivate', null);
|
|
let element = event.composedPath()[0];
|
|
if (element?.isContentEditable) element = element.closest('[contenteditable=true]') || element;
|
|
const editable = element?.isContentEditable || element?.localName === 'textarea' ||
|
|
(element?.localName === 'input' && ['text','search','email','url','tel','password','number'].includes(element.type));
|
|
if (!editable || element.disabled || element.readOnly) {
|
|
if (event.type === 'click' && event.isTrusted) this.sendAsyncMessage('Vulpes:WebDismissInput', null);
|
|
return;
|
|
}
|
|
this.editor ||= new InputEditor(this.contentWindow);
|
|
this.editor.focus(element);
|
|
this.sendAsyncMessage('Vulpes:InputFocus', this.editor.state());
|
|
}
|
|
receiveMessage(message) {
|
|
if (message.name === 'Vulpes:PageIcon') {
|
|
const doc = this.document;
|
|
const origin = doc.location.origin;
|
|
const candidates = [...doc.querySelectorAll('link[rel~="icon" i][href]')]
|
|
.map(link => ({href:link.href, sizes:link.getAttribute('sizes') || ''}));
|
|
candidates.push({href:origin + '/favicon.ico', sizes:''});
|
|
return (async () => {
|
|
for (const candidate of candidates.slice(0, 12)) {
|
|
let url;
|
|
try { url = new URL(candidate.href); } catch { continue; }
|
|
if (url.origin !== origin || !['https:', 'http:'].includes(url.protocol)) continue;
|
|
const image = doc.createElement('img');
|
|
image.src = url.href;
|
|
try { await image.decode(); } catch { continue; }
|
|
if (image.naturalWidth) return {href:url.href, sizes:candidate.sizes};
|
|
}
|
|
return null;
|
|
})();
|
|
}
|
|
if (message.name !== 'Vulpes:Edit' || !this.editor) throw Error('INPUT_CONTEXT_EXPIRED');
|
|
return this.editor.edit(message.data);
|
|
}
|
|
}
|