|
9 | 9 | * OF ANY KIND, either express or implied. See the License for the specific language
|
10 | 10 | * governing permissions and limitations under the License.
|
11 | 11 | */
|
12 |
| -/* global CodeMirror, showdown, html_beautify, ExcelJS */ |
| 12 | +/* global CodeMirror, showdown, html_beautify, ExcelJS, WebImporter */ |
13 | 13 | import { initOptionFields, attachOptionFieldsListeners } from '../shared/fields.js';
|
14 | 14 | import { getDirectoryHandle, saveFile } from '../shared/filesystem.js';
|
15 | 15 | import { asyncForEach } from '../shared/utils.js';
|
@@ -41,6 +41,8 @@ const BULK_URLS_LIST = document.querySelector('#import-result ul');
|
41 | 41 |
|
42 | 42 | const IMPORT_FILE_PICKER_CONTAINER = document.getElementById('import-file-picker-container');
|
43 | 43 |
|
| 44 | +const DOWNLOAD_BINARY_TYPES = ['pdf']; |
| 45 | + |
44 | 46 | const ui = {};
|
45 | 47 | const config = {};
|
46 | 48 | const importStatus = {
|
@@ -276,57 +278,74 @@ const attachListeners = () => {
|
276 | 278 | });
|
277 | 279 | processNext();
|
278 | 280 | } else {
|
279 |
| - const frame = document.createElement('iframe'); |
280 |
| - frame.id = 'import-content-frame'; |
281 |
| - |
282 |
| - if (config.fields['import-enable-js']) { |
283 |
| - frame.removeAttribute('sandbox'); |
284 |
| - } else { |
285 |
| - frame.setAttribute('sandbox', 'allow-same-origin'); |
286 |
| - } |
287 |
| - |
288 |
| - const onLoad = async () => { |
289 |
| - const includeDocx = !!dirHandle; |
290 |
| - |
291 |
| - window.setTimeout(async () => { |
292 |
| - const { originalURL, replacedURL } = frame.dataset; |
293 |
| - if (frame.contentDocument) { |
294 |
| - try { |
295 |
| - config.importer.setTransformationInput({ |
296 |
| - url: replacedURL, |
297 |
| - document: frame.contentDocument, |
298 |
| - includeDocx, |
299 |
| - params: { originalURL }, |
300 |
| - }); |
301 |
| - await config.importer.transform(); |
302 |
| - } catch (e) { |
303 |
| - // eslint-disable-next-line no-console |
304 |
| - console.error(`Cannot transform ${originalURL} - transformation error ?`, e); |
305 |
| - // fallback, probably transformation error |
306 |
| - importStatus.rows.push({ |
307 |
| - url: originalURL, |
308 |
| - status: `Error: ${e.message}`, |
309 |
| - }); |
| 281 | + const contentType = res.headers.get('content-type'); |
| 282 | + if (contentType.includes('html')) { |
| 283 | + const frame = document.createElement('iframe'); |
| 284 | + frame.id = 'import-content-frame'; |
| 285 | + |
| 286 | + if (config.fields['import-enable-js']) { |
| 287 | + frame.removeAttribute('sandbox'); |
| 288 | + } else { |
| 289 | + frame.setAttribute('sandbox', 'allow-same-origin'); |
| 290 | + } |
| 291 | + |
| 292 | + const onLoad = async () => { |
| 293 | + const includeDocx = !!dirHandle; |
| 294 | + |
| 295 | + window.setTimeout(async () => { |
| 296 | + const { originalURL, replacedURL } = frame.dataset; |
| 297 | + if (frame.contentDocument) { |
| 298 | + try { |
| 299 | + config.importer.setTransformationInput({ |
| 300 | + url: replacedURL, |
| 301 | + document: frame.contentDocument, |
| 302 | + includeDocx, |
| 303 | + params: { originalURL }, |
| 304 | + }); |
| 305 | + await config.importer.transform(); |
| 306 | + } catch (e) { |
| 307 | + // eslint-disable-next-line no-console |
| 308 | + console.error(`Cannot transform ${originalURL} - transformation error ?`, e); |
| 309 | + // fallback, probably transformation error |
| 310 | + importStatus.rows.push({ |
| 311 | + url: originalURL, |
| 312 | + status: `Error: ${e.message}`, |
| 313 | + }); |
| 314 | + } |
310 | 315 | }
|
311 |
| - } |
312 |
| - |
313 |
| - const event = new Event('transformation-complete'); |
314 |
| - frame.dispatchEvent(event); |
315 |
| - }, config.fields['import-pageload-timeout'] || 100); |
316 |
| - }; |
317 | 316 |
|
318 |
| - frame.addEventListener('load', onLoad); |
319 |
| - frame.addEventListener('transformation-complete', processNext); |
320 |
| - |
321 |
| - frame.dataset.originalURL = url; |
322 |
| - frame.dataset.replacedURL = src; |
323 |
| - frame.src = src; |
324 |
| - |
325 |
| - const current = getContentFrame(); |
326 |
| - current.removeEventListener('load', onLoad); |
327 |
| - current.removeEventListener('transformation-complete', processNext); |
328 |
| - |
329 |
| - current.replaceWith(frame); |
| 317 | + const event = new Event('transformation-complete'); |
| 318 | + frame.dispatchEvent(event); |
| 319 | + }, config.fields['import-pageload-timeout'] || 100); |
| 320 | + }; |
| 321 | + |
| 322 | + frame.addEventListener('load', onLoad); |
| 323 | + frame.addEventListener('transformation-complete', processNext); |
| 324 | + |
| 325 | + frame.dataset.originalURL = url; |
| 326 | + frame.dataset.replacedURL = src; |
| 327 | + frame.src = src; |
| 328 | + |
| 329 | + const current = getContentFrame(); |
| 330 | + current.removeEventListener('load', onLoad); |
| 331 | + current.removeEventListener('transformation-complete', processNext); |
| 332 | + |
| 333 | + current.replaceWith(frame); |
| 334 | + } else if (IS_BULK |
| 335 | + && DOWNLOAD_BINARY_TYPES.filter((t) => contentType.includes(t)).length > 0) { |
| 336 | + const blob = await res.blob(); |
| 337 | + const u = new URL(src); |
| 338 | + const path = WebImporter.FileUtils.sanitizePath(u.pathname); |
| 339 | + |
| 340 | + await saveFile(dirHandle, path, blob); |
| 341 | + importStatus.rows.push({ |
| 342 | + url, |
| 343 | + status: 'Success', |
| 344 | + path, |
| 345 | + }); |
| 346 | + updateImporterUI(null, url); |
| 347 | + processNext(); |
| 348 | + } |
330 | 349 | }
|
331 | 350 | } else {
|
332 | 351 | // eslint-disable-next-line no-console
|
|
0 commit comments