-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentScript.js
1 lines (1 loc) · 5.42 KB
/
contentScript.js
1
(()=>{"use strict";function e(e){e.parentElement.querySelector(".create-snippet-button")||function(e){const t=e.parentElement.querySelector("div.pointer-events-none.sticky");t?(t.style.pointerEvents="auto",function(e,t){const n=function(e){const t=document.createElement("button"),n='\n <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="currentColor" viewBox="0 0 256 256" class="text-text-500 mr-px -translate-y-[0.5px]">\n <path d="M128 24C65.8 24 16 73.8 16 136s49.8 112 112 112 112-49.8 112-112S190.2 24 128 24zm40 128h-32v32a8 8 0 01-16 0v-32H88a8 8 0 010-16h32V104a8 8 0 0116 0v32h32a8 8 0 010 16z"></path>\n </svg>\n ';return t.className="create-snippet-button flex flex-row items-center gap-1 rounded-md p-1 py-0.5 text-xs transition-opacity delay-100 hover:bg-bg-200 opacity-60 hover:opacity-100",t.innerHTML=`\n ${n}\n <span class="text-text-200 pr-0.5">Create Snippet</span>\n `,t.style.pointerEvents="auto",t.addEventListener("click",(()=>{t.textContent="Creating...";const o=e.parentElement.querySelector(".text-text-300.absolute.pl-3.text-xs"),r=o?o.textContent:"Language not found",s=e.querySelector("code"),a=s?s.textContent:"Code content not found",i=e.closest("div.flex.min-h-screen.w-full").querySelector(".font-tiempos.truncate.font-normal.tracking-tight"),c=i?i.textContent:"Title not found";chrome.runtime.sendMessage({action:"createSnippet",data:{title:c,code:a,language:r}},(e=>{e.success?(t.textContent="Snippet Created!",setTimeout((()=>{t.innerHTML=`\n ${n}\n <span class="text-text-200 pr-0.5">Create Snippet</span>\n `}),2e3),chrome.runtime.sendMessage({action:"refreshSnippets",data:e.data})):"No access token found"===e.error?(t.textContent="Login to create snippet",t.style.pointerEvents="none",t.classList.add("opacity-50"),setTimeout((()=>{t.innerHTML=`\n ${n}\n <span class="text-text-200 pr-0.5">Create Snippet</span>\n `,t.style.pointerEvents="auto",t.classList.remove("opacity-50")}),3e3)):(console.error(e.error),t.textContent="Error! Please try again.",setTimeout((()=>{t.innerHTML=`\n ${n}\n <span class="text-text-200 pr-0.5">Create Snippet</span>\n `}),2e3))}))})),t}(t);e.appendChild(n)}(t,e)):console.log("Copy button container not found.")}(e)}function t(){document.querySelectorAll("pre").forEach((t=>{e(t)}))}const n='\n <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="currentColor" viewBox="0 0 256 256" class="text-text-500 mr-px -translate-y-[0.5px]">\n <path d="M128 24C65.8 24 16 73.8 16 136s49.8 112 112 112 112-49.8 112-112S190.2 24 128 24zm40 128h-32v32a8 8 0 01-16 0v-32H88a8 8 0 010-16h32V104a8 8 0 0116 0v32h32a8 8 0 010 16z"></path>\n </svg>',o='<span class="text-text-200 pr-0.5">Create Snippet</span>';function r(e){e&&e.appendChild(function(){const e=document.createElement("button");return e.className="create-snippet-button flex flex-row items-center gap-1 rounded-md p-1 py-0.5 text-xs transition-opacity delay-100 hover:bg-bg-200 opacity-60 hover:opacity-100",e.innerHTML=`${n}${o}`,e.style.pointerEvents="auto",e.addEventListener("click",(()=>{e.textContent="Creating...",chrome.runtime.sendMessage({action:"executeScript"},(t=>{chrome.runtime.lastError?(console.error("Background script not responding:",chrome.runtime.lastError),e.textContent="Error!"):t&&t.success&&console.log("Script executed successfully.")})),e.dataset.clicked="true"})),e}())}window.addEventListener("message",(e=>{if(e.source===window&&e.data.type&&"FROM_PAGE"===e.data.type){const{code:t,language:r}=e.data,s=document.querySelector(".create-snippet-button[data-clicked='true']");if(s){const e="Snippet Title";chrome.runtime.sendMessage({action:"createSnippet",data:{title:e,code:t,language:r}},(e=>{chrome.runtime.lastError?(console.error("Background script not responding:",chrome.runtime.lastError),s.textContent="Error!",s.removeAttribute("data-clicked")):e&&e.success?(s.textContent="Snippet Created!",setTimeout((()=>{s.innerHTML=`${n}${o}`,s.removeAttribute("data-clicked")}),2e3),chrome.runtime.sendMessage({action:"refreshSnippets",data:e.data})):"No access token found"===e.error?(s.textContent="Login to create snippet",s.style.pointerEvents="none",s.classList.add("opacity-50"),setTimeout((()=>{s.innerHTML=`${n}${o}`,s.style.pointerEvents="auto",s.classList.remove("opacity-50"),s.removeAttribute("data-clicked")}),3e3)):(console.error(e.error),s.textContent="Error!",s.removeAttribute("data-clicked"))}))}}})),t(),new MutationObserver((function(n){n.forEach((t=>{t.addedNodes.forEach((t=>{1===t.nodeType&&t.querySelectorAll("pre").forEach((t=>{e(t)}))}))})),t()})).observe(document.body,{childList:!0,subtree:!0}),new MutationObserver((e=>{for(const t of e)"childList"===t.type&&t.addedNodes.forEach((e=>{if(1===e.nodeType){const t=e.querySelector("div.border-border-400.sticky.flex.items-center.gap-1.border-b.px-2.py-2 > div.flex.shrink-0.flex-grow.items-center.justify-end.gap-1");t&&r(t)}}))})).observe(document.body,{childList:!0,subtree:!0}),chrome.runtime.onMessage.addListener(((e,t,n)=>{if((!t.tab||!t.tab.url||t.tab.url.includes("claude.ai"))&&"addCodeToChat"===e.action){const{code:t,fileName:n}=e,o=new Blob([t],{type:"text/plain"}),r=new File([o],n,{type:"text/plain"}),s=new DataTransfer;s.items.add(r);const a=document.querySelector('input[data-testid="file-upload"]');console.log(a),a&&(a.files=s.files,a.dispatchEvent(new Event("change",{bubbles:!0})))}}))})();