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.
This commit is contained in:
Jackson Harper
2022-02-16 14:18:40 -08:00
parent 6c9e316884
commit f070020dd9
3 changed files with 29 additions and 22 deletions

View File

@ -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);

View File

@ -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']"],

View File

@ -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 () {