Files
omnivore/pkg/extension-v3/extension/content.js
Jackson Harper 2ec1d6f58c Initial prototype of the V3 manifest extension (#4357)
* Initial prototype of the V3 manifest extension

* Make sure the content script is only injected once

* Implement addNote button

* More separation work for tasks, implement archive and update note

* Add back missing functionality, add guide to install Extensions

* Revert v2 changes

---------

Co-authored-by: Thomas Rogers <Podginator@gmail.com>
2025-02-23 16:35:41 +01:00

1 line
17 KiB
JavaScript

(()=>{"use strict";var e={322:(e,o,t)=>{t.d(o,{W:()=>s});var n=t(454),i=t(782),r=function(e,o,t,n){return new(t||(t=Promise))((function(i,r){function s(e){try{l(n.next(e))}catch(e){r(e)}}function d(e){try{l(n.throw(e))}catch(e){r(e)}}function l(e){var o;e.done?i(e.value):(o=e.value,o instanceof t?o:new t((function(e){e(o)}))).then(s,d)}l((n=n.apply(e,o||[])).next())}))};function s(){var e;return r(this,void 0,void 0,(function*(){(0,i.ig)();const o=document.querySelector("#omnivore-extension-root");if(!o||!o.shadowRoot)return void console.log("no statusBox to update");let t=yield(0,n.$)("labels").then((e=>{if(e&&e.length>0)return e}));(0,i.jC)("#omnivore-edit-labels-row"),null===(e=o.shadowRoot.querySelector("#omnivore-edit-label-input"))||void 0===e||e.focus();const r=o.shadowRoot.querySelector("#omnivore-edit-labels-list");if(!t)return console.error("No labels found, trying to update the cache."),void chrome.runtime.sendMessage({action:"enqueueTask",task:"updateLabelCache",clientRequestId:(0,i.mm)()});r&&(r.innerHTML="",t.sort(((e,o)=>e.name.localeCompare(o.name,void 0,{sensitivity:"base"}))).forEach((function(e,o){const t=function(e){const o=document.createElement("button"),t=document.createElement("span");t.style="width:10px;height:10px;border-radius:1000px;",t.style.setProperty("background-color",e.color);const n=document.createElement("span");n.style="margin-left: 10px;pointer-events: none;",n.innerText=e.name;const i=document.createElement("span");return i.style="margin-left: auto;pointer-events: none;",i.className="checkbox",i.innerHTML='\n <svg width="14" height="11" viewBox="0 0 14 11" fill="none" xmlns="http://www.w3.org/2000/svg">\n <path d="M13.7411 1.75864L4.79692 10.7028L0.69751 6.60341L1.74845 5.55246L4.79692 8.59348L12.6902 0.707703L13.7411 1.75864Z" fill="#888888"/>\n </svg>\n ',o.appendChild(t),o.appendChild(n),o.appendChild(i),o.onclick=d,o.setAttribute("data-label-id",e.id),o.setAttribute("data-label-selected",e.selected?"on":"off"),o.setAttribute("tabIndex","-1"),o}(e);r.appendChild(t)})))}))}function d(e){var o;e.preventDefault();const t=null===(o=e.target)||void 0===o?void 0:o.getAttribute("data-label-id");t&&function(e,o){var t,s;r(this,void 0,void 0,(function*(){const r=null===(t=e.target)||void 0===t?void 0:t.getAttribute("data-label-selected");if(!o||!r)return;const d="on"!=r;null===(s=e.target)||void 0===s||s.setAttribute("data-label-selected",d?"on":"off");let l=yield(0,n.$)("labels").then((e=>{if(e&&e.length>0)return e}));if(!l)throw Error("No labels selected");l.find((e=>e.id===o))&&function(){var e;(0,i.PC)("#omnivore-edit-labels-status","waiting","Updating Labels...",void 0);const o=document.querySelector("#omnivore-extension-root"),t=null===(e=null==o?void 0:o.shadowRoot)||void 0===e?void 0:e.querySelector("#omnivore-edit-labels-list");if(t){const e=[...t.children].filter((e=>"on"===e.getAttribute("data-label-selected"))).map((e=>e.getAttribute("data-label-id")));chrome.runtime.sendMessage({action:"enqueueTask",task:"setLabels",clientRequestId:(0,i.mm)(),labels:e})}}()}))}(e,t)}},782:(e,o,t)=>{t.d(o,{Dz:()=>l,Jw:()=>m,PC:()=>f,Wj:()=>c,ig:()=>a,jC:()=>h,ll:()=>d,mm:()=>p});var n=t(454),i=t(322),r=function(e,o,t,n){return new(t||(t=Promise))((function(i,r){function s(e){try{l(n.next(e))}catch(e){r(e)}}function d(e){try{l(n.throw(e))}catch(e){r(e)}}function l(e){var o;e.done?i(e.value):(o=e.value,o instanceof t?o:new t((function(e){e(o)}))).then(s,d)}l((n=n.apply(e,o||[])).next())}))};const s={waiting:'<div class="loading-spinner"></div>',success:'\n <svg width="19" height="19" viewBox="0 0 19 19" fill="none" xmlns="http://www.w3.org/2000/svg">\n <path fill-rule="evenodd" clip-rule="evenodd" d="M9.91626 18.6047C14.8868 18.6047 18.9163 14.5752 18.9163 9.60468C18.9163 4.63411 14.8868 0.604675 9.91626 0.604675C4.9457 0.604675 0.91626 4.63411 0.91626 9.60468C0.91626 14.5752 4.9457 18.6047 9.91626 18.6047ZM9.91626 17.1046C14.0584 17.1046 17.4163 13.7468 17.4163 9.60463C17.4163 5.4625 14.0584 2.10463 9.91626 2.10463C5.77412 2.10463 2.41626 5.4625 2.41626 9.60463C2.41626 13.7468 5.77412 17.1046 9.91626 17.1046Z" fill="#32D74B"/>\n <path d="M13.3538 7.28851L8.7704 11.9209L6.47876 9.60469" stroke="#32D74B" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>\n </svg>',failure:'\n <svg width="19" height="19" viewBox="0 0 19 19" fill="none" xmlns="http://www.w3.org/2000/svg">\n <path fill-rule="evenodd" clip-rule="evenodd" d="M9.74048 18.5508C14.711 18.5508 18.7405 14.5213 18.7405 9.55078C18.7405 4.58022 14.711 0.550781 9.74048 0.550781C4.76992 0.550781 0.740479 4.58022 0.740479 9.55078C0.740479 14.5213 4.76992 18.5508 9.74048 18.5508ZM9.74048 17.0507C13.8826 17.0507 17.2405 13.6929 17.2405 9.55074C17.2405 5.4086 13.8826 2.05074 9.74048 2.05074C5.59834 2.05074 2.24048 5.4086 2.24048 9.55074C2.24048 13.6929 5.59834 17.0507 9.74048 17.0507Z" fill="#C7372F"/>\n <path d="M12.794 11.897L12.794 11.897L10.4474 9.55078L12.794 7.2046L12.794 7.20459C12.8878 7.11079 12.9405 6.98358 12.9405 6.85093C12.9405 6.71828 12.8878 6.59107 12.794 6.49727C12.7002 6.40348 12.573 6.35078 12.4403 6.35078C12.3077 6.35078 12.1805 6.40348 12.0867 6.49727L12.0867 6.49728L9.74048 8.84382L7.3943 6.49728L7.39429 6.49727C7.30049 6.40348 7.17328 6.35078 7.04063 6.35078C6.90798 6.35078 6.78077 6.40348 6.68697 6.49727C6.59317 6.59107 6.54048 6.71828 6.54048 6.85093C6.54048 6.98358 6.59317 7.11079 6.68697 7.20459L6.68698 7.2046L9.03351 9.55078L6.68698 11.897L6.68697 11.897C6.59317 11.9908 6.54048 12.118 6.54048 12.2506C6.54048 12.3833 6.59317 12.5105 6.68697 12.6043C6.78077 12.6981 6.90798 12.7508 7.04063 12.7508C7.17328 12.7508 7.30049 12.6981 7.39429 12.6043L7.3943 12.6043L9.74048 10.2577L12.0867 12.6043L12.0867 12.6043C12.1331 12.6507 12.1882 12.6876 12.2489 12.7127C12.3096 12.7378 12.3746 12.7508 12.4403 12.7508C12.506 12.7508 12.571 12.7378 12.6317 12.7127C12.6924 12.6876 12.7474 12.6507 12.7938 12.6043C12.8878 12.5105 12.9405 12.3833 12.9405 12.2506C12.9405 12.118 12.8878 11.9908 12.7938 11.897L12.7939 11.897H12.794Z" fill="#C7372F" stroke="#C7372F" stroke-width="0.3"/>\n </svg>\n '},d=e=>r(void 0,void 0,void 0,(function*(){var o;let t=null!==(o=document.querySelector("#omnivore-extension-root"))&&void 0!==o?o:void 0;document.body&&(console.log("existing currentToastEl: ",t),t||(t=yield r(void 0,void 0,void 0,(function*(){console.log("===== CREATING TOAST CONTAINER ===== ");const e=yield fetch(chrome.runtime.getURL("views/toast.html")),o=yield e.text(),t=document.createElement("div");if(t.tabIndex=0,t.id="omnivore-extension-root",t.attachShadow({mode:"open"}),t.style.opacity="1.0",!t.shadowRoot)return void alert("Error opening Omnivore user interface.");t.shadowRoot.innerHTML="<style>:host {all initial;}</style>";const n=document.createElement("div");return n.id="#omnivore-toast",n.innerHTML=o,n.tabIndex=0,t.shadowRoot.appendChild(n),document.body.appendChild(t),u(t),l("waiting"),t}))),(yield(0,n.$)("disableAutoDismiss"))&&(null==t||t.setAttribute("data-disable-auto-dismiss","true")),null==t||t.setAttribute("data-omnivore-client-request-id",e),null==t||t.focus({preventScroll:!0}),l("waiting"))})),l=(e,o=void 0)=>r(void 0,void 0,void 0,(function*(){var t;const n=document.querySelector("#omnivore-extension-root"),i=null===(t=null==n?void 0:n.shadowRoot)||void 0===t?void 0:t.querySelector(".omnivore-toast-statusBox");if(console.log("updating",e,i),i)switch(e){case"success":i.innerHTML=s.success;break;case"failure":i.innerHTML=s.failure;break;case"waiting":i.innerHTML=s.waiting}o&&("addNote"==o&&"failure"==e&&f("#omnivore-add-note-status","failure","Error adding note...",void 0),"addNote"==o&&"success"==e&&(f("#omnivore-add-note-status","success","Note updated.",2500),setTimeout((()=>{h("#omnivore-add-note-status")}),3e3)),"setLabels"==o&&"success"==e&&f("#omnivore-edit-labels-status","success","Labels Updated",2500),"setLabels"==o&&"failure"==e&&f("#omnivore-edit-labels-status","failure","Error Updating Labels...",2500),"editTitle"==o&&"failure"==e&&f("#omnivore-add-note-status","failure","Error updating title...",void 0),"editTitle"==o&&"success"==e&&(f("omnivore-edit-title-status","success","Title updated.",2500),setTimeout((()=>{h("#omnivore-edit-title-status")}),3e3)),"archive"==o&&(f("#omnivore-extra-status",e,"success"==e?"Success":"Error","success"==e?2500:void 0),"success"==e&&q()))})),a=()=>{const e=document.querySelector("#omnivore-extension-root");e&&e.setAttribute("data-disable-auto-dismiss","true")},c=e=>r(void 0,void 0,void 0,(function*(){e.status&&l(e.status);const o=yield r(void 0,void 0,void 0,(function*(){var e;const o=null!==(e=yield(0,n.$)("autoDismissTime"))&&void 0!==e?e:"2500";return Number.isNaN(Number(o))?2500:Number(o)}));setTimeout((()=>{const e=document.querySelector("#omnivore-extension-root");e&&!e.getAttribute("data-disable-auto-dismiss")&&(e.style.transition="opacity 3.5s ease;",e.style.opacity="0",setTimeout((()=>{const e=document.querySelector("#omnivore-extension-root");e&&!e.getAttribute("data-disable-auto-dismiss")&&e.remove()}),500))}),o)})),u=e=>{var o,t,n;const r=[{id:"#omnivore-toast-add-note-btn",func:w},{id:"#omnivore-toast-edit-title-btn",func:v},{id:"#omnivore-toast-edit-labels-btn",func:i.W},{id:"#omnivore-toast-read-now-btn",func:L},{id:"#omnivore-open-menu-btn",func:y},{id:"#omnivore-toast-close-btn",func:R},{id:"#omnivore-toast-login-btn",func:b},{id:"#omnivore-toast-archive-btn",func:x},{id:"#omnivore-toast-delete-btn",func:C}];for(const t of r){const n=null===(o=e.shadowRoot)||void 0===o?void 0:o.querySelector(t.id);n&&(console.log(t.id),n.addEventListener("click",t.func))}if(window.matchMedia("(max-width: 500px)").matches){const o=null===(t=e.shadowRoot)||void 0===t?void 0:t.querySelectorAll(".omnivore-top-button-label");null==o||o.forEach((e=>{e.style.display="none"}));const i=null===(n=e.shadowRoot)||void 0===n?void 0:n.querySelector("#omnivore-toast-container");i&&(i.style.width="280px",i.style.top="unset",i.style.bottom="20px")}};function v(){var e,o,t;a(),h("#omnivore-edit-title-row");let n=null!==(e=document.querySelector("#omnivore-extension-root"))&&void 0!==e?e:void 0;if(!n)return void console.log("no statusBox to update");const i=null===(o=null==n?void 0:n.shadowRoot)||void 0===o?void 0:o.querySelector("#omnivore-edit-title-textarea");i&&(i.focus(),i.onkeydown=e=>{e.cancelBubble=!0});const r=null===(t=null==n?void 0:n.shadowRoot)||void 0===t?void 0:t.querySelector("#omnivore-edit-title-form");r?r.onsubmit=e=>{var o;f("#omnivore-edit-title-status","waiting","Updating title...",void 0);const t=null!==(o=null==i?void 0:i.value)&&void 0!==o?o:"";chrome.runtime.sendMessage({action:"enqueueTask",task:"editTitle",clientRequestId:p(),title:t}),e.preventDefault()}:console.log("no form to update")}const m=()=>{a(),l("failure"),h("#omnivore-logged-out-row"),g(),f("#omnivore-logged-out-status",void 0,"You are not logged in.",void 0)},f=(e,o,t,n)=>{var i;const r=document.querySelector("#omnivore-extension-root"),d=null===(i=null==r?void 0:r.shadowRoot)||void 0===i?void 0:i.querySelector(e),l=(()=>{switch(o){case"waiting":return s.animatedLoader;case"success":return s.success;case"failure":return s.failure;default:return}})();if(l&&d){const e="failure"==o?"red":"unset";d.innerHTML=`<span style='padding-right: 10px'>${l}</span><span style='line-height: 20px;color: ${e};text-decoration: none;'>${t}</span>`}else d&&(d.innerHTML=t);n&&d&&setTimeout((()=>{d.innerHTML=""}),n)},g=()=>{let e=document.querySelector("#omnivore-extension-root");["#omnivore-toast-edit-title-btn","#omnivore-toast-edit-labels-btn","#omnivore-toast-read-now-btn","#omnivore-toast-add-note-btn","#omnivore-open-menu-btn"].forEach((o=>{var t;const n=null===(t=null==e?void 0:e.shadowRoot)||void 0===t?void 0:t.querySelector(o);n&&(n.disabled=!0)}))},h=e=>{var o,t;let n=document.querySelector("#omnivore-extension-root");if(!n)return void console.log("toggleRow: no row to toggle");const i=null===(o=null==n?void 0:n.shadowRoot)||void 0===o?void 0:o.querySelector(e),r=null==i?void 0:i.getAttribute("data-state"),s=null===(t=null==n?void 0:n.shadowRoot)||void 0===t?void 0:t.querySelectorAll(".omnivore-toast-func-row");if(null==s||s.forEach((e=>{e.setAttribute("data-state","closed")})),i&&r){const e="open"===r?"closed":"open";i.setAttribute("data-state",e)}},p=()=>{const e=document.querySelector("#omnivore-extension-root");return null==e?void 0:e.getAttribute("data-omnivore-client-request-id")},b=()=>{window.open(new URL("/login",process.env.OMNIVORE_URL),"_blank"),q()},y=()=>{a(),h("#omnivore-extra-buttons-row")},w=()=>r(void 0,void 0,void 0,(function*(){var e,o,t;console.log("[omnivore] adding note"),a();const i=document.querySelector("#omnivore-extension-root"),s=null==i?void 0:i.getAttribute("data-omnivore-client-request-id");if(console.log("client request id: ",s),!s)return void f("#omnivore-add-note-status","failure","Error adding note...",void 0);const d=`cached-note-${document.location.href}`;a(),h("#omnivore-add-note-row");const l=null===(e=null==i?void 0:i.shadowRoot)||void 0===e?void 0:e.querySelector("#omnivore-add-note-textarea");if(l){if(d){const e=null!==(o=yield(0,n.$)(d))&&void 0!==o?o:"";l.value=e}l.value?l.select():l.focus(),l.addEventListener("input",(e=>r(void 0,void 0,void 0,(function*(){const o={};o[d]=e.target.value,yield(0,n.c)(o)})))),l.onkeydown=e=>r(void 0,void 0,void 0,(function*(){e.stopPropagation(),console.log("handling the enter key: ",e.keyCode),13==e.keyCode&&(e.metaKey||e.ctrlKey)&&(f("#omnivore-add-note-status","waiting","Adding note...",void 0),yield T(s,l.value))}))}const c=null===(t=null==i?void 0:i.shadowRoot)||void 0===t?void 0:t.querySelector("#omnivore-add-note-form");c&&(c.onsubmit=e=>r(void 0,void 0,void 0,(function*(){console.log("handling form submit"),f("#omnivore-add-note-status","waiting","Adding note...",void 0),l&&(yield T(s,l.value)),e.preventDefault(),e.stopPropagation()})))})),x=e=>r(void 0,void 0,void 0,(function*(){const o=p();try{yield chrome.runtime.sendMessage({action:"enqueueTask",task:"archive",clientRequestId:o})}catch(e){console.log("error archiving item")}e.preventDefault()})),C=e=>r(void 0,void 0,void 0,(function*(){const o=p();try{yield chrome.runtime.sendMessage({action:"enqueueTask",task:"delete",clientRequestId:o})}catch(e){console.log("error archiving item")}e.preventDefault()})),L=()=>r(void 0,void 0,void 0,(function*(){var e;a();let o=document.querySelector("#omnivore-extension-root");const t=null===(e=null==o?void 0:o.shadowRoot)||void 0===e?void 0:e.querySelector("#omnivore-toast-container");null==t||t.setAttribute("data-state","open"),window.open(new URL(`/article?url=${encodeURI(document.location.href)}`,yield(0,n.$)("omnivoreUrl")),"_blank"),q()})),q=()=>{setTimeout((()=>{R()}),1e3)},R=()=>{const e=document.querySelector("#omnivore-extension-root");e&&e.remove()},T=(e,o)=>r(void 0,void 0,void 0,(function*(){try{yield chrome.runtime.sendMessage({action:"enqueueTask",task:"addNote",note:o,clientRequestId:e})}catch(e){console.log("error adding note: ",e)}}))},454:(e,o,t)=>{t.d(o,{$:()=>n,c:()=>i});const n=e=>{return o=void 0,t=void 0,i=function*(){return new Promise((o=>{chrome.storage.local.get(e,(t=>{const n=t&&t[e]||null;o(n)}))}))},new((n=void 0)||(n=Promise))((function(e,r){function s(e){try{l(i.next(e))}catch(e){r(e)}}function d(e){try{l(i.throw(e))}catch(e){r(e)}}function l(o){var t;o.done?e(o.value):(t=o.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,d)}l((i=i.apply(o,t||[])).next())}));var o,t,n,i},i=e=>chrome.storage.local.set(e)}},o={};function t(n){var i=o[n];if(void 0!==i)return i.exports;var r=o[n]={exports:{}};return e[n](r,r.exports,t),r.exports}t.d=(e,o)=>{for(var n in o)t.o(o,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:o[n]})},t.o=(e,o)=>Object.prototype.hasOwnProperty.call(e,o);var n=t(782),i=t(322),r=function(e,o,t,n){return new(t||(t=Promise))((function(i,r){function s(e){try{l(n.next(e))}catch(e){r(e)}}function d(e){try{l(n.throw(e))}catch(e){r(e)}}function l(e){var o;e.done?i(e.value):(o=e.value,o instanceof t?o:new t((function(e){e(o)}))).then(s,d)}l((n=n.apply(e,o||[])).next())}))};const s=(e,o,t)=>r(void 0,void 0,void 0,(function*(){switch(console.log("[omnivore] content script message:",e),e.message){case"showLoggedOutToolbar":(0,n.Jw)(),t({success:!0});break;case"updateToolbar":(0,n.Dz)(e.status,e.task),t({success:!0});break;case"startToolbarDismiss":return(0,n.Wj)(e),void t({success:!0});case"updateLabelCache":(0,i.W)();break;default:return void t({success:!1})}}));chrome.runtime.onMessage.addListener(((e,o,t)=>r(void 0,void 0,void 0,(function*(){if("savePage"===e.message)return yield(o=e.clientRequestId,r(void 0,void 0,void 0,(function*(){console.log("[omnivore] v3 extension triggered: ",o),yield(e=>r(void 0,void 0,void 0,(function*(){chrome.runtime.onMessage.hasListener(s)||chrome.runtime.onMessage.addListener(s),yield(0,n.ll)(e)})))(o);const e=yield r(void 0,void 0,void 0,(function*(){const e=document.documentElement.outerHTML;return console.log("[omnivore] captured mainContent"),e}));console.log("[omnivore] collected page content: ",e);try{const t={clientRequestId:o,title:document.title,url:document.location.href,originalContent:e};yield chrome.runtime.sendMessage(Object.assign({action:"savePage"},t))}catch(e){console.log("error sending content: ",e)}}))),void t({success:!0});var o}))))})();