diff --git a/apple/Sources/SafariExtension/Resources/manifest.json b/apple/Sources/SafariExtension/Resources/manifest.json index b7c98ec12..a5c39db66 100644 --- a/apple/Sources/SafariExtension/Resources/manifest.json +++ b/apple/Sources/SafariExtension/Resources/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "Omnivore", "short_name": "Omnivore", - "version": "0.1.20", + "version": "0.1.22", "description": "Save articles to your Omnivore library", "author": "Omnivore Media, Inc", "default_locale": "en", diff --git a/apple/Sources/SafariExtension/Resources/scripts/background.js b/apple/Sources/SafariExtension/Resources/scripts/background.js index 41d461289..6e026ee9d 100644 --- a/apple/Sources/SafariExtension/Resources/scripts/background.js +++ b/apple/Sources/SafariExtension/Resources/scripts/background.js @@ -1 +1 @@ -(()=>{"use strict";let e;const t="https://omnivore.app",s="https://omnivore.app/api/";function o(e){return new Promise((t=>{browserApi.storage.local.get(e,(s=>{const o=s&&s[e]||null;t(o)}))}))}function n(e){return new Promise((t=>{browserApi.storage.local.set(e,t)}))}function r(e){return new Promise((t=>{browserApi.storage.local.remove(e,t)}))}function a(t){t.setRequestHeader("Content-Type","application/json"),e&&t.setRequestHeader("Authorization",e)}function i(){o("postInstallClickComplete").then((e=>{e&&r("postInstallClickComplete")}))}function l(e){browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{type:"loading",text:"Saving..."}});const o=new XMLHttpRequest,n={BAD_DATA:"Unable to save page",NOT_ALLOWED_TO_PARSE:"Not allowed to parse this article",UNAUTHORIZED:"Please login to Omnivore to authorize this action",UNABLE_TO_FETCH:"Unable to fetch page",PAYLOAD_TOO_LARGE:"This article is too large"};o.onreadystatechange=function(){if(4===o.readyState)if(200===o.status){const{data:s}=JSON.parse(o.response);if("createArticle"in s)if("errorCodes"in s.createArticle){const o={text:n[s.createArticle.errorCodes[0]]||"Unable to save page",type:"error"};"UNAUTHORIZED"===s.createArticle.errorCodes[0]&&(o.errorCode=401,o.url=t,i()),browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:o})}else{const o=s.createArticle.createdArticle,n=s.createArticle.user,r=t+(o.hasContent?`/${n.profile.username}/`+o.slug:"/home");browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{text:"Page saved!",link:r,linkText:"View",type:"success"}})}else if("errorCodes"in s.createArticleSavingRequest)browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{text:n[s.createArticleSavingRequest.errorCodes[0]]||"Unable to save page",type:"error"}});else{const o=s.createArticleSavingRequest.articleSavingRequest,n=t+"/article/sr/"+o.id;browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{text:"Page saved!",link:n,linkText:"View",type:"success"}})}}else 400===o.status&&browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{text:"Unable to save page",type:"error"}})},browserApi.tabs.sendMessage(e.id,{action:ACTIONS.GetContent},(async n=>{if(!n||"object"!=typeof n)return;const{type:r,pageInfo:l,doc:c,uploadContentObjUrl:p}=n;let d=null;if("pdf"===r&&(d=await function(e,o,n,r){return new Promise((l=>{const c=new XMLHttpRequest;c.onreadystatechange=async function(){if(4===c.readyState){if(200===c.status){const{data:s}=JSON.parse(c.response);if("errorCodes"in s.uploadFileRequest&&"UNAUTHORIZED"===s.uploadFileRequest.errorCodes[0]&&(i(),browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{text:"Unable to save page",type:"error",errorCode:401,url:t}})),s.uploadFileRequest&&s.uploadFileRequest.id&&!("errorCodes"in s.uploadFileRequest)){const e=await function({id:e,uploadSignedUrl:t},s,o){return fetch(o).then((e=>e.blob())).then((o=>new Promise((n=>{const r=new XMLHttpRequest;r.open("PUT",t,!0),r.setRequestHeader("Content-Type",s),r.onerror=()=>{n(null)},r.onload=()=>{n({id:e})},r.send(o)}))))}(s.uploadFileRequest,n,r);return URL.revokeObjectURL(r),l(e)}browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{text:"Unable to save page",type:"error"}})}else 400===c.status&&browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{text:"Unable to save page",type:"error"}});l(!1)}};const p=JSON.stringify({query:"mutation UploadFileRequest($input: UploadFileRequestInput!) {\n uploadFileRequest(input:$input) {\n ... on UploadFileRequestError {\n errorCodes\n }\n ... on UploadFileRequestSuccess {\n id\n uploadSignedUrl\n }\n }\n }",variables:{input:{url:o,contentType:n}}});c.open("POST",s+"graphql",!0),a(c),c.send(p)}))}(e,encodeURI(e.url),l.contentType,p),!d||!d.id))return void browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{text:"Unable to save page",type:"error"}});const u=c&&c.length||d,g=u?CREATE_ARTICLE_QUERY:CREATE_ARTICLE_SAVING_REQUEST_QUERY,b={url:encodeURI(e.url)};u&&(b.preparedDocument={document:c,pageInfo:l},b.uploadFileId=d&&d.id||null);const A=JSON.stringify({query:g,variables:{input:b}});o.open("POST",s+"graphql",!0),a(o),o.send(A)}))}async function c(e){const t=await o(e+"_saveInProgress");if(!t)return;clearInterval(t);const s=await o(e+"_saveInProgressTimeoutId_"+t);s&&clearTimeout(s)}function p(e){function t(t,s){browserApi.tabs.get(e,(e=>{"complete"!==e.status?(browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{type:"loading",text:"Page loading..."}}),s&&"function"==typeof s&&s()):(t&&"function"==typeof t&&t(),l(e))}))}c(e),t(null,(()=>{!function(e,t,s,o=1e3,n=10500){const r=setInterval(e,o),a=setTimeout((()=>{clearInterval(r),t()}),n);s&&"function"==typeof s&&s(r,a)}((()=>{t((()=>{c(e)}))}),(()=>{c(e),browserApi.tabs.get(e,(e=>{l(e)}))}),((t,s)=>{const o={};o[e+"_saveInProgress"]=t,o[e+"_saveInProgressTimeoutId_"+t]=s,n(o)}))}))}function d(r){return o("postInstallClickComplete").then((async o=>{if(o)return!0;if("undefined"!=typeof browser&&browser.runtime&&browser.runtime.sendNativeMessage){const t=await browser.runtime.sendNativeMessage("omnivore",{message:ACTIONS.GetAuthToken});t.authToken&&(e=t.authToken)}return new Promise((e=>{const o=new XMLHttpRequest;o.onreadystatechange=function(){if(4===o.readyState&&200===o.status){console.log("response:"+o.response);const{data:s}=JSON.parse(o.response);s.me?(n({postInstallClickComplete:!0}),e(!0)):(browserApi.tabs.sendMessage(r,{action:ACTIONS.ShowMessage,payload:{type:"loading",text:"Loading..."}}),browserApi.tabs.sendMessage(r,{action:ACTIONS.ShowMessage,payload:{text:"",type:"error",errorCode:401,url:t}}),e(null))}};const i=JSON.stringify({query:"{me{id}}"});console.log("querying url",s),o.open("POST",s+"graphql",!0),a(o),o.send(i)}))}))}function u(e,t){let s="/images/toolbar/icon";if(ENV_IS_FIREFOX?s+="_firefox":ENV_IS_EDGE&&(s+="_edge"),e||(s+="_inactive"),("boolean"==typeof t?t:window.matchMedia("(prefers-color-scheme: dark)").matches)&&(s+="_dark"),ENV_IS_FIREFOX)return s+".svg";const o=["16","24","32","48"];ENV_IS_EDGE||o.push("19","38");const n={};for(let e=0;e{!function t(){browserApi.tabs.get(e,(function(e){browserApi.runtime.lastError&&setTimeout(t,150),b(e)}))}()})),browserApi.tabs.onUpdated.addListener(((e,t,s)=>{t.status&&s&&s.active&&b(s)})),browserApi.tabs.onRemoved.addListener((e=>{!function(e){new Promise((e=>{browserApi.storage.local.get(null,(t=>{e(t||{})}))})).then((function(t){const s=[],o=Object.keys(t),n=e+"_saveInProgress";for(let e=0;e{browserApi.tabs.query({active:!0,currentWindow:!0},(function(t){e(t[0]||null)}))})).then((e=>{browserApi.tabs.sendMessage(e.id,{action:ACTIONS.Ping},(async function(t){if(t&&t.pong)await d(e.id)&&p(e.id);else{const t=browserApi.runtime.getManifest().content_scripts,s=[...t[0].js,...t[1].js];!function(t,s,o){function n(e,t,s){return function(){browserScriptingApi.executeScript(e,t,s)}}let r=async function(){await d(e.id)&&p(e.id)};for(let e=s.length-1;e>=0;--e)r=n(t,{file:s[e]},r);null!==r&&r()}(e.id,s)}}))}))})),browserApi.runtime.onMessage.addListener(((e,t,s)=>{if(e.forwardToTab)return delete e.forwardToTab,void browserApi.tabs.sendRequest(t.tab.id,e);e.action===ACTIONS.RefreshDarkMode&&g(t.tab.id,e.payload.value)})),browserActionApi.setIcon({path:u(!0)})})(); \ No newline at end of file +(()=>{"use strict";let e;const t="https://omnivore.app",s="https://omnivore.app/api/";function o(e){return new Promise((t=>{browserApi.storage.local.get(e,(s=>{const o=s&&s[e]||null;t(o)}))}))}function n(e){return new Promise((t=>{browserApi.storage.local.set(e,t)}))}function r(e){return new Promise((t=>{browserApi.storage.local.remove(e,t)}))}function a(t){t.setRequestHeader("Content-Type","application/json"),e&&t.setRequestHeader("Authorization",e)}function i(){o("postInstallClickComplete").then((e=>{e&&r("postInstallClickComplete")}))}function l(e){browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{type:"loading",text:"Saving..."}});const o=new XMLHttpRequest,n={BAD_DATA:"Unable to save page",NOT_ALLOWED_TO_PARSE:"Not allowed to parse this article",UNAUTHORIZED:"Please login to Omnivore to authorize this action",UNABLE_TO_FETCH:"Unable to fetch page",PAYLOAD_TOO_LARGE:"This article is too large"};o.onreadystatechange=function(){if(4===o.readyState)if(200===o.status){const{data:s}=JSON.parse(o.response);if("createArticle"in s)if("errorCodes"in s.createArticle){const o={text:n[s.createArticle.errorCodes[0]]||"Unable to save page",type:"error"};"UNAUTHORIZED"===s.createArticle.errorCodes[0]&&(o.errorCode=401,o.url=t,i()),browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:o})}else{const o=s.createArticle.createdArticle,n=s.createArticle.user,r=t+(o.hasContent?`/${n.profile.username}/`+o.slug:"/home");browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{text:"Page saved!",link:r,linkText:"View",type:"success"}})}else if("errorCodes"in s.createArticleSavingRequest)browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{text:n[s.createArticleSavingRequest.errorCodes[0]]||"Unable to save page",type:"error"}});else{const o=s.createArticleSavingRequest.articleSavingRequest,n=t+"/article/sr/"+o.id;browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{text:"Page saved!",link:n,linkText:"View",type:"success"}})}}else 400===o.status&&browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{text:"Unable to save page",type:"error"}})},browserApi.tabs.sendMessage(e.id,{action:ACTIONS.GetContent},(async n=>{if(!n||"object"!=typeof n)return;const{type:r,pageInfo:l,doc:c,uploadContentObjUrl:p}=n;let d=null;switch(r){case"pdf":if(d=await function(e,o,n,r){return new Promise((l=>{const c=new XMLHttpRequest;c.onreadystatechange=async function(){if(4===c.readyState){if(200===c.status){const{data:s}=JSON.parse(c.response);if("errorCodes"in s.uploadFileRequest&&"UNAUTHORIZED"===s.uploadFileRequest.errorCodes[0]&&(i(),browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{text:"Unable to save page",type:"error",errorCode:401,url:t}})),s.uploadFileRequest&&s.uploadFileRequest.id&&!("errorCodes"in s.uploadFileRequest)){const e=await function({id:e,uploadSignedUrl:t},s,o){return fetch(o).then((e=>e.blob())).then((o=>new Promise((n=>{const r=new XMLHttpRequest;r.open("PUT",t,!0),r.setRequestHeader("Content-Type",s),r.onerror=()=>{n(null)},r.onload=()=>{n({id:e})},r.send(o)})))).catch((e=>{console.error("error uploading file",e)}))}(s.uploadFileRequest,n,r);return URL.revokeObjectURL(r),l(e)}browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{text:"Unable to save page",type:"error"}})}else 400===c.status&&browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{text:"Unable to save page",type:"error"}});l(!1)}};const p=JSON.stringify({query:"mutation UploadFileRequest($input: UploadFileRequestInput!) {\n uploadFileRequest(input:$input) {\n ... on UploadFileRequestError {\n errorCodes\n }\n ... on UploadFileRequestSuccess {\n id\n uploadSignedUrl\n }\n }\n }",variables:{input:{url:o,contentType:n}}});c.open("POST",s+"graphql",!0),a(c),c.send(p)}))}(e,encodeURI(e.url),l.contentType,p),!d||!d.id)return void browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{text:"Unable to save page",type:"error"}})}const u=c&&c.length||d,g=u?CREATE_ARTICLE_QUERY:CREATE_ARTICLE_SAVING_REQUEST_QUERY,b={url:encodeURI(e.url)};u&&(b.preparedDocument={document:c,pageInfo:l},b.uploadFileId=d&&d.id||null);const A=JSON.stringify({query:g,variables:{input:b}});o.open("POST",s+"graphql",!0),a(o),o.send(A)}))}async function c(e){const t=await o(e+"_saveInProgress");if(!t)return;clearInterval(t);const s=await o(e+"_saveInProgressTimeoutId_"+t);s&&clearTimeout(s)}function p(e){function t(t,s){browserApi.tabs.get(e,(e=>{"complete"!==e.status?(browserApi.tabs.sendMessage(e.id,{action:ACTIONS.ShowMessage,payload:{type:"loading",text:"Page loading..."}}),s&&"function"==typeof s&&s()):(t&&"function"==typeof t&&t(),l(e))}))}c(e),t(null,(()=>{!function(e,t,s,o=1e3,n=10500){const r=setInterval(e,o),a=setTimeout((()=>{clearInterval(r),t()}),n);s&&"function"==typeof s&&s(r,a)}((()=>{t((()=>{c(e)}))}),(()=>{c(e),browserApi.tabs.get(e,(e=>{l(e)}))}),((t,s)=>{const o={};o[e+"_saveInProgress"]=t,o[e+"_saveInProgressTimeoutId_"+t]=s,n(o)}))}))}function d(r){return o("postInstallClickComplete").then((async o=>{if(o)return!0;if("undefined"!=typeof browser&&browser.runtime&&browser.runtime.sendNativeMessage){const t=await browser.runtime.sendNativeMessage("omnivore",{message:ACTIONS.GetAuthToken});t.authToken&&(e=t.authToken)}return new Promise((e=>{const o=new XMLHttpRequest;o.onreadystatechange=function(){if(4===o.readyState&&200===o.status){const{data:s}=JSON.parse(o.response);s.me?(n({postInstallClickComplete:!0}),e(!0)):(browserApi.tabs.sendMessage(r,{action:ACTIONS.ShowMessage,payload:{type:"loading",text:"Loading..."}}),browserApi.tabs.sendMessage(r,{action:ACTIONS.ShowMessage,payload:{text:"",type:"error",errorCode:401,url:t}}),e(null))}};const i=JSON.stringify({query:"{me{id}}"});o.open("POST",s+"graphql",!0),a(o),o.send(i)}))}))}function u(e,t){let s="/images/toolbar/icon";if(ENV_IS_FIREFOX?s+="_firefox":ENV_IS_EDGE&&(s+="_edge"),e||(s+="_inactive"),("boolean"==typeof t?t:window.matchMedia("(prefers-color-scheme: dark)").matches)&&(s+="_dark"),ENV_IS_FIREFOX)return s+".svg";const o=["16","24","32","48"];ENV_IS_EDGE||o.push("19","38");const n={};for(let e=0;e{!function t(){browserApi.tabs.get(e,(function(e){browserApi.runtime.lastError&&setTimeout(t,150),b(e)}))}()})),browserApi.tabs.onUpdated.addListener(((e,t,s)=>{t.status&&s&&s.active&&b(s)})),browserApi.tabs.onRemoved.addListener((e=>{!function(e){new Promise((e=>{browserApi.storage.local.get(null,(t=>{e(t||{})}))})).then((function(t){const s=[],o=Object.keys(t),n=e+"_saveInProgress";for(let e=0;e{browserApi.tabs.query({active:!0,currentWindow:!0},(function(t){e(t[0]||null)}))})).then((e=>{browserApi.tabs.sendMessage(e.id,{action:ACTIONS.Ping},(async function(t){if(t&&t.pong)await d(e.id)&&p(e.id);else{const t=browserApi.runtime.getManifest().content_scripts,s=[...t[0].js,...t[1].js];!function(t,s,o){function n(e,t,s){return function(){browserScriptingApi.executeScript(e,t,s)}}let r=async function(){await d(e.id)&&p(e.id)};for(let e=s.length-1;e>=0;--e)r=n(t,{file:s[e]},r);null!==r&&r()}(e.id,s)}}))}))})),browserApi.runtime.onMessage.addListener(((e,t,s)=>{if(e.forwardToTab)return delete e.forwardToTab,void browserApi.tabs.sendRequest(t.tab.id,e);e.action===ACTIONS.RefreshDarkMode&&g(t.tab.id,e.payload.value)})),browserActionApi.setIcon({path:u(!0)})})(); \ No newline at end of file diff --git a/apple/Sources/SafariExtension/Resources/scripts/constants.js b/apple/Sources/SafariExtension/Resources/scripts/constants.js index 648eb047d..86558e7ad 100644 --- a/apple/Sources/SafariExtension/Resources/scripts/constants.js +++ b/apple/Sources/SafariExtension/Resources/scripts/constants.js @@ -1 +1 @@ -"use strict";window.browserApi="object"==typeof chrome&&chrome&&chrome.runtime&&chrome||"object"==typeof browser&&browser||{},window.browserActionApi=browserApi.action||browserApi.browserAction||browserApi.pageAction,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.SELECTORS={CANONICAL_URL:["head > link[rel='canonical']"],TITLE:["head > meta[property='og:title']"]},window.ACTIONS={ShowMessage:"SHOW_MESSAGE",GetContent:"GET_CONTENT",GetPageInfo:"GET_PAGE_INFO",Ping:"PING",AddIframeContent:"ADD_IFRAME_CONTENT",RefreshDarkMode:"REFRESH_DARK_MODE",GetAuthToken:"GET_AUTH_TOKEN"},window.DONT_REMOVE_ELEMENTS=["meta","script","title"],window.CREATE_ARTICLE_QUERY="mutation CreateArticle ($input: CreateArticleInput!){\n createArticle(input:$input){\n ... on CreateArticleSuccess{\n createdArticle{\n id\n title\n slug\n hasContent\n }\n user {\n id\n profile {\n id\n username\n }\n }\n}\n ... on CreateArticleError{\n errorCodes\n }\n}\n}",window.CREATE_ARTICLE_SAVING_REQUEST_QUERY="mutation CreateArticleSavingRequest ($input: CreateArticleSavingRequestInput!){\n createArticleSavingRequest(input:$input){\n ... on CreateArticleSavingRequestSuccess{\n articleSavingRequest{\n id\n }\n }\n ... on CreateArticleSavingRequestError{\n errorCodes\n }\n }\n}"; \ No newline at end of file +"use strict";window.browserApi="object"==typeof chrome&&chrome&&chrome.runtime&&chrome||"object"==typeof browser&&browser||{},window.browserActionApi=browserApi.action||browserApi.browserAction||browserApi.pageAction,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']"],TITLE:["head > meta[property='og:title']"]},window.ACTIONS={ShowMessage:"SHOW_MESSAGE",GetContent:"GET_CONTENT",GetPageInfo:"GET_PAGE_INFO",Ping:"PING",AddIframeContent:"ADD_IFRAME_CONTENT",RefreshDarkMode:"REFRESH_DARK_MODE",GetAuthToken:"GET_AUTH_TOKEN"},window.DONT_REMOVE_ELEMENTS=["meta","script","title"],window.CREATE_ARTICLE_QUERY="mutation CreateArticle ($input: CreateArticleInput!){\n createArticle(input:$input){\n ... on CreateArticleSuccess{\n createdArticle{\n id\n title\n slug\n hasContent\n }\n user {\n id\n profile {\n id\n username\n }\n }\n}\n ... on CreateArticleError{\n errorCodes\n }\n}\n}",window.CREATE_ARTICLE_SAVING_REQUEST_QUERY="mutation CreateArticleSavingRequest ($input: CreateArticleSavingRequestInput!){\n createArticleSavingRequest(input:$input){\n ... on CreateArticleSavingRequestSuccess{\n articleSavingRequest{\n id\n }\n }\n ... on CreateArticleSavingRequestError{\n errorCodes\n }\n }\n}"; \ No newline at end of file diff --git a/apple/Sources/SafariExtension/Resources/scripts/content/prepare-content.js b/apple/Sources/SafariExtension/Resources/scripts/content/prepare-content.js index f36aa3b1c..0976f985e 100644 --- a/apple/Sources/SafariExtension/Resources/scripts/content/prepare-content.js +++ b/apple/Sources/SafariExtension/Resources/scripts/content/prepare-content.js @@ -1 +1 @@ -"use strict";!function(){const e={};function t(t){const n=t.tagName.toLowerCase();if("iframe"===n){const n=e[t.src];if(!n)return;const o=document.createElement("div");o.className="omnivore-instagram-embed",o.innerHTML=n;const r=t.parentNode;if(!r)return;return void r.replaceChild(o,t)}if("img"===n||"image"===n){if(-1===window.getComputedStyle(t).getPropertyValue("filter").indexOf("blur("))return;return void t.remove()}const o=window.getComputedStyle(t),r=o.getPropertyValue("background-image");if(r&&"none"!==r)return;const i=o.getPropertyValue("filter");if(i&&-1!==i.indexOf("blur("))return void t.remove();if(t.src)return;if(t.innerHTML.length>24)return;const a=/url\("(.+?)"\)/gi,c=a.exec(r);a.lastIndex=0;const l=c&&c[1];if(!l)return;const s=document.createElement("img");s.src=l;const p=t.parentNode;p&&p.replaceChild(s,t)}function n(){const e=document.createElement("div");e.style.position="absolute",e.style.left="-2000px",e.style.zIndex="-2000",e.innerHTML=document.body.innerHTML,document.documentElement.appendChild(e),console.log("\n\nStarting evaluation!"),Array.from(e.getElementsByTagName("*")).forEach(t);const n=`${document.head.innerHTML}${e.innerHTML}`;return e.remove(),n}function o(){const e=document.querySelectorAll(".webext-omnivore-backdrop");for(let t=0;t{for(let t=0;t{if(t!==ACTIONS.AddIframeContent)return;const{url:i,content:a}=n;e[i]=a,r({})})),window.prepareContent=async function(){const e=await async function(){const e=".pdf"===window.location.pathname.slice(-4).toLowerCase(),t=-1!==["application/acrobat","application/pdf","application/x-pdf","applications/vnd.pdf","text/pdf","text/x-pdf"].indexOf(document.contentType);if(!e&&!t)return Promise.resolve(null);const n=document.querySelector("embed");return n&&"application/pdf"!==n.type?Promise.resolve(null):new Promise(((e,t)=>{const n=new XMLHttpRequest;n.open("GET","",!0),n.responseType="blob",n.onload=function(n){200===this.status?e(URL.createObjectURL(this.response)):t(n)},n.send()}))}();return e?{type:"pdf",uploadContentObjUrl:e}:(await async function(){const e=document.scrollingElement||document.body,t=e.scrollTop,n=e.scrollHeight;o();const r=function(){const e=document.createElement("div");return e.className="webext-omnivore-backdrop",e.style.cssText="all: initial !important;\n position: fixed !important;\n top: 0 !important;\n right: 0 !important;\n bottom: 0 !important;\n left: 0 !important;\n z-index: 99999 !important;\n background: #fff !important;\n opacity: 0.8 !important;\n transition: opacity 0.3s !important;\n -webkit-backdrop-filter: blur(4px) !important;\n backdrop-filter: blur(4px) !important;\n ",e}();for(document.body.appendChild(r);e.scrollTop<=n-500;){const t=e.scrollTop;if(e.scrollTop+=500,await new Promise((e=>{setTimeout(e,10)})),e.scrollTop===t)break}e.scrollTop=t,await new Promise((e=>{setTimeout(e,10)}))}(),o(),{type:"html",content:n()})}}(); \ No newline at end of file +"use strict";!function(){const e={};function t(t){const n=t.tagName.toLowerCase();if("iframe"===n){const n=e[t.src];if(!n)return;const o=document.createElement("div");o.className="omnivore-instagram-embed",o.innerHTML=n;const r=t.parentNode;if(!r)return;return void r.replaceChild(o,t)}if("img"===n||"image"===n){if(-1===window.getComputedStyle(t).getPropertyValue("filter").indexOf("blur("))return;return void t.remove()}const o=window.getComputedStyle(t),r=o.getPropertyValue("background-image");if(r&&"none"!==r)return;const i=o.getPropertyValue("filter");if(i&&-1!==i.indexOf("blur("))return void t.remove();if(t.src)return;if(t.innerHTML.length>24)return;const a=/url\("(.+?)"\)/gi,c=a.exec(r);a.lastIndex=0;const l=c&&c[1];if(!l)return;const s=document.createElement("img");s.src=l;const p=t.parentNode;p&&p.replaceChild(s,t)}function n(){const e=document.createElement("div");e.style.position="absolute",e.style.left="-2000px",e.style.zIndex="-2000",e.innerHTML=document.body.innerHTML,document.documentElement.appendChild(e),Array.from(e.getElementsByTagName("*")).forEach(t);const n=`${document.head.innerHTML}${e.innerHTML}`;return e.remove(),n}function o(){const e=document.querySelectorAll(".webext-omnivore-backdrop");for(let t=0;t{for(let t=0;t{if(t!==ACTIONS.AddIframeContent)return;const{url:i,content:a}=n;e[i]=a,r({})})),window.prepareContent=async function(){return await async function(){const e=".pdf"===window.location.pathname.slice(-4).toLowerCase(),t=-1!==["application/acrobat","application/pdf","application/x-pdf","applications/vnd.pdf","text/pdf","text/x-pdf"].indexOf(document.contentType);if(!e&&!t)return Promise.resolve(null);const n=document.querySelector("embed");return n&&"application/pdf"!==n.type?Promise.resolve(null):ENV_DOES_NOT_SUPPORT_BLOB_URL_ACCESS&&n.src?Promise.resolve({type:"url",uploadContentObjUrl:n.src}):new Promise(((e,t)=>{const n=new XMLHttpRequest;n.open("GET","",!0),n.responseType="blob",n.onload=function(n){200===this.status?e({type:"pdf",uploadContentObjUrl:URL.createObjectURL(this.response)}):t(n)},n.send()}))}()||(await async function(){const e=document.scrollingElement||document.body,t=e.scrollTop,n=e.scrollHeight;o();const r=function(){const e=document.createElement("div");return e.className="webext-omnivore-backdrop",e.style.cssText="all: initial !important;\n position: fixed !important;\n top: 0 !important;\n right: 0 !important;\n bottom: 0 !important;\n left: 0 !important;\n z-index: 99999 !important;\n background: #fff !important;\n opacity: 0.8 !important;\n transition: opacity 0.3s !important;\n -webkit-backdrop-filter: blur(4px) !important;\n backdrop-filter: blur(4px) !important;\n ",e}();for(document.body.appendChild(r);e.scrollTop<=n-500;){const t=e.scrollTop;if(e.scrollTop+=500,await new Promise((e=>{setTimeout(e,10)})),e.scrollTop===t)break}e.scrollTop=t,await new Promise((e=>{setTimeout(e,10)}))}(),o(),{type:"html",content:n()})}}(); \ No newline at end of file diff --git a/pkg/extension/src/manifest.json b/pkg/extension/src/manifest.json index fefcfcae4..180c8db5b 100644 --- a/pkg/extension/src/manifest.json +++ b/pkg/extension/src/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "process.env.EXTENSION_NAME", "short_name": "process.env.EXTENSION_NAME", - "version": "0.1.20", + "version": "0.1.22", "description": "Save articles to your Omnivore library", "author": "Omnivore Media, Inc", "default_locale": "en", diff --git a/pkg/extension/src/scripts/background.js b/pkg/extension/src/scripts/background.js index 3c4969c29..9a93ef912 100644 --- a/pkg/extension/src/scripts/background.js +++ b/pkg/extension/src/scripts/background.js @@ -73,6 +73,10 @@ function uploadFile ({ id, uploadSignedUrl }, contentType, contentObjUrl) { }; xhr.send(blob); }); + }) + .catch((err) => { + console.error('error uploading file', err) + return undefined }); } @@ -260,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 } } @@ -434,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, { @@ -467,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 () {