import { isAnnotationMimeType } from '../discovery'; import notify from '../util/notify'; import { AnnotationSource } from '../storage/AnnotationSource'; // When the user navigates to an annotation (collection) directly, suggest // whether they want to import the annotation(s). function onHeadersReceived({ responseHeaders, url, }: { responseHeaders?: browser.webRequest.HttpHeaders; url: string; }) { const isAnnotation = responseHeaders?.some( (header) => header.name.toLowerCase() === 'content-type' && header.value && isAnnotationMimeType(header.value), ); if (isAnnotation) { notify({ title: 'Import Web Annotation(s)', iconUrl: '/assets/icon/icon.svg', message: 'You opened a Web Annotation (Collection). Click here to import it to your annotation library.', onClicked() { // Note we do not have the response yet at this point. For simplicity, just // add it as an (inactive) source, then it will be fetched again. AnnotationSource.addSource( { type: 'container', url, title: 'Manually imported annotation', }, false, ); }, }); } } browser.webRequest.onHeadersReceived.addListener( onHeadersReceived, { urls: [''], types: ['main_frame'], }, ['responseHeaders'], ); // As files loaded through file://… URLs do not have headers, we run a separate check for these. async function onVisitFileUrl({ url, tabId }: { url: string; tabId: number }) { // TODO (likely to be quite a hassle, if possible at all..) } browser.webNavigation.onCommitted.addListener(onVisitFileUrl, { url: [{ schemes: ['file'] }], });