PageSourceSearch

https://cacow.com.br/cdn/shop/t/31/assets/cart-mode-guard.js?v=37109635108057582811783014857

js cacow.com.br collected 2026-10-04 23:46:03 UTC 8,458 bytes, 2 lines download raw bytes

1(function(){if(typeof window=="undefined"||window.__cacowCartGuardInstalled)return;window.__cacowCartGuardInstalled=!0;var CONFIG=window.cacowCartGuard||{},SUB_TAG=String(CONFIG.subscriptionTag||"assinatura").toLowerCase(),originalFetch=window.fetch.bind(window),guardInProgress=!1,STORAGE_KEY="cacow:product-tags",tagCache=new Map;try{var raw=sessionStorage.getItem(STORAGE_KEY);if(raw){var parsed=JSON.parse(raw);Object.keys(parsed||{}).forEach(function(handle){Array.isArray(parsed[handle])&&tagCache.set(handle,parsed[handle])})}}catch(e){}function persistCache(){try{var obj={};tagCache.forEach(function(tags,handle){obj[handle]=tags}),sessionStorage.setItem(STORAGE_KEY,JSON.stringify(obj))}catch(e){}}function fetchProductTags(handle){return handle?tagCache.has(handle)?Promise.resolve(tagCache.get(handle)):originalFetch.call(window,"/products/"+encodeURIComponent(handle)+".js",{headers:{Accept:"application/json"},credentials:"same-origin"}).then(function(res){if(!res.ok)throw new Error("Failed to fetch product "+handle);return res.json()}).then(function(product){var tags=Array.isArray(product&&product.tags)?product.tags:[];return tagCache.set(handle,tags),persistCache(),tags}).catch(function(){return tagCache.set(handle,[]),[]}):Promise.resolve([])}function isSubscriptionTags(tags){if(!Array.isArray(tags))return!1;for(var i=0;i<tags.length;i++)if(String(tags[i]).toLowerCase()===SUB_TAG)return!0;return!1}function isSubscriptionItem(item){return fetchProductTags(item&&item.handle).then(isSubscriptionTags)}function getFrequencyKey(item){if(!item)return null;if(Array.isArray(item.options_with_values))for(var i=0;i<item.options_with_values.length;i++){var opt=item.options_with_values[i];if(opt){var name=String(opt.name||"").toLowerCase();if(name.indexOf("frequ")!==-1||name.indexOf("plano")!==-1||name.indexOf("plan")!==-1){var value=String(opt.value||"").trim().toLowerCase();if(value)return value}}}var fallback=String(item.variant_title||"").trim().toLowerCase();return fallback||null}function getRequestInfo(args){var input=args[0],init=args[1]||{},url="",method=(init.method||"GET").toUpperCase(),body=init.body;return typeof input=="string"?url=input:input&&typeof input=="object"&&(url=input.url||"",input.method&&(method=String(input.method).toUpperCase())),{url:url,method:method,body:body}}function extractSectionsFromBody(body){if(!body)return null;try{if(typeof body=="string"){var parsed2=JSON.parse(body);return parsed2&&parsed2.sections?parsed2.sections:null}}catch(e){}return null}function isAddCartUrl(url){if(!url)return!1;try{var pathname;return/^https?:\/\//i.test(url)?pathname=new URL(url).pathname:pathname=url.split("?")[0],/\/cart\/add(?:\.js)?$/.test(pathname)}catch(e){return/\/cart\/add(?:\.js)?(?:\?|$)/.test(url)}}function changeCartLine(lineKey,quantity,sections){var body={id:lineKey,quantity:quantity};return sections&&(body.sections=sections),originalFetch.call(window,"/cart/change.js",{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json"},credentials:"same-origin",body:JSON.stringify(body)}).then(function(res){return res.json().catch(function(){return null})}).catch(function(){return null})}function dispatchCartRefresh(detail){document.dispatchEvent(new CustomEvent("theme:update:cart")),document.dispatchEvent(new CustomEvent("cacow:cart:guarded",{detail:detail||{}}))}function extractJustAdded(addResponse){return!addResponse||typeof addResponse!="object"?null:Array.isArray(addResponse.items)&&addResponse.items.length?addResponse.items[addResponse.items.length-1]:addResponse.variant_id?addResponse:null}function runGuard(justAdded,sectionsParam){return guardInProgress?Promise.resolve():(guardInProgress=!0,originalFetch.call(window,"/cart.js",{headers:{Accept:"application/json"},credentials:"same-origin"}).then(function(res){return res.json()}).then(function(cart){if(!(!cart||!Array.isArray(cart.items)||cart.items.length===0))return Promise.all(cart.items.map(function(item){return isSubscriptionItem(item).then(function(isSub){return{item:item,isSub:isSub,freqKey:isSub?getFrequencyKey(item):null}})})).then(function(classifications){return resolveJustAdded(justAdded,classifications).then(function(resolved){var justAddedIsSub=resolved.isSub,justAddedFreq=resolved.freqKey,justAddedItem=resolved.item,conflicting=classifications.filter(function(c){return justAddedItem&&c.item.key===justAddedItem.key?!1:justAddedIsSub?!!(!c.isSub||justAddedFreq&&c.freqKey&&c.freqKey!==justAddedFreq):c.isSub}).map(function(c){return c.item});if(conflicting.length!==0)return removeLines(conflicting,sectionsParam).then(function(lastResponse){var sectionsHtml=lastResponse&&lastResponse.sections?lastResponse.sections:null;dispatchCartRefresh({action:"cleared-opposite",keptSubscription:justAddedIsSub,keptFrequency:justAddedFreq,sections:sections
1Html})})})})}).catch(function(e){window.console&&console.warn("[cacow cart guard] error",e)}).then(function(){guardInProgress=!1}))}function resolveJustAdded(justAdded,classifications){if(justAdded&&justAdded.variant_id){var match=classifications.find(function(c){return c.item.variant_id===justAdded.variant_id});return match?Promise.resolve({item:match.item,isSub:match.isSub,freqKey:match.freqKey}):isSubscriptionItem(justAdded).then(function(isSub){return{item:null,isSub:isSub,freqKey:isSub?getFrequencyKey(justAdded):null}})}var last=classifications[classifications.length-1];return Promise.resolve({item:last.item,isSub:last.isSub,freqKey:last.freqKey})}function removeLines(items,sections){var lastResponse=null;return items.reduce(function(chain,item,index){return chain.then(function(){var sectionsForThis=sections&&index===items.length-1?sections:null;return changeCartLine(item.key,0,sectionsForThis).then(function(resp){return resp&&(lastResponse=resp),resp})})},Promise.resolve()).then(function(){return lastResponse})}function enforceMode(targetIsSubscription,options){if(guardInProgress)return Promise.resolve({removed:0,sections:null});guardInProgress=!0;var sectionsParam=options&&options.sections?options.sections:null;return originalFetch.call(window,"/cart.js",{headers:{Accept:"application/json"},credentials:"same-origin"}).then(function(res){return res.json()}).then(function(cart){return!cart||!Array.isArray(cart.items)||cart.items.length===0?{removed:0,sections:null}:Promise.all(cart.items.map(function(item){return isSubscriptionItem(item).then(function(isSub){return{item:item,isSub:isSub}})})).then(function(classifications){var conflicting=classifications.filter(function(c){return c.isSub!==targetIsSubscription}).map(function(c){return c.item});return conflicting.length===0?{removed:0,sections:null}:removeLines(conflicting,sectionsParam).then(function(lastResponse){var sectionsHtml=lastResponse&&lastResponse.sections?lastResponse.sections:null;return dispatchCartRefresh({action:"enforced-mode",keptSubscription:targetIsSubscription,sections:sections
1Html}),{removed:conflicting.length,sections:sectionsHtml}})})}).catch(function(e){return window.console&&console.warn("[cacow cart guard] enforceMode error",e),{removed:0,sections:null}}).then(function(result){return guardInProgress=!1,result})}window.fetch=function(){var args=Array.prototype.slice.call(arguments),info=getRequestInfo(args),shouldGuard=!guardInProgress&&info.method==="POST"&&isAddCartUrl(info.url),requestSections=shouldGuard?extractSectionsFromBody(info.body):null,promise=originalFetch.apply(window,args);return shouldGuard?promise.then(function(response){if(!response||!response.ok)return response;try{var clone=response.clone();clone.json().then(function(data){var justAdded=extractJustAdded(data);return runGuard(justAdded,requestSections)}).catch(function(){return runGuard(null,requestSections)})}catch(e){runGuard(null,requestSections)}return response}):promise},document.addEventListener("theme:cart:change",function(event){if(!guardInProgress){var detail=event&&event.detail;if(detail&&detail.type==="theme:product:add"){var stub=detail.variantId?{variant_id:detail.variantId}:null;runGuard(stub,null)}}}),window.cacowCartGuard=window.cacowCartGuard||{},window.cacowCartGuard.api={enforceSubscriptionOnly:function(options){return enforceMode(!0,options)},enforceRegularOnly:function(options){return enforceMode(!1,options)},isReady:!0},document.dispatchEvent(new CustomEvent("cacow:cart-guard:ready"))})();
2//# sourceMappingURL=/cdn/shop/t/31/assets/cart-mode-guard.js.map?v=37109635108057582811783014857

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.