From f070020dd93ae9b67c214c01b8c5be31802e66de Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Wed, 16 Feb 2022 14:18:40 -0800 Subject: [PATCH] Fallback to SaveUrl ifd browser doesnt support blob url access (safari) In Safari we can't access blob URLs in the background script, so we fallback to submitting the embedded PDF URL instead of trying to upload the content. --- pkg/extension/src/scripts/background.js | 34 +++++++++++-------- pkg/extension/src/scripts/constants.js | 1 + .../src/scripts/content/prepare-content.js | 16 +++++---- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/pkg/extension/src/scripts/background.js b/pkg/extension/src/scripts/background.js index c44c8c31b..9a93ef912 100644 --- a/pkg/extension/src/scripts/background.js +++ b/pkg/extension/src/scripts/background.js @@ -75,7 +75,7 @@ function uploadFile ({ id, uploadSignedUrl }, contentType, contentObjUrl) { }); }) .catch((err) => { - console.log('error uploading file', err) + console.error('error uploading file', err) return undefined }); } @@ -264,18 +264,24 @@ function saveArticle (tab) { let uploadResult = null; - if (type === 'pdf') { - // For PDF articles, we first upload the PDF file before passing the upload file ID in createArticle - uploadResult = await savePdfFile(tab, encodeURI(tab.url), pageInfo.contentType, uploadContentObjUrl); - if (!uploadResult || !uploadResult.id) { - browserApi.tabs.sendMessage(tab.id, { - action: ACTIONS.ShowMessage, - payload: { - text: 'Unable to save page', - type: 'error' - } - }); - return; + switch(type) { + case 'pdf': { + // For PDFs, we first upload the PDF file before passing the upload file ID in createArticle + uploadResult = await savePdfFile(tab, encodeURI(tab.url), pageInfo.contentType, uploadContentObjUrl); + if (!uploadResult || !uploadResult.id) { + browserApi.tabs.sendMessage(tab.id, { + action: ACTIONS.ShowMessage, + payload: { + text: 'Unable to save page', + type: 'error' + } + }); + return; + } + } + case 'url': { + // We don't have to special case URL, it will fall through + // and be handled when isContentAvailable returns false } } @@ -438,7 +444,6 @@ function checkAuthOnFirstClickPostInstall (tabId) { const xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 200) { - console.log('response:' + xhr.response); const { data } = JSON.parse(xhr.response); if (!data.me) { browserApi.tabs.sendMessage(tabId, { @@ -471,7 +476,6 @@ function checkAuthOnFirstClickPostInstall (tabId) { const data = JSON.stringify({ query }); - console.log('querying url', omnivoreGraphqlURL) xhr.open('POST', omnivoreGraphqlURL + 'graphql', true); setupConnection(xhr); diff --git a/pkg/extension/src/scripts/constants.js b/pkg/extension/src/scripts/constants.js index 690ccfcba..596447c31 100644 --- a/pkg/extension/src/scripts/constants.js +++ b/pkg/extension/src/scripts/constants.js @@ -7,6 +7,7 @@ window.browserScriptingApi = browserApi.scripting || browserApi.tabs; window.ENV_EXTENSION_ORIGIN = browserApi.runtime.getURL('PATH/').replace('/PATH/', ''); window.ENV_IS_FIREFOX = ENV_EXTENSION_ORIGIN.startsWith('moz-extension://'); window.ENV_IS_EDGE = navigator.userAgent.toLowerCase().indexOf('edg') > -1; +window.ENV_DOES_NOT_SUPPORT_BLOB_URL_ACCESS = /^((?!chrome|android).)*safari/i.test(navigator.userAgent); window.SELECTORS = { CANONICAL_URL: ["head > link[rel='canonical']"], diff --git a/pkg/extension/src/scripts/content/prepare-content.js b/pkg/extension/src/scripts/content/prepare-content.js index d577c823c..ecf808fd3 100644 --- a/pkg/extension/src/scripts/content/prepare-content.js +++ b/pkg/extension/src/scripts/content/prepare-content.js @@ -2,6 +2,7 @@ browserApi XMLHttpRequest ACTIONS + ENV_DOES_NOT_SUPPORT_BLOB_URL_ACCESS */ 'use strict'; @@ -28,7 +29,6 @@ 'text/x-pdf' ]; const isPdfContent = pdfContentTypes.indexOf(document.contentType) !== -1; - if (!hasPdfExtension && !isPdfContent) { return Promise.resolve(null); } @@ -38,6 +38,10 @@ return Promise.resolve(null); } + if (ENV_DOES_NOT_SUPPORT_BLOB_URL_ACCESS && embedEl.src) { + return Promise.resolve({ type: 'url', uploadContentObjUrl: embedEl.src }) + } + return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); // load `document` from `cache` @@ -45,8 +49,7 @@ xhr.responseType = 'blob'; xhr.onload = function (e) { if (this.status === 200) { - // `blob` response - resolve(URL.createObjectURL(this.response)); + resolve({ type: 'pdf', uploadContentObjUrl: URL.createObjectURL(this.response) }) } else { reject(e); } @@ -132,7 +135,6 @@ // Without adding that copy to the DOM the `window.getComputedStyle` method will always return undefined. document.documentElement.appendChild(contentCopyEl); - console.log('\n\nStarting evaluation!'); Array.from(contentCopyEl.getElementsByTagName('*')).forEach(prepareContentPostItem); /* @@ -181,9 +183,9 @@ } async function prepareContent () { - const pdfContentObjectUrl = await grabPdfContent(); - if (pdfContentObjectUrl) { - return { type: 'pdf', uploadContentObjUrl: pdfContentObjectUrl }; + const pdfContent = await grabPdfContent(); + if (pdfContent) { + return pdfContent } async function scrollPage () {