diff --git a/src/_locales/en/main.json b/src/_locales/en/main.json index add05f4..3742671 100644 --- a/src/_locales/en/main.json +++ b/src/_locales/en/main.json @@ -105,5 +105,6 @@ "Always pin the floating window": "Always pin the floating window", "Export": "Export", "Always Create New Conversation Window": "Always Create New Conversation Window", - "Please keep this tab open. You can now use the web mode of ChatGPTBox": "Please keep this tab open. You can now use the web mode of ChatGPTBox" + "Please keep this tab open. You can now use the web mode of ChatGPTBox": "Please keep this tab open. You can now use the web mode of ChatGPTBox", + "Go Back": "Go Back" } diff --git a/src/_locales/zh-hans/main.json b/src/_locales/zh-hans/main.json index 428e839..fe0c8f8 100644 --- a/src/_locales/zh-hans/main.json +++ b/src/_locales/zh-hans/main.json @@ -105,5 +105,6 @@ "Always pin the floating window": "总是固定浮动窗口", "Export": "导出", "Always Create New Conversation Window": "总是创建新的对话窗口", - "Please keep this tab open. You can now use the web mode of ChatGPTBox": "请保持这个页面打开, 现在你可以使用ChatGPTBox的网页版模式" + "Please keep this tab open. You can now use the web mode of ChatGPTBox": "请保持这个页面打开, 现在你可以使用ChatGPTBox的网页版模式", + "Go Back": "返回" } diff --git a/src/_locales/zh-hant/main.json b/src/_locales/zh-hant/main.json index 5c65e3e..fd6b06e 100644 --- a/src/_locales/zh-hant/main.json +++ b/src/_locales/zh-hant/main.json @@ -105,5 +105,6 @@ "Always pin the floating window": "總是固定浮動視窗", "Export": "匯出", "Always Create New Conversation Window": "總是建立新的對話視窗", - "Please keep this tab open. You can now use the web mode of ChatGPTBox": "請保持這個頁面開啟, 現在妳可以使用ChatGPTBox的網頁版模式" + "Please keep this tab open. You can now use the web mode of ChatGPTBox": "請保持這個頁面開啟, 現在妳可以使用ChatGPTBox的網頁版模式", + "Go Back": "返回" } diff --git a/src/background/index.mjs b/src/background/index.mjs index 52a736a..024e32b 100644 --- a/src/background/index.mjs +++ b/src/background/index.mjs @@ -47,7 +47,7 @@ function setPortProxy(port, proxyTabId) { const proxyOnDisconnect = () => { port.proxy = Browser.tabs.connect(proxyTabId) } - const portOnDisconnect = (msg) => { + const portOnDisconnect = () => { port.proxy.onMessage.removeListener(proxyOnMessage) port.onMessage.removeListener(portOnMessage) port.proxy.onDisconnect.removeListener(proxyOnDisconnect) @@ -118,7 +118,7 @@ async function executeApi(session, port, config) { } } -Browser.runtime.onMessage.addListener(async (message) => { +Browser.runtime.onMessage.addListener(async (message, sender) => { switch (message.type) { case 'FEEDBACK': { const token = await getChatGptAccessToken() @@ -130,6 +130,22 @@ Browser.runtime.onMessage.addListener(async (message) => { await deleteConversation(token, message.data.conversationId) break } + case 'NEW_URL': { + const newTab = await Browser.tabs.create({ + url: message.data.url, + pinned: message.data.pinned, + }) + if (message.data.saveAsChatgptConfig) { + await setUserConfig({ + chatgptTabId: newTab.id, + chatgptJumpBackTabId: sender.tab.id, + }) + } + break + } + case 'ACTIVATE_URL': + await Browser.tabs.update(message.data.tabId, { active: true }) + break case 'OPEN_URL': openUrl(message.data.url) break @@ -139,17 +155,11 @@ Browser.runtime.onMessage.addListener(async (message) => { case 'PIN_TAB': { let tabId if (message.data.tabId) tabId = message.data.tabId - else { - const currentTab = (await Browser.tabs.query({ active: true, currentWindow: true }))[0] - if (message.data.saveAsChatgptConfig) { - if (currentTab.url.includes('chat.openai.com')) tabId = currentTab.id - } else { - tabId = currentTab.id - } - } - if (tabId) { - await Browser.tabs.update(tabId, { pinned: true }) - if (message.data.saveAsChatgptConfig) await setUserConfig({ chatgptTabId: tabId }) + else tabId = sender.tab.id + + await Browser.tabs.update(tabId, { pinned: true }) + if (message.data.saveAsChatgptConfig) { + await setUserConfig({ chatgptTabId: tabId }) } break } diff --git a/src/components/MarkdownRender/Hyperlink.jsx b/src/components/MarkdownRender/Hyperlink.jsx new file mode 100644 index 0000000..b758c6e --- /dev/null +++ b/src/components/MarkdownRender/Hyperlink.jsx @@ -0,0 +1,37 @@ +import PropTypes from 'prop-types' +import Browser from 'webextension-polyfill' + +export function Hyperlink({ href, children }) { + const linkProperties = { + target: '_blank', + style: 'color: #8ab4f8; cursor: pointer;', + rel: 'nofollow noopener noreferrer', + } + + return href.includes('chat.openai.com') ? ( + { + Browser.runtime.sendMessage({ + type: 'NEW_URL', + data: { + url: href, + pinned: true, + saveAsChatgptConfig: true, + }, + }) + }} + > + {children} + + ) : ( + + {children} + + ) +} + +Hyperlink.propTypes = { + href: PropTypes.string.isRequired, + children: PropTypes.object.isRequired, +} diff --git a/src/components/MarkdownRender/markdown-without-katex.jsx b/src/components/MarkdownRender/markdown-without-katex.jsx index 50ac088..f415c42 100644 --- a/src/components/MarkdownRender/markdown-without-katex.jsx +++ b/src/components/MarkdownRender/markdown-without-katex.jsx @@ -4,13 +4,9 @@ import rehypeHighlight from 'rehype-highlight' import remarkGfm from 'remark-gfm' import remarkBreaks from 'remark-breaks' import { Pre } from './Pre' +import { Hyperlink } from './Hyperlink' export function MarkdownRender(props) { - const linkProperties = { - target: '_blank', - style: 'color: #8ab4f8;', - rel: 'nofollow noopener noreferrer', - } return (