/*************************************************************************
* ADOBE CONFIDENTIAL
* ___________________
*
*  Copyright 2015 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any.  The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and are protected by all applicable intellectual property laws,
* including trade secret and or copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/
class ChatGPTExpressIntegration{previewEntryPointId="chatgpt-preview-adobe-express-entry-point";chatViewEntryPointId="chatgpt-chatview-adobe-express-entry-point";webImagesPreviewEntryPointId="chatgpt-webimagespreview-adobe-express-entry-point";imageContainerClassname=["flex gap-1 md:gap-2"];imageElementClassname=["h-full max-h-full w-auto max-w-full rounded object-contain"];chatViewThumbnailImageContainerXpath=["//img[contains(@class, 'top-0') and contains(@class, 'z-1') and contains(@class, 'w-full')]/ancestor::div[contains(@class, 'w-full') and contains(@class, 'min-w-0') and contains(@class, 'flex-col') and contains(@class, 'agent-turn')][1]//div[contains(@class, 'gap-y-4') and contains(@class, 'p-1')]"];chatViewThumbnailImageElementClassname=["top-0 z-1 w-full"];webImagesPreviewImageContainerClassname=["px-1 pt-1","flex items-center sm:end-6 sm:top-6"];webImagesPreviewImageElementClassname=["h-full max-w-full object-contain","h-full max-w-[100vw] object-contain sm:max-w-[calc(100vw-10rem)]"];editSelectionModeTouchpointSelector=["//*[@id='"+this.previewEntryPointId+"'][following-sibling::*[@class='text-center font-semibold']]"];touchpoint="chatgptPreview";chatViewTouchpoint="chatgptChatView";webImagesPreviewTouchpoint="chatgptWebImagesPreview";chatgptExpressStateKey="chatgptExpressState";chatViewTooltips=[];enableConnectedAppBasedModeration=()=>{this.addApiResponseListener(),this.injectApiInterceptor()};injectApiInterceptor=()=>{const e=this.config.enableConnectedAppBasedModeration?.config?.apis;sessionStorage.setItem("adobeAcrobatExpressConnectedAppAPIs",JSON.stringify(e));const t=document.createElement("script");t.src=chrome.runtime.getURL("content_scripts/express/express_chatgpt/chatgpt-inject.js"),t.onload=()=>{t.remove()},document.head?.appendChild(t)};addApiResponseListener=()=>{document.addEventListener("acrobat-chatgpt-api-response",e=>{this.handleApiResponse(e.detail)})};handleApiResponse=e=>{if(e.success)try{const t=JSON.parse(e.responseData),s=this.config.enableConnectedAppBasedModeration?.config?.connectors;if(!t?.links||!Array.isArray(t.links)||0===t.links.length)return;if(!s||!Array.isArray(s)||0===s.length)return;const i=t.links.find(e=>"ENABLED"===e.connector_status&&(s.includes(e.connector_id)||s.includes(e.connector_name)));!!i?(this.setStatusOfConnectedApp(!0),this.expressUtils?.sendAnalyticsEventOncePerDay([["DCBrowserExt:Express:ChatGPT:ConnectedAppDetected"]]),this.mObserver.disconnect(),this.cleanup()):this.setStatusOfConnectedApp(!1)}catch(e){this.sendErrorLog("Error processing ChatGPT API response",e.message||e.toString())}else this.sendErrorLog("ChatGPT API interception failed",e.error||"Unknown error")};setStatusOfConnectedApp=e=>{localStorage.setItem("adobeAcrobatExpressConnectedAppStatus",e)};getStatusOfConnectedApp=()=>localStorage.getItem("adobeAcrobatExpressConnectedAppStatus");launchExpress=(e,t,s)=>{chrome.runtime.sendMessage({main_op:"launch-express",imgUrl:e,intent:s,touchpoint:t})};sendErrorLog=(e,t)=>{chrome.runtime.sendMessage({main_op:"log-error",log:{message:e,error:t}})};async loadContentScripts(){const e=chrome.runtime.getURL("content_scripts/express/single-click-cta.js"),t=chrome.runtime.getURL("content_scripts/express/express-utils.js"),s=await Promise.all([import(e),import(t)]);this.ExpressCTAClass=s[0].default,this.expressUtils=s[1].default,this.expressCTA=new this.ExpressCTAClass}addImageObserver=()=>{if(!document.body)return void setTimeout(()=>this.addImageObserver(),100);"true"!==this.getStatusOfConnectedApp()&&(this.mObserver=new MutationObserver(e=>{this.imagePreviewerObserverHandler(),this.chatViewObserverHandler(),this.webImagesPreviewObserverHandler()}),this.mObserver.observe(document.body,{childList:!0,subtree:!0}))};previewEntryPointClickHandler=e=>{if(!chrome?.runtime?.id)return void this.removeAllTouchpoints();const t=this.expressUtils.getElementsFromClassNames(document,this.imageElementClassname)[0];if(!t||"IMG"!==t.tagName)return void this.sendErrorLog("Error executing express in ChatGPT","image element not found");const s=t.src;s?(this.setTouchpointClickedForFteState(this.touchpoint),this.launchExpress(s,this.touchpoint,e)):this.sendErrorLog("Error executing express in ChatGPT","image URL not found")};chatViewEntryPointClickHandler=(e,t)=>{if(!chrome?.runtime?.id)return void this.removeAllTouchpoints();const s=this.expressUtils.getElementsFromClassNames(e.parentElement?.parentElement,this.chatViewThumbnailImageElementClassname)[0];if(!s||"IMG"!==s.tagName)return void this.sendErrorLog("Error executing express in ChatGPT chat view","image element not found");const i=s.src;i?(this.setTouchpointClickedForFteState(this.chatViewTouchpoint),this.launchExpress(i,this.chatViewTouchpoint,t)):this.sendErrorLog("Error executing express in ChatGPT chat view","image URL not found")};webImagesPreviewEntryPointClickHandler=e=>{if(!chrome?.runtime?.id)return void this.removeAllTouchpoints();const t=this.getActiveWebImagesPreviewImage();if(!t||"IMG"!==t.tagName)return void this.sendErrorLog("Error executing express in ChatGPT web images preview","image element not found");const s=t.src;s?(this.setTouchpointClickedForFteState(this.webImagesPreviewTouchpoint),this.launchExpress(s,this.webImagesPreviewTouchpoint,e)):this.sendErrorLog("Error executing express in ChatGPT web images preview","image URL not found")};getActiveWebImagesPreviewImage=()=>{const e=this.expressUtils.getElementsFromClassNames(document,this.webImagesPreviewImageElementClassname);for(const t of e){if(!t||"IMG"!==t.tagName)continue;const e=t.parentElement;if(e&&"button"!==e.getAttribute("role"))return t}const t=this.expressUtils.getElementsFromClassNames(document,this.webImagesPreviewImageElementClassname);return t&&"IMG"===t.tagName?t:null};setTouchpointClickedForFteState=async e=>{this.removeContextualFte();let t=await chrome.storage.local.get(this.chatgptExpressStateKey);switch(t=t?.chatgptExpressState?t.chatgptExpressState:{},e){case this.touchpoint:t.previewTouchpointUsed=!0;break;case this.chatViewTouchpoint:t.chatViewTouchpointUsed=!0;break;case this.webImagesPreviewTouchpoint:t.webImagesPreviewTouchpointUsed=!0}chrome.storage.local.set({[this.chatgptExpressStateKey]:t})};imagePreviewerObserverHandler=()=>{if(!this.config.enableChatGPTPreviewExpressMenu)return;if(!chrome?.runtime?.id)return void this.removeAllTouchpoints();this.removeTooltipIfFteRendered();const e=this.expressUtils.getElementsFromClassNames(document,this.imageContainerClassname)[0];if(!e)return;if(document.getElementById(this.previewEntryPointId)){const e=this.expressUtils.getElementsFromXPath(document,[this.editSelectionModeTouchpointSelector])[0];return void(e&&e.remove())}this.addPreviewEntryPoint(e,this.previewEntryPointId,this.previewEntryPointClickHandler),this.expressUtils.sendAnalyticsEventOncePerDay([["DCBrowserExt:Express:ChatGPT:PreviewEntryPoint:Shown"]])};chatViewObserverHandler=()=>{if(!this.config.enableChatGPTChatViewExpressMenu)return;if(!chrome?.runtime?.id)return void this.removeAllTouchpoints();this.removeTooltipIfFteRendered();const e=this.expressUtils.getElementsFromXPath(document,this.chatViewThumbnailImageContainerXpath);for(const t of e){if(t.querySelector(`#${this.chatViewEntryPointId}`))continue;const e=this.expressUtils.getElementsFromClassNames(t.parentElement?.parentElement,this.chatViewThumbnailImageElementClassname)[0];e&&"IMG"===e.tagName&&(this.addChatViewEntryPoint(t,this.chatViewEntryPointId,this.chatViewEntryPointClickHandler),this.expressUtils.sendAnalyticsEventOncePerDay([["DCBrowserExt:Express:ChatGPT:ChatViewEntryPoint:Shown"]]))}};webImagesPreviewObserverHandler=()=>{if(!this.config.enableChatGPTWebImagesPreviewExpressMenu)return;if(!chrome?.runtime?.id)return void this.removeAllTouchpoints();this.removeTooltipIfFteRendered();const e=this.expressUtils.getElementsFromClassNames(document,this.webImagesPreviewImageContainerClassname)[0];if(this.expressUtils.getElementsFromClassNames(document,"cc440d50ba-express-entrypoint-button")[0])return;const t=this.getActiveWebImagesPreviewImage();e&&t&&"IMG"===t.tagName&&(this.addWebImagesPreviewEntryPoint(e,this.webImagesPreviewEntryPointId,this.webImagesPreviewEntryPointClickHandler),this.expressUtils.sendAnalyticsEventOncePerDay([["DCBrowserExt:Express:ChatGPT:WebImagesPreviewEntryPoint:Shown"]]))};addPreviewEntryPoint=async(e,t,s)=>{if(document.getElementById(t))return;const i=await this.expressCTA.renderMenuButton(s,this.touchpoint);i.id=t,e.insertBefore(i,e.firstChild),this.tooltip=this.expressCTA.attachTooltip("bottom"),chrome.runtime.sendMessage({main_op:"reRenderShowOneChild"})};addChatViewEntryPoint=async(e,t,s)=>{if(this.expressUtils.getElementsFromClassNames(e,"cc440d50ba-express-entrypoint-button")[0])return;const i=await this.expressCTA.renderMenuButton(t=>{s(e,t)},this.chatViewTouchpoint);i.id=t;const n=this.expressUtils.getElementsFromClassNames(e.parentElement?.parentElement,this.chatViewThumbnailImageElementClassname)[0];let r=0;if(e.children.length>0){for(const t of e.children)r+=t.offsetWidth;e.insertAdjacentElement("beforeend",i),i.style.position="relative",i.style.left=n.offsetWidth-i.offsetWidth-r+"px",this.chatViewTooltips.push(this.expressCTA.attachTooltip("bottom")),chrome.runtime.sendMessage({main_op:"reRenderShowOneChild"})}};addWebImagesPreviewEntryPoint=async(e,t,s)=>{if(document.getElementById(t))return;const i=await this.expressCTA.renderMenuButton(s,this.webImagesPreviewTouchpoint);i.id=t;const n=(e,t)=>t.split(" ").filter(e=>""!==e.trim()).every(t=>e.contains(t));if(n(e.classList,this.webImagesPreviewImageContainerClassname[0]))e.insertAdjacentElement("beforeend",i);else{if(!n(e.classList,this.webImagesPreviewImageContainerClassname[1]))return;i.classList.add("chatgptWebImagesPreviewGreyBackground"),e.insertBefore(i,e.firstChild)}this.webImagesPreviewTooltip=this.expressCTA.attachTooltip("bottom"),chrome.runtime.sendMessage({main_op:"reRenderShowOneChild"})};removeAllTouchpoints=()=>{const e=this.expressUtils.getElementsFromClassNames(document,"cc440d50ba-express-entrypoint-button");for(const t of e)t.remove()};cleanup=()=>{this.removeAllTouchpoints(),this.cleanupTooltips(),this.removeContextualFte()};cleanupTooltips=()=>{this.tooltip?.tooltip?.remove(),this.tooltip=null;for(const e of this.chatViewTooltips)e?.tooltip?.remove();this.chatViewTooltips=[],this.webImagesPreviewTooltip?.tooltip?.remove(),this.webImagesPreviewTooltip=null};removeContextualFte=()=>{const e=document.getElementById("express-contextual-fte");e&&e.remove()};removeTooltipIfFteRendered=()=>{document.getElementById("express-contextual-fte")&&this.cleanupTooltips()};init=async()=>{const e=await chrome.runtime.sendMessage({main_op:"chatgpt-express-init"});this.config=e,(this.config.enableChatGPTPreviewExpressMenu||this.config.enableChatGPTChatViewExpressMenu||this.config.enableChatGPTWebImagesPreviewExpressMenu)&&(this.config.enableConnectedAppBasedModeration?.enabled&&this.enableConnectedAppBasedModeration(),this.imageContainerClassname=this.config.selectors?.imageContainerClassname||this.imageContainerClassname,this.imageElementClassname=this.config.selectors?.imageElementClassname||this.imageElementClassname,this.chatViewThumbnailImageContainerXpath=this.config.selectors?.chatViewThumbnailImageContainerXpath||this.chatViewThumbnailImageContainerXpath,this.chatViewThumbnailImageElementClassname=this.config.selectors?.chatViewThumbnailImageElementClassname||this.chatViewThumbnailImageElementClassname,this.webImagesPreviewImageContainerClassname=this.config.selectors?.webImagesPreviewImageContainerClassname||this.webImagesPreviewImageContainerClassname,this.webImagesPreviewImageElementClassname=this.config.selectors?.webImagesPreviewImageElementClassname||this.webImagesPreviewImageElementClassname,this.editSelectionModeTouchpointSelector=this.config.selectors?.editSelectionModeTouchpointSelector||this.editSelectionModeTouchpointSelector,await this.loadContentScripts(),this.addImageObserver())}}const chatgptExpressIntegration=new ChatGPTExpressIntegration;chatgptExpressIntegration.init();