/*************************************************************************
* 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.
**************************************************************************/
let initDcLocalStoragePromise;const landingURL=document.URL;let currentURL=document.URL,isBackForwardNavigation=!1,scrolledURLObj={};function isGoogleQuery(e){if(!e)return!1;try{const t=new URL(e);if(t.host.startsWith("www.google.")||t.host.startsWith("www.bing."))return!0}catch(e){return!1}return!1}function isSupportedBrowserVersion(){const e=navigator.userAgent.match(/Chrome\/([0-9]+)/);return!(e&&e.length>=2)||+e[1]>=SETTINGS.SUPPORTED_VERSION}function checkForThirdPartyCookiesStatus(e){const t=document.createElement("iframe");t.id="third-party-cookies-checker",t.style.display="none",t.src=chrome.runtime.getURL("browser/js/check-cookies.html");const o=(n,r,i)=>{"thirdPartyCookiesChecked"===n.content_op&&(t.remove(),chrome.runtime.onMessage.removeListener(o),e&&e(n.status))};chrome.runtime.onMessage.addListener(o),document.documentElement.appendChild(t)}window.shouldUseReactFAB=async function(){return await initDcLocalStorage(),!0===window.dcLocalStorage?.getItem("enableReactFab")},window.getFABManager=async function(){if(!await shouldUseReactFAB())return new FABManager;try{return(await import(chrome.runtime.getURL("dist/FloatingActionButton.js"))).default}catch(e){}return new FABManager};const functionThrottler=(e,t)=>{let o=0;return function(...n){const r=Date.now();r-o>=t&&(o=r,e.apply(this,n))}};window.addEventListener("popstate",()=>{isBackForwardNavigation=!0,setTimeout(()=>{isBackForwardNavigation=!1},500)});const isPageScrolledByHashRoute=()=>{const e=new URL(document.URL),t=new URL(landingURL),o=e.origin+e.pathname,n=t.origin+t.pathname,r=e.hash.slice(1);return!!r&&(o===n&&(document.getElementById(r)||document.querySelector(`a[name="${r}"]`)))},detectURLChangeWhileScrolling=async()=>{if(document.URL!==currentURL){if(currentURL=document.URL,isPageScrolledByHashRoute()||isBackForwardNavigation||window.scrollY<5)return;scrolledURLObj={[currentURL]:!0}}};window.addEventListener("scroll",()=>detectURLChangeWhileScrolling());const throttleDetectURLChangeWhileScrolling=functionThrottler(detectURLChangeWhileScrolling,300),renderGenAIWebpageFAB=()=>{$(document).ready(()=>{!async function(){if(isPageScrolledByHashRoute()&&window.dcLocalStorage.getItem("enableURLHashScrollingFeature"))return;throttleDetectURLChangeWhileScrolling();const e=await GenAIWebpageEligibilityService.shouldShowTouchpoints();if(await shouldUseReactFAB()||window.dcLocalStorage.getItem("genAiWebpageCoachmarkData")){const t=await getFABManager();e?(await initDcLocalStoragePromise,t.renderFAB({enableHoveredState:!window.dcLocalStorage.getItem("fabHoveredStateShown")}),window.dcLocalStorage.setItem("fabHoveredStateShown",!0)):t.removeFAB()}chrome.runtime.sendMessage({type:"send_to_sidepanel",main_op:"page_switched",qualified:e,url:document.URL})}()})},throttledRenderGenAIWebpageFAB=functionThrottler(renderGenAIWebpageFAB,500),initDcLocalStorageHelper=async()=>{const e=chrome.runtime.getURL("./common/local-storage.js"),{dcLocalStorage:t}=await import(e);return await t.init(),t},initDcLocalStorage=async()=>(initDcLocalStoragePromise||(initDcLocalStoragePromise=initDcLocalStorageHelper()),window.dcLocalStorage||(window.dcLocalStorage=await initDcLocalStoragePromise),window.dcLocalStorage);function getCleanedHtml(){try{const e=document.cloneNode(!0),t=new Set(["script","style","iframe","head","meta","noscript","template","link","colgroup","col","track","param","source","slot","base","map","embed","area","applet"]),o=new Set(["href","title","src","id","alt"]);t.forEach(t=>{e.querySelectorAll(t).forEach(e=>e.remove())});const n=e.createTreeWalker(e.documentElement,NodeFilter.SHOW_ELEMENT,null);for(;n.nextNode();){const e=n.currentNode;[...e?.attributes||[]].forEach(t=>{o.has(t.name)||e.removeAttribute(t.name)})}return e.documentElement.outerHTML}catch(e){return document.documentElement.outerHTML}}function PromiseTimeout(e,t={}){const o=new Promise(e=>{const o=setTimeout(()=>{clearTimeout(o),e(t?.defaultVal)},t?.timeout)});return Promise.race([e,o])}function isInvalidContentScript(){return!chrome.runtime?.id}function Deferred(){let e,t;return{promise:new Promise((o,n)=>{e=o,t=n}),resolve:e,reject:t}}function getScrolledContent(){let e="";try{e=Viewport.getHtmlContent()||"",chrome.runtime.sendMessage({main_op:"log-info",log:{message:e?"Scrolled content found":"Scrolled content not found"}})}catch(e){chrome.runtime.sendMessage({main_op:"log-error",log:{message:"Error occurred while getting scrolled content"}})}return e}function isElementClickable(e){if(!e)return!1;const t=e.tagName?.toLowerCase(),o="pointer"===window.getComputedStyle(e).cursor;return["button","a","input","label","textarea","select"].includes(t)||e.hasAttribute("onclick")||"button"===e.getAttribute("role")||e.tabIndex>=0||o}function getOverlappingElement(e,t){if(!e&&!t)return[];const o=new Set,n=t||e.getBoundingClientRect(),r=[[n.left+n.width/2,n.top+n.height/2],[n.left+1,n.top+1],[n.right-1,n.top+1],[n.left+1,n.bottom-1],[n.right-1,n.bottom-1],[n.left+n.width/2,n.top+1],[n.left+n.width/2,n.bottom-1],[n.left+1,n.top+n.height/2],[n.right-1,n.top+n.height/2]];for(const[t,n]of r){const r=getDOMElementsAtPoint(t,n);for(const t of r)t===e||e?.contains(t)||o.add(t)}return Array.from(o)}function getClickableOverlappingElement(e,t){if(!e&&!t)return[];const o=new Set,n=getOverlappingElement(e,t);for(const e of n)isElementClickable(e)&&o.add(e);return Array.from(o)}function getDOMElementsAtPoint(e,t){const o=document.elementsFromPoint(e,t),n=getShadowDOMElements(e,t,o),r=new Set([...o,...n]);return Array.from(r)}function getShadowDOMElements(e,t,o){const n=[];for(const r of o)if(r.shadowRoot)try{const o=r.shadowRoot.elementsFromPoint(e,t);o&&o.length>0&&n.push(...o)}catch(e){chrome.runtime.sendMessage({main_op:"log-error",log:{message:"Error while getting shadow DOM elements",error:e.toString()}})}return n}initDcLocalStorage(),chrome.runtime.onMessage.addListener((e,t,o)=>{if("removeActionableCoachmark"===e.type){(new ActionableCoachmark).remove().catch(e=>console.error("Error removing actionable coachmark:",e))}return(async()=>{switch(e.main_op){case"getHtmlContent":const t=window.dcLocalStorage.getItem("enableScrolledContentIngestionInGenAI")&&scrolledURLObj[document.URL]?getScrolledContent():"",n=window.initialQuestion||initialQuestion;window.initialQuestion=void 0,o({htmlContent:getCleanedHtml(),url:document.URL,initialQuestion:n,disqualified:!await GenAIWebpageEligibilityService.isCurrentWebPageReadable(),htmlContentForDocOverview:t,textContent:document.documentElement.innerText});break;case"highlightText":{const t=new AttributionManager;o(await t.highlightText(e));break}case"canHighlightText":{const t=new AttributionManager;o(await t.canHighlightText(e));break}case"sanitiseSources":{const t=new AttributionManager;o(await t.sanitiseSources(e));break}case"removeHighlights":(new AttributionManager).removeHighlights();break;case"shouldDisableGenAIForWebPagesTPs":o(await GenAIWebpageEligibilityService.shouldDisableTouchpoints());break;case"shouldShowGenAIForWebPagesTPs":o(await GenAIWebpageEligibilityService.shouldShowTouchpoints());break;case"rerunGenAiWebpage":GenAIWebpageEligibilityService.reset(),await initDcLocalStorage(),throttledRenderGenAIWebpageFAB();break;case"shouldShowAddWebpageToProjectTouchpoint":o(await KWEligibilityService.shouldShowAddWebpageToProjectTouchpoint(e))}})(),!0});