From 6ac3bd245297140c74321351c167f0379bcdd094 Mon Sep 17 00:00:00 2001 From: oeyoews Date: Sat, 23 Mar 2024 00:59:32 +0800 Subject: [PATCH] feat: update port --- README.md | 2 +- entrypoints/background.ts | 4 +-- entrypoints/popup/Popup.vue | 61 +++++++++++++++++++++++++++++-------- 3 files changed, 51 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index bf6e636..3bf8f36 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ * 完成 welcome page * tour: https://element-plus.org/zh-CN/component/tour.html * tag -* port 改变检测 +* port 通知优化 * extension icon * 统计大小 * darkmode diff --git a/entrypoints/background.ts b/entrypoints/background.ts index 4664b2e..137abe8 100644 --- a/entrypoints/background.ts +++ b/entrypoints/background.ts @@ -8,7 +8,7 @@ export default defineBackground(() => { }); }); - chrome.runtime.onInstalled.addListener(function () { + /* chrome.runtime.onInstalled.addListener(function () { chrome.contextMenus.create({ id: 'tiddlywiki', title: '添加到 TiddlyWiki(WIP)', @@ -26,5 +26,5 @@ export default defineBackground(() => { if (info.menuItemId === 'tiddlywiki') { // 处理右键菜单点击事件的逻辑 } - }); + }); */ }); diff --git a/entrypoints/popup/Popup.vue b/entrypoints/popup/Popup.vue index 98e44c1..61ac1fa 100644 --- a/entrypoints/popup/Popup.vue +++ b/entrypoints/popup/Popup.vue @@ -27,7 +27,13 @@ const link = ref('') const faviconUrl = ref('') const title = ref('') const username = ref('oeyoews') -const port = ref('8000') + +const port = ref('') + +chrome.storage.local.get(['port'], function (result) { + port.value = result.port || '8080' +}); + chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { const tab = tabs[0] @@ -43,10 +49,6 @@ chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { }) -watch(md, async () => { - html.value = (await md2html(md.value)) -}) - const status = ref<{ username: string, tiddlywiki_version: string }>({ @@ -54,18 +56,46 @@ const status = ref<{ tiddlywiki_version: '' }) -fetch(`http://localhost:${port.value}/status`).then((res) => { - return res.json() -}).then((data) => { - status.value = data; - if (!data.tiddlywiki_version) { +function checkStatus() { + + fetch(`http://localhost:${port.value}/status`).then((res) => { + return res.json() + }).then((data) => { + if (!data) { + return + } + status.value = data + + if (!data.tiddlywiki_version) { + ElMessage({ + message: 'TiddlyWiki 未连接', + type: 'error' + }) + } else { + // ElMessage({ + // message: 'TiddlyWiki 连接成功', + // type: 'success' + // }) + } + }).catch((e) => { ElMessage({ - message: 'TiddlyWiki 未连接', + message: "TiddlyWiki 未成功连接" + e, type: 'error' }) - } + }) + +} + +watch(md, async () => { + html.value = (await md2html(md.value)) }) +watch(port, () => { + chrome.storage.local.set({ port: port.value }) + checkStatus() +}) + + const save2TiddlyWiki = async (title: string, text: string, port: string, url: string) => { if (!status.value.tiddlywiki_version) { @@ -95,6 +125,11 @@ const save2TiddlyWiki = async (title: string, text: string, port: string, url: s type: 'success' }) } + }).catch((e) => { + ElMessage({ + message: '保存失败' + e, + type: 'error' + }) }) } @@ -163,7 +198,7 @@ const save2TiddlyWiki = async (title: string, text: string, port: string, url: s
端口
- +