diff --git a/extension/turbowarp/src/i18n/source.json b/extension/turbowarp/src/i18n/source.json index 1f15260..d3e851c 100644 --- a/extension/turbowarp/src/i18n/source.json +++ b/extension/turbowarp/src/i18n/source.json @@ -6,7 +6,18 @@ "exist": "随记便签是否存在 [code]", "login": "登录 lightnotes 账号", "close": "关闭验证窗口", - "website": "寻找河流的源头..." + "logout": "退出登录", + "check": "是否登录", + "uget": "获取便签 id [id]", + "utime": "获取便签更新时间 id [id]", + "unew": "新建便签 标题 [title] 内容 [body]", + "uupdate": "更新便签 id [id] 标题 [title] 内容 [body]", + "udelete": "删除便签 id [id]", + "ulist": "获取便签列表 第 [page] 页", + "utotal": "获取便签页数", + "uprev": "第 [page] 页是否有上一页", + "unext": "第 [page] 页是否有下一页", + "website": "Lightnotes 官网" }, "en": { "title": "Memorize Lightnotes", @@ -15,6 +26,17 @@ "exist": "Presence of a casual sticky note [code]", "login": "Log in to your lightnotes account", "close": "Close verification window", - "website": "Finding the source of the river..." + "logout": "Log out", + "check": "Logged in or not", + "uget": "Get note id [id]", + "utime": "Get note update time id [id]", + "unew": "New Note Heading [title] Content [body]", + "uupdate": "Update note id [id] title [title] content [body]", + "udelete": "Delete note id [id]", + "ulist": "Get notes list page [page]", + "utotal": "Get the number of note pages", + "uprev": "Is there a previous page on page [page]", + "unext": "Is there a next page on page [page]", + "website": "Lightnotes' website" } } \ No newline at end of file diff --git a/extension/turbowarp/src/index.ts b/extension/turbowarp/src/index.ts index d98db0e..ee18a57 100644 --- a/extension/turbowarp/src/index.ts +++ b/extension/turbowarp/src/index.ts @@ -2,15 +2,60 @@ import Extension from './include/plugin' import axios from 'axios' let show = false; -let offsetX: number, offsetY: number, isDragging = false; + +type Note = { + id: number; + title: string; + body: string; + updated_at: string; +} + +type PaginationResponse = { + status: boolean; + total: number; + page: number; + prev_page: boolean; + next_page: boolean; + notes: Note[]; +} function setDisplay(state: boolean): boolean { show = state; const el = document.getElementById('notes'); - if (el) el.className = show ? 'fade': ''; + if (el) { + el.className = show ? 'fade': ''; + el.style.zIndex = show ? '9999' : '-1'; + } return show; } +function setToken(raw: string): void { + const token = raw.split('').map((i: string) => { + return String.fromCharCode(i.charCodeAt(0) + 1) + }).join(''); + sessionStorage.setItem('notes', token); + axios.defaults.headers.common['Authorization'] = raw; +} + +function getToken(): string { + const raw = sessionStorage.getItem('notes'); + if (!raw) return ''; + return raw.split('').map((i: string) => { + return String.fromCharCode(i.charCodeAt(0) - 1) + }).join(''); +} + +async function getNotes(page: number): Promise<{data: PaginationResponse, status: boolean, reason: string }> { + try { + const res = await axios.get(`/user/list?page=${page}`); + if (!res.data.status) return { data: {} as PaginationResponse, status: false, reason: "接收失败!请检查您是否登录" }; + return { data: res.data as PaginationResponse, status: true, reason: "" }; + } catch (e) { + console.debug(e); + return { data: {} as PaginationResponse, status: false, reason: "接收失败!请检查您的网络环境" }; + } +} + new Extension({ id: 'notes', name: '随记便签 Lightnotes', @@ -25,11 +70,7 @@ new Extension({ return "标题或内容不能为空" } try { - const res = await axios.post("https://notes.lightxi.com/api/anonymous/send", { title, body }, { - headers: { - 'Content-Type': 'application/json' - }, - }) + const res = await axios.post("/anonymous/send", { title, body }); if (!res.data.status) return "发送失败!请稍后重试"; return res.data.code; } catch (e) { @@ -46,11 +87,7 @@ new Extension({ return "接收失败!取签码不能为空" } try { - const res = await axios.get(`https://notes.lightxi.com/api/anonymous/get?code=${code}`, { - headers: { - 'Content-Type': 'application/json' - }, - }); + const res = await axios.get(`/anonymous/get?code=${code}`); if (!res.data.status) return "接收失败!请检查您的接签码是否正确,匿名便签是否过期"; return JSON.stringify({ title: res.data.title, @@ -68,11 +105,7 @@ new Extension({ bind: async function ({ code }): Promise { if (!code) return false try { - const res = await axios.get(`https://notes.lightxi.com/api/anonymous/get?code=${code}`, { - headers: { - 'Content-Type': 'application/json' - } - }); + const res = await axios.get(`/anonymous/get?code=${code}`); return res.data.status; } catch (e) { console.debug(e); @@ -93,10 +126,157 @@ new Extension({ bind: function (): void { setDisplay(false); } + }, { + opcode: "logout", + blockType: "command", + text: "退出登录", + bind: function (): void { + sessionStorage.removeItem('notes'); + delete axios.defaults.headers.common['Authorization']; + } + }, { + opcode: "check", + blockType: "Boolean", + text: "是否登录", + bind: async function (): Promise { + if (!getToken()) return false; + try { + const res = await axios.post("/user/state"); + return res.data.status; + } catch (e) { + console.debug(e); + return false; + } + } + }, { + opcode: "uget", + blockType: "reporter", + text: "获取便签 id [id:number]", + bind: async function ({ id }): Promise> { + try { + const res = await axios.get(`/user/get?id=${id}`); + if (!res.data.status) return "接收失败!请检查您的便签 id 是否正确"; + const note = res.data.note as Note; + return JSON.stringify({ + title: note.title, + body: note.body, + }); + } catch (e) { + console.debug(e); + return "接收失败!请检查您的网络环境 以及是否登录!"; + } + } + }, { + opcode: "utime", + blockType: "reporter", + text: "获取便签更新时间 id [id:number]", + bind: async function ({ id }): Promise { + try { + const res = await axios.get(`/user/get?id=${id}`); + if (!res.data.status) return "接收失败!请检查您的便签 id 是否正确"; + return (res.data.note as Note).updated_at; + } catch (e) { + console.debug(e); + return "接收失败!请检查您的网络环境 以及是否登录!"; + } + } + }, { + opcode: "unew", + blockType: "reporter", + text: "新建便签 标题 [title:string] 内容 [body:string]", + default: { title: "新便签", body: "写点什么" }, + bind: async function ({ title, body }): Promise { + if (!title || !body) { + return "标题或内容不能为空" + } + try { + const res = await axios.post("/user/save", { title, body }); + if (!res.data.status) return "发送失败!请稍后重试"; + return res.data.id; + } catch (e) { + console.debug(e); + return "发送失败!请检查您的网络环境 以及是否登录!"; + } + } + }, { + opcode: "uupdate", + blockType: "reporter", + text: "更新便签 id [id:number] 标题 [title:string] 内容 [body:string]", + default: { title: "新便签", body: "写点什么" }, + bind: async function ({ id, title, body }): Promise { + if (!id || !title || !body) { + return "标题或内容不能为空" + } + try { + const res = await axios.post(`/user/update?id=${id}`, { id, title, body }); + if (!res.data.status) return "发送失败!请稍后重试"; + return res.data.status; + } catch (e) { + console.debug(e); + return "发送失败!请检查您的网络环境 以及是否登录!"; + } + } + }, { + opcode: "udelete", + blockType: "reporter", + text: "删除便签 id [id:number]", + bind: async function ({ id }): Promise { + if (!id) { + return "id 不能为空" + } + try { + const res = await axios.post(`/user/delete?id=${id}`); + if (!res.data.status) return "发送失败!请稍后重试"; + return res.data.status; + } catch (e) { + console.debug(e); + return "发送失败!请检查您的网络环境 以及是否登录!"; + } + } + }, { + opcode: "ulist", + blockType: "reporter", + text: "获取便签列表 第 [page:number] 页", + default: { page: "1" }, + bind: async function ({ page }): Promise> { + const resp = await getNotes(page); + if (!resp.status) return resp.reason; + return JSON.stringify(resp.data.notes); + } + }, { + opcode: "utotal", + blockType: "reporter", + text: "获取便签页数", + disableMonitor: true, + bind: async function (): Promise { + const resp = await getNotes(1); + if (!resp.status) return resp.reason; + return resp.data.total; + } + }, { + opcode: "uprev", + blockType: "reporter", + text: "第 [page:number] 页是否有上一页", + default: { page: "1" }, + bind: async function ({ page }): Promise { + const resp = await getNotes(page); + if (!resp.status) return resp.reason; + return resp.data.prev_page; + } + }, { + opcode: "unext", + blockType: "reporter", + text: "第 [page:number] 页是否有下一页", + default: { page: "1" }, + bind: async function ({ page }): Promise { + const resp = await getNotes(page); + if (!resp.status) return resp.reason; + return resp.data.next_page; + } }, { opcode: "website", blockType: "command", - text: "寻找河流的源头...", + text: "Lightnotes 官网", bind: () => window.open("https://notes.lightxi.com"), }], i18n: { @@ -125,7 +305,7 @@ new Extension({ const container = document.createElement('div'); container.id = 'notes'; container.style.position = 'absolute'; - container.style.zIndex = '1024'; + container.style.zIndex = '-1'; container.style.overflow = 'hidden'; container.style.top = '50%'; container.style.left = '50%'; @@ -152,7 +332,7 @@ new Extension({ container.appendChild(close); const frame = document.createElement('iframe'); - frame.src = 'http://localhost:5173/'; + frame.src = 'https://notes.lightxi.com/'; frame.style.width = '100%'; frame.style.height = '100%'; frame.style.border = 'none'; @@ -163,12 +343,18 @@ new Extension({ iframe.postMessage({ type: 'login' }, '*'); }, 1000); window.addEventListener('message', (e) => { - console.log(e.data); if (e.data.type === 'login') { - + if (e.data.status) { + setToken(e.data.token); + setDisplay(false); + } } }, false); } container.appendChild(frame); + + axios.defaults.baseURL = 'https://notes.lightxi.com/api'; + axios.defaults.headers.common['Authorization'] = getToken(); + axios.defaults.headers.common['Content-Type'] = 'application/json'; } }).register()