(function(){var e=[{name:`ChatGPT`,host:`chatgpt.com`,selectors:{container:`[data-testid^="conversation-turn-"]`,title:`title`,actionContainer:`[data-testid="conversation-turn-user"] .flex.gap-3, [data-testid="conversation-turn-assistant"] .flex.gap-3, .mt-1.flex.gap-3`,content:`.markdown, .flex-col.gap-1`,files:`[data-testid="file-attachment-name"]`},rules:{userMarkers:{attributes:{"data-message-author-role":`user`}},assistantMarkers:{attributes:{"data-message-author-role":`assistant`}}}},{name:`DeepSeek`,host:`chat.deepseek.com`,selectors:{container:`.ds-message`,title:`title`,actionContainer:`.ds-chat-bubble__actions, .ds-message__actions`,content:`:scope > .ds-markdown`,files:`.ds-file-uploader__file-name`},rules:{userMarkers:{selectors:[`.ds-message:not(:has(.ds-markdown))`]},assistantMarkers:{selectors:[`.ds-message:has(.ds-markdown)`]}}},{name:`Claude`,host:`claude.ai`,selectors:{container:`[data-testid="user-message"], [data-is-streaming]`,title:`title`,actionContainer:`[aria-label="Message actions"] .flex.items-stretch.justify-between`,content:`.standard-markdown, p.whitespace-pre-wrap`,files:`[data-testid="file-attachment"]`},rules:{userMarkers:{selectors:[`[data-testid="user-message"]`]},assistantMarkers:{selectors:[`[data-is-streaming]`,`.font-claude-response`]}}},{name:`Gemini`,host:`gemini.google.com`,selectors:{container:`.query-content, model-response`,title:`title`,actionContainer:`.query-content > .action-button-container:first-child, .actions-container-v2`,content:`.query-text, .markdown`},rules:{userMarkers:{selectors:[`.query-content`,`[id^="user-query-content-"]`]},assistantMarkers:{selectors:[`model-response`,`.markdown-main-panel`]}}}];function t(t){return e.find(e=>t.includes(e.host))}function n(e,t){let{userMarkers:n}=t.rules;if(n.attributes){for(let[t,r]of Object.entries(n.attributes))if(e.getAttribute(t)===r)return!0}if(n.selectors)for(let t of n.selectors)try{if(e.matches(t)||e.querySelector(t))return!0}catch{}return!!n.textPatterns?.some(t=>e.textContent?.includes(t))}function r(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}function i(e,t){return Array(t+1).join(e)}function a(e){return e.replace(/^\n*/,``)}function o(e){for(var t=e.length;t>0&&e[t-1]===`
`;)t--;return e.substring(0,t)}function s(e){return o(a(e))}var c=`ADDRESS.ARTICLE.ASIDE.AUDIO.BLOCKQUOTE.BODY.CANVAS.CENTER.DD.DIR.DIV.DL.DT.FIELDSET.FIGCAPTION.FIGURE.FOOTER.FORM.FRAMESET.H1.H2.H3.H4.H5.H6.HEADER.HGROUP.HR.HTML.ISINDEX.LI.MAIN.MENU.NAV.NOFRAMES.NOSCRIPT.OL.OUTPUT.P.PRE.SECTION.TABLE.TBODY.TD.TFOOT.TH.THEAD.TR.UL`.split(`.`);function l(e){return h(e,c)}var u=[`AREA`,`BASE`,`BR`,`COL`,`COMMAND`,`EMBED`,`HR`,`IMG`,`INPUT`,`KEYGEN`,`LINK`,`META`,`PARAM`,`SOURCE`,`TRACK`,`WBR`];function d(e){return h(e,u)}function f(e){return g(e,u)}var p=[`A`,`TABLE`,`THEAD`,`TBODY`,`TFOOT`,`TH`,`TD`,`IFRAME`,`SCRIPT`,`AUDIO`,`VIDEO`];function m(e){return h(e,p)}function ee(e){return g(e,p)}function h(e,t){return t.indexOf(e.nodeName)>=0}function g(e,t){return e.getElementsByTagName&&t.some(function(t){return e.getElementsByTagName(t).length})}var _=[[/\\/g,`\\\\`],[/\*/g,`\\*`],[/^-/g,`\\-`],[/^\+ /g,`\\+ `],[/^(=+)/g,`\\$1`],[/^(#{1,6}) /g,`\\$1 `],[/`/g,"\\`"],[/^~~~/g,`\\~~~`],[/\[/g,`\\[`],[/\]/g,`\\]`],[/^>/g,`\\>`],[/_/g,`\\_`],[/^(\d+)\. /g,`$1\\. `]];function v(e){return _.reduce(function(e,t){return e.replace(t[0],t[1])},e)}var y={};y.paragraph={filter:`p`,replacement:function(e){return`

`+e+`

`}},y.lineBreak={filter:`br`,replacement:function(e,t,n){return n.br+`
`}},y.heading={filter:[`h1`,`h2`,`h3`,`h4`,`h5`,`h6`],replacement:function(e,t,n){var r=Number(t.nodeName.charAt(1));if(n.headingStyle===`setext`&&r<3){var a=i(r===1?`=`:`-`,e.length);return`

`+e+`
`+a+`

`}else return`

`+i(`#`,r)+` `+e+`

`}},y.blockquote={filter:`blockquote`,replacement:function(e){return e=s(e).replace(/^/gm,`> `),`

`+e+`

`}},y.list={filter:[`ul`,`ol`],replacement:function(e,t){var n=t.parentNode;return n.nodeName===`LI`&&n.lastElementChild===t?`
`+e:`

`+e+`

`}},y.listItem={filter:`li`,replacement:function(e,t,n){var r=n.bulletListMarker+`   `,i=t.parentNode;if(i.nodeName===`OL`){var a=i.getAttribute(`start`),o=Array.prototype.indexOf.call(i.children,t);r=(a?Number(a)+o:o+1)+`.  `}var c=/\n$/.test(e);return e=s(e)+(c?`
`:``),e=e.replace(/\n/gm,`
`+` `.repeat(r.length)),r+e+(t.nextSibling?`
`:``)}},y.indentedCodeBlock={filter:function(e,t){return t.codeBlockStyle===`indented`&&e.nodeName===`PRE`&&e.firstChild&&e.firstChild.nodeName===`CODE`},replacement:function(e,t,n){return`

    `+t.firstChild.textContent.replace(/\n/g,`
    `)+`

`}},y.fencedCodeBlock={filter:function(e,t){return t.codeBlockStyle===`fenced`&&e.nodeName===`PRE`&&e.firstChild&&e.firstChild.nodeName===`CODE`},replacement:function(e,t,n){for(var r=((t.firstChild.getAttribute(`class`)||``).match(/language-(\S+)/)||[null,``])[1],a=t.firstChild.textContent,o=n.fence.charAt(0),s=3,c=RegExp(`^`+o+`{3,}`,`gm`),l;l=c.exec(a);)l[0].length>=s&&(s=l[0].length+1);var u=i(o,s);return`

`+u+r+`
`+a.replace(/\n$/,``)+`
`+u+`

`}},y.horizontalRule={filter:`hr`,replacement:function(e,t,n){return`

`+n.hr+`

`}},y.inlineLink={filter:function(e,t){return t.linkStyle===`inlined`&&e.nodeName===`A`&&e.getAttribute(`href`)},replacement:function(e,t){var n=x(t.getAttribute(`href`)),r=S(b(t.getAttribute(`title`))),i=r?` "`+r+`"`:``;return`[`+e+`](`+n+i+`)`}},y.referenceLink={filter:function(e,t){return t.linkStyle===`referenced`&&e.nodeName===`A`&&e.getAttribute(`href`)},replacement:function(e,t,n){var r=x(t.getAttribute(`href`)),i=b(t.getAttribute(`title`));i&&=` "`+S(i)+`"`;var a,o;switch(n.linkReferenceStyle){case`collapsed`:a=`[`+e+`][]`,o=`[`+e+`]: `+r+i;break;case`shortcut`:a=`[`+e+`]`,o=`[`+e+`]: `+r+i;break;default:var s=this.references.length+1;a=`[`+e+`][`+s+`]`,o=`[`+s+`]: `+r+i}return this.references.push(o),a},references:[],append:function(e){var t=``;return this.references.length&&(t=`

`+this.references.join(`
`)+`

`,this.references=[]),t}},y.emphasis={filter:[`em`,`i`],replacement:function(e,t,n){return e.trim()?n.emDelimiter+e+n.emDelimiter:``}},y.strong={filter:[`strong`,`b`],replacement:function(e,t,n){return e.trim()?n.strongDelimiter+e+n.strongDelimiter:``}},y.code={filter:function(e){var t=e.previousSibling||e.nextSibling,n=e.parentNode.nodeName===`PRE`&&!t;return e.nodeName===`CODE`&&!n},replacement:function(e){if(!e)return``;e=e.replace(/\r?\n|\r/g,` `);for(var t=/^`|^ .*?[^ ].* $|`$/.test(e)?` `:``,n="`",r=e.match(/`+/gm)||[];r.indexOf(n)!==-1;)n+="`";return n+t+e+t+n}},y.image={filter:`img`,replacement:function(e,t){var n=v(b(t.getAttribute(`alt`))),r=x(t.getAttribute(`src`)||``),i=b(t.getAttribute(`title`)),a=i?` "`+S(i)+`"`:``;return r?`![`+n+`](`+r+a+`)`:``}};function b(e){return e?e.replace(/(\n+\s*)+/g,`
`):``}function x(e){var t=e.replace(/([<>()])/g,`\\$1`);return t.indexOf(` `)>=0?`<`+t+`>`:t}function S(e){return e.replace(/"/g,`\\"`)}function C(e){for(var t in this.options=e,this._keep=[],this._remove=[],this.blankRule={replacement:e.blankReplacement},this.keepReplacement=e.keepReplacement,this.defaultRule={replacement:e.defaultReplacement},this.array=[],e.rules)this.array.push(e.rules[t])}C.prototype={add:function(e,t){this.array.unshift(t)},keep:function(e){this._keep.unshift({filter:e,replacement:this.keepReplacement})},remove:function(e){this._remove.unshift({filter:e,replacement:function(){return``}})},forNode:function(e){if(e.isBlank)return this.blankRule;var t;return(t=w(this.array,e,this.options))||(t=w(this._keep,e,this.options))||(t=w(this._remove,e,this.options))?t:this.defaultRule},forEach:function(e){for(var t=0;t<this.array.length;t++)e(this.array[t],t)}};function w(e,t,n){for(var r=0;r<e.length;r++){var i=e[r];if(te(i,t,n))return i}}function te(e,t,n){var r=e.filter;if(typeof r==`string`){if(r===t.nodeName.toLowerCase())return!0}else if(Array.isArray(r)){if(r.indexOf(t.nodeName.toLowerCase())>-1)return!0}else if(typeof r==`function`){if(r.call(e,t,n))return!0}else throw TypeError("`filter` needs to be a string, array, or function")}function T(e){var t=e.element,n=e.isBlock,r=e.isVoid,i=e.isPre||function(e){return e.nodeName===`PRE`};if(!(!t.firstChild||i(t))){for(var a=null,o=!1,s=null,c=D(s,t,i);c!==t;){if(c.nodeType===3||c.nodeType===4){var l=c.data.replace(/[ \r\n\t]+/g,` `);if((!a||/ $/.test(a.data))&&!o&&l[0]===` `&&(l=l.substr(1)),!l){c=E(c);continue}c.data=l,a=c}else if(c.nodeType===1)n(c)||c.nodeName===`BR`?(a&&(a.data=a.data.replace(/ $/,``)),a=null,o=!1):r(c)||i(c)?(a=null,o=!0):a&&(o=!1);else{c=E(c);continue}var u=D(s,c,i);s=c,c=u}a&&(a.data=a.data.replace(/ $/,``),a.data||E(a))}}function E(e){var t=e.nextSibling||e.parentNode;return e.parentNode.removeChild(e),t}function D(e,t,n){return e&&e.parentNode===t||n(t)?t.nextSibling||t.parentNode:t.firstChild||t.nextSibling||t.parentNode}var O=typeof window<`u`?window:{};function k(){var e=O.DOMParser,t=!1;try{new e().parseFromString(``,`text/html`)&&(t=!0)}catch{}return t}function ne(){var e=function(){};return re()?e.prototype.parseFromString=function(e){var t=new window.ActiveXObject(`htmlfile`);return t.designMode=`on`,t.open(),t.write(e),t.close(),t}:e.prototype.parseFromString=function(e){var t=document.implementation.createHTMLDocument(``);return t.open(),t.write(e),t.close(),t},e}function re(){var e=!1;try{document.implementation.createHTMLDocument(``).open()}catch{O.ActiveXObject&&(e=!0)}return e}var ie=k()?O.DOMParser:ne();function ae(e,t){var n=typeof e==`string`?j().parseFromString(`<x-turndown id="turndown-root">`+e+`</x-turndown>`,`text/html`).getElementById(`turndown-root`):e.cloneNode(!0);return T({element:n,isBlock:l,isVoid:d,isPre:t.preformattedCode?M:null}),n}var A;function j(){return A||=new ie,A}function M(e){return e.nodeName===`PRE`||e.nodeName===`CODE`}function N(e,t){return e.isBlock=l(e),e.isCode=e.nodeName===`CODE`||e.parentNode.isCode,e.isBlank=P(e),e.flankingWhitespace=F(e,t),e}function P(e){return!d(e)&&!m(e)&&/^\s*$/i.test(e.textContent)&&!f(e)&&!ee(e)}function F(e,t){if(e.isBlock||t.preformattedCode&&e.isCode)return{leading:``,trailing:``};var n=I(e.textContent);return n.leadingAscii&&L(`left`,e,t)&&(n.leading=n.leadingNonAscii),n.trailingAscii&&L(`right`,e,t)&&(n.trailing=n.trailingNonAscii),{leading:n.leading,trailing:n.trailing}}function I(e){var t=e.match(/^(([ \t\r\n]*)(\s*))(?:(?=\S)[\s\S]*\S)?((\s*?)([ \t\r\n]*))$/);return{leading:t[1],leadingAscii:t[2],leadingNonAscii:t[3],trailing:t[4],trailingNonAscii:t[5],trailingAscii:t[6]}}function L(e,t,n){var r,i,a;return e===`left`?(r=t.previousSibling,i=/ $/):(r=t.nextSibling,i=/^ /),r&&(r.nodeType===3?a=i.test(r.nodeValue):n.preformattedCode&&r.nodeName===`CODE`?a=!1:r.nodeType===1&&!l(r)&&(a=i.test(r.textContent))),a}var R=Array.prototype.reduce;function z(e){if(!(this instanceof z))return new z(e);var t={rules:y,headingStyle:`setext`,hr:`* * *`,bulletListMarker:`*`,codeBlockStyle:`indented`,fence:"```",emDelimiter:`_`,strongDelimiter:`**`,linkStyle:`inlined`,linkReferenceStyle:`full`,br:`  `,preformattedCode:!1,blankReplacement:function(e,t){return t.isBlock?`

`:``},keepReplacement:function(e,t){return t.isBlock?`

`+t.outerHTML+`

`:t.outerHTML},defaultReplacement:function(e,t){return t.isBlock?`

`+e+`

`:e}};this.options=r({},t,e),this.rules=new C(this.options)}z.prototype={turndown:function(e){if(!W(e))throw TypeError(e+` is not a string, or an element/document/fragment node.`);if(e===``)return``;var t=B.call(this,new ae(e,this.options));return V.call(this,t)},use:function(e){if(Array.isArray(e))for(var t=0;t<e.length;t++)this.use(e[t]);else if(typeof e==`function`)e(this);else throw TypeError(`plugin must be a Function or an Array of Functions`);return this},addRule:function(e,t){return this.rules.add(e,t),this},keep:function(e){return this.rules.keep(e),this},remove:function(e){return this.rules.remove(e),this},escape:function(e){return v(e)}};function B(e){var t=this;return R.call(e.childNodes,function(e,n){n=new N(n,t.options);var r=``;return n.nodeType===3?r=n.isCode?n.nodeValue:t.escape(n.nodeValue):n.nodeType===1&&(r=H.call(t,n)),U(e,r)},``)}function V(e){var t=this;return this.rules.forEach(function(n){typeof n.append==`function`&&(e=U(e,n.append(t.options)))}),e.replace(/^[\t\r\n]+/,``).replace(/[\t\r\n\s]+$/,``)}function H(e){var t=this.rules.forNode(e),n=B.call(this,e),r=e.flankingWhitespace;return(r.leading||r.trailing)&&(n=n.trim()),r.leading+t.replacement(n,e,this.options)+r.trailing}function U(e,t){var n=o(e),r=a(t),i=Math.max(e.length-n.length,t.length-r.length);return n+`

`.substring(0,i)+r}function W(e){return e!=null&&(typeof e==`string`||e.nodeType&&(e.nodeType===1||e.nodeType===9||e.nodeType===11))}var G=new z({headingStyle:`atx`,hr:`---`,codeBlockStyle:`fenced`});G.addRule(`pre`,{filter:[`pre`],replacement:(e,t)=>{let n=t.querySelector(`code`);return`\n\`\`\`${n?.className.match(/language-(\w+)/)?.[1]??``}\n${n?.textContent??e}\n\`\`\`\n`}});var K=`.cdk-visually-hidden, .screen-reader-only, .sr-only, [aria-hidden="true"]`,q=/^(Copy|Share|Regenerate|Copy\s+Share\s+Regenerate)\s*$/gim;function J(e,t){let n=t.selectors.content?.split(`,`).map(e=>e.trim())??[],r=null;for(let t of n)try{if(r=e.querySelector(t),r)break}catch{}r||=e;let i=r.cloneNode(!0);i.querySelectorAll(K).forEach(e=>e.remove());let a=G.turndown(i.innerHTML);return a=a.replace(q,``).trim(),a}var Y={zh:{user:`用户说`,assistant:`助手说`,thinking:`思维过程`},en:{user:`User Said`,assistant:`Assistant Said`,thinking:`Thinking Process`},ar:{user:`قال المستخدم`,assistant:`قال المساعد`,thinking:`عملية التفكير`},fr:{user:`Utilisateur a dit`,assistant:`Assistant a dit`,thinking:`Processus de réflexion`},de:{user:`Benutzer sagte`,assistant:`Assistent sagte`,thinking:`Denkprozess`},es:{user:`Usuario dijo`,assistant:`Asistente dijo`,thinking:`Proceso de pensamiento`},ko:{user:`사용자 말함`,assistant:`어시스턴트 말함`,thinking:`생각 과정`}};async function X(){return Y[(await chrome.runtime.sendMessage({type:`GET_EFFECTIVE_LANGUAGE`}))?.lang||`en`]??Y.en}var Z=`sidermem-save-btn`,oe=`<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 5a3 3 0 1 0-5.997.125 4 4 0 0 0-2.526 5.77 4 4 0 0 0 .52 8.105 4 4 0 0 0 5.327 2.7M12 5V2m12 7c0-3-2-5-5-5-1.38 0-2.63.56-3.54 1.46M12 5V2m12 7c0 3 2 5 5 5 1.38 0 2.63-.56 3.54-1.46"/></svg>`;function Q(e){document.querySelectorAll(e.selectors.actionContainer).forEach(t=>{if(t.querySelector(`.${Z}`))return;let r=document.createElement(`button`);r.className=`${Z} p-1.5 rounded-lg hover:bg-indigo-50 dark:hover:bg-indigo-900/30 transition-all ml-2 cursor-pointer border-none bg-transparent`,r.title=`Save to SiderMem`,r.innerHTML=oe,r.addEventListener(`click`,async t=>{t.preventDefault(),t.stopPropagation();let i=r.closest(e.selectors.container);if(!i)return;let a=J(i,e);if(!a)return;let o=n(i,e),s=await X(),c=o?s.user:s.assistant;chrome.runtime.sendMessage({type:`SAVE_MEMORY`,payload:{title:`Fragment from ${e.name}`,contentOriginal:`### ${c}\n\n${a}`,sourceType:`chatbot`,sourceApp:e.name,tags:[`chatbot`]}},e=>{e?.success&&(r.style.color=`#10b981`)})}),t.appendChild(r)})}async function se(e){let t=document.querySelector(e.selectors.title)?.textContent?.trim()||document.title||`Extracted Conversation`,r=await X(),i=[],a=new Set;document.querySelectorAll(e.selectors.container).forEach(t=>{let o=J(t,e);if(!o||o.length<=1||a.has(o))return;a.add(o);let s=n(t,e),c=o;if(e.rules.thinkingSelector&&!s){let n=t.querySelector(e.rules.thinkingSelector);if(n){let e=n.textContent?.trim()??``,t=o.replace(e,``).trim();c=`[${r.thinking}]\n${e}\n\n${t}`}}i.push({isUser:s,text:c,top:t.getBoundingClientRect().top+window.scrollY})}),i.sort((e,t)=>e.top-t.top);let o=i.map((e,t)=>{let n=e.isUser?r.user:r.assistant,a=t<i.length-1?`

---

`:``;return`### ${n}\n\n${e.text}${a}`}).join(``);await new Promise((n,r)=>{chrome.runtime.sendMessage({type:`SAVE_MEMORY`,payload:{title:t,contentOriginal:o.trim(),sourceType:`chatbot`,sourceApp:e.name,tags:[`chatbot`]}},e=>e?.success?n():r(Error(`Save failed`)))});let s=e.selectors.files?Array.from(new Set(Array.from(document.querySelectorAll(e.selectors.files)).map(e=>e.textContent?.trim()).filter(e=>!!e))):[];return await Promise.allSettled(s.map(t=>new Promise(n=>{chrome.runtime.sendMessage({type:`SAVE_MEMORY`,payload:{title:`File: ${t}`,contentOriginal:`File "${t}" was referenced in a conversation on ${e.name}.`,sourceType:`file`,sourceApp:e.name,tags:[`auto-extracted-file`]}},()=>n())}))),{success:!0,filesCount:s.length}}var $=t(window.location.href);if($){console.log(`[SiderMem] Active on ${$.name}`),setTimeout(()=>Q($),2e3);let e;new MutationObserver(()=>{clearTimeout(e),e=setTimeout(()=>Q($),500)}).observe(document.body,{childList:!0,subtree:!0}),chrome.runtime.onMessage.addListener((e,t,n)=>{if(e.type===`EXTRACT_PAGE_CONTENT`)return se($).then(e=>n({success:!0,data:e})).catch(e=>n({success:!1,error:e.message})),!0})}})()
