Commit 03d3365d authored by Vũ Hoàng Anh's avatar Vũ Hoàng Anh

fix: resolve extension context invalidated and connection errors

parent 2bb16eba
......@@ -19,7 +19,7 @@
]
},
"src/content/content-script.ts": {
"file": "assets/content-script.ts-CTdB63jU.js",
"file": "assets/content-script.ts-DmyOab9v.js",
"name": "content-script.ts",
"src": "src/content/content-script.ts",
"isEntry": true
......
(function(){let c="",u="",d="";async function m(){for(let o=0;o<3;o++){try{const n=document.cookie.match(/(?:^|; )memos\.access-token=([^;]+)/);if(n&&n[1])return n[1];for(let r=0;r<localStorage.length;r++){const a=localStorage.key(r);if(a&&(a.includes("access_token")||a.includes("token"))){const s=localStorage.getItem(a);if(s&&typeof s=="string"&&s.length>20&&!s.startsWith("{"))return s}}}catch{}o<2&&await new Promise(n=>setTimeout(n,500))}return null}async function p(){const e=window.location.hostname;if(!(e==="172.16.2.210"||e==="localhost"||e.includes("canifa")||e.includes("cucunote")||e.includes("cucu-note")))return;const o=await m();o&&chrome.runtime.sendMessage({type:"SYNC_AUTH",data:{memosAccessToken:o}})}p();chrome.runtime.onMessage.addListener((e,t,o)=>{if(e.type==="GET_TOKEN")return m().then(n=>{o({token:n})}),!0});document.addEventListener("mouseup",h);document.addEventListener("keydown",async e=>{if((e.key===" "||e.key==="Enter")&&c.length>0){const t=e.target;if(!t||t.tagName!=="INPUT"&&t.tagName!=="TEXTAREA"&&!t.isContentEditable){e.preventDefault(),e.stopPropagation(),await y();return}}});document.addEventListener("keyup",h);function h(){try{setTimeout(()=>{const e=window.getSelection();if(!e||e.rangeCount===0){c="",l();return}const t=e.toString().trim();if(t.length===0){c="",l();return}c=t,u=window.location.href,d=document.title,f()},50)}catch{}}async function y(){if(c)try{i("Đang lưu...","loading"),chrome.runtime.sendMessage({type:"SAVE_NOTE",data:{text:c,url:u,title:d}},e=>{if(chrome.runtime.lastError){i(`Lỗi: ${chrome.runtime.lastError.message||"Extension error"}`,"error");return}if(e?.success)i("Đã lưu vào Canifa Note","success"),window.getSelection()?.removeAllRanges(),c="",l();else{const t=e?.error||"Không thể lưu";i(t,"error")}})}catch(e){i(e?.message||"Lỗi khi lưu","error")}}function f(){l();const e=document.createElement("div");if(e.id="cucu-quick-hint",e.textContent="Space hoặc Enter để lưu nhanh",e.style.cssText=`
(function(){let i="",l="",d="";async function m(){for(let o=0;o<3;o++){try{const n=document.cookie.match(/(?:^|; )memos\.access-token=([^;]+)/);if(n&&n[1])return n[1];for(let a=0;a<localStorage.length;a++){const s=localStorage.key(a);if(s&&(s.includes("access_token")||s.includes("token"))){const c=localStorage.getItem(s);if(c&&typeof c=="string"&&c.length>20&&!c.startsWith("{"))return c}}}catch{}o<2&&await new Promise(n=>setTimeout(n,500))}return null}async function f(){const t=window.location.hostname;if(!(t==="172.16.2.210"||t==="localhost"||t.includes("canifa")||t.includes("cucunote")||t.includes("cucu-note")))return;const o=await m();if(o){if(!chrome.runtime?.id)return;try{chrome.runtime.sendMessage({type:"SYNC_AUTH",data:{memosAccessToken:o}},()=>{chrome.runtime.lastError})}catch{}}}f();chrome.runtime.onMessage.addListener((t,e,o)=>{if(t.type==="GET_TOKEN")return m().then(n=>{o({token:n})}),!0});document.addEventListener("mouseup",h);document.addEventListener("keydown",async t=>{if((t.key===" "||t.key==="Enter")&&i.length>0){const e=t.target;if(!e||e.tagName!=="INPUT"&&e.tagName!=="TEXTAREA"&&!e.isContentEditable){t.preventDefault(),t.stopPropagation(),await p();return}}});document.addEventListener("keyup",h);function h(){try{setTimeout(()=>{const t=window.getSelection();if(!t||t.rangeCount===0){i="",u();return}const e=t.toString().trim();if(e.length===0){i="",u();return}i=e,l=window.location.href,d=document.title,y()},50)}catch{}}async function p(){if(i)try{if(r("Đang lưu...","loading"),!chrome.runtime?.id){r("Extension vừa cập nhật. Vui lòng tải lại trang (F5) để lưu note!","error");return}chrome.runtime.sendMessage({type:"SAVE_NOTE",data:{text:i,url:l,title:d}},t=>{if(chrome.runtime.lastError){r("Đang kết nối lại Extension, vui lòng thử lại...","error");return}if(t?.success)r("Đã lưu vào Canifa Note","success"),window.getSelection()?.removeAllRanges(),i="",u();else{const e=t?.error||"Không thể lưu";r(e,"error")}})}catch(t){r(t?.message||"Lỗi khi lưu","error")}}function y(){u();const t=document.createElement("div");if(t.id="cucu-quick-hint",t.textContent="Space hoặc Enter để lưu nhanh",t.style.cssText=`
position: fixed;
bottom: 20px;
right: 20px;
......@@ -14,17 +14,17 @@
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
animation: cucuSlideUp 0.2s ease-out;
pointer-events: none;
`,!document.getElementById("cucu-hint-style")){const t=document.createElement("style");t.id="cucu-hint-style",t.textContent=`
`,!document.getElementById("cucu-hint-style")){const e=document.createElement("style");e.id="cucu-hint-style",e.textContent=`
@keyframes cucuSlideUp {
from { opacity: 0; transform: translateY(12px); }
to { opacity: 1; transform: translateY(0); }
}
`,document.head.appendChild(t)}try{document.body.appendChild(e)}catch{document.documentElement.appendChild(e)}setTimeout(()=>l(),3e3)}function l(){const e=document.getElementById("cucu-quick-hint");if(e)try{e.remove()}catch{}}function i(e,t="success"){const o=document.getElementById("cucu-toast");if(o)try{o.remove()}catch{}const n=document.createElement("div");n.id="cucu-toast";const r={success:"✓",error:"✕",loading:"⟳"};n.textContent=`${r[t]} ${e}`;const a={success:"hsl(142 71% 45%)",error:"hsl(0 72% 51%)",loading:"hsl(240 5% 64.9%)"};if(n.style.cssText=`
`,document.head.appendChild(e)}try{document.body.appendChild(t)}catch{document.documentElement.appendChild(t)}setTimeout(()=>u(),3e3)}function u(){const t=document.getElementById("cucu-quick-hint");if(t)try{t.remove()}catch{}}function r(t,e="success"){const o=document.getElementById("cucu-toast");if(o)try{o.remove()}catch{}const n=document.createElement("div");n.id="cucu-toast";const a={success:"✓",error:"✕",loading:"⟳"};n.textContent=`${a[e]} ${t}`;const s={success:"hsl(142 71% 45%)",error:"hsl(0 72% 51%)",loading:"hsl(240 5% 64.9%)"};if(n.style.cssText=`
position: fixed;
top: 20px;
right: 20px;
background: hsl(240 10% 3.9%);
color: ${a[t]};
color: ${s[e]};
padding: 12px 18px;
border-radius: 8px;
font-size: 13px;
......@@ -36,10 +36,10 @@
animation: cucuToastSlide 0.25s ease-out;
max-width: 320px;
word-wrap: break-word;
`,!document.getElementById("cucu-toast-style")){const s=document.createElement("style");s.id="cucu-toast-style",s.textContent=`
`,!document.getElementById("cucu-toast-style")){const c=document.createElement("style");c.id="cucu-toast-style",c.textContent=`
@keyframes cucuToastSlide {
from { opacity: 0; transform: translateX(100%); }
to { opacity: 1; transform: translateX(0); }
}
`;try{document.head.appendChild(s)}catch{}}try{document.body.appendChild(n)}catch{try{document.documentElement.appendChild(n)}catch{return}}t!=="loading"&&setTimeout(()=>{n.parentNode&&(n.style.animation="cucuToastSlide 0.2s ease-out reverse",setTimeout(()=>{try{n.remove()}catch{}},200))},3e3)}
`;try{document.head.appendChild(c)}catch{}}try{document.body.appendChild(n)}catch{try{document.documentElement.appendChild(n)}catch{return}}e!=="loading"&&setTimeout(()=>{n.parentNode&&(n.style.animation="cucuToastSlide 0.2s ease-out reverse",setTimeout(()=>{try{n.remove()}catch{}},200))},3e3)}
})()
......@@ -20,7 +20,7 @@
"content_scripts": [
{
"js": [
"assets/content-script.ts-CTdB63jU.js"
"assets/content-script.ts-DmyOab9v.js"
],
"matches": [
"<all_urls>"
......@@ -48,7 +48,7 @@
"<all_urls>"
],
"resources": [
"assets/content-script.ts-CTdB63jU.js"
"assets/content-script.ts-DmyOab9v.js"
],
"use_dynamic_url": false
}
......
......@@ -55,10 +55,18 @@ async function autoSyncToken() {
const token = await tryExtractToken();
if (token) {
chrome.runtime.sendMessage({
type: 'SYNC_AUTH',
data: { memosAccessToken: token },
});
if (!chrome.runtime?.id) return; // Prevent "Extension context invalidated"
try {
chrome.runtime.sendMessage({
type: 'SYNC_AUTH',
data: { memosAccessToken: token },
}, () => {
// Catch runtime.lastError to prevent "Unchecked runtime.lastError"
if (chrome.runtime.lastError) { /* ignore */ }
});
} catch {
// Ignore
}
}
}
......@@ -123,6 +131,11 @@ async function handleAutoSave() {
try {
showToast('Đang lưu...', 'loading');
if (!chrome.runtime?.id) {
showToast('Extension vừa cập nhật. Vui lòng tải lại trang (F5) để lưu note!', 'error');
return;
}
chrome.runtime.sendMessage({
type: 'SAVE_NOTE',
data: {
......@@ -132,7 +145,8 @@ async function handleAutoSave() {
},
}, (response) => {
if (chrome.runtime.lastError) {
showToast(`Lỗi: ${chrome.runtime.lastError.message || 'Extension error'}`, 'error');
// Service worker might be waking up or disconnected
showToast('Đang kết nối lại Extension, vui lòng thử lại...', 'error');
return;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment