From bc7637bcfc1284b624211b7ce24e70f61e44e6ee Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Fri, 26 Jan 2024 16:57:15 +0000 Subject: [PATCH] Fix FastGPT disappearing on settings toggle (#65) Also change the context summarization to open in the popup as the extension, instead of a new tab. This will be released on 0.5.1. Fixes #45 Fixes #64 --- chrome/manifest.json | 2 +- firefox/manifest.json | 2 +- shared/src/background.js | 17 +++++++++----- shared/src/popup.js | 2 ++ shared/src/summarize_result.js | 43 +++++++++++++++++++--------------- 5 files changed, 39 insertions(+), 27 deletions(-) diff --git a/chrome/manifest.json b/chrome/manifest.json index 3c34edf..fd553f7 100644 --- a/chrome/manifest.json +++ b/chrome/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "Kagi Search for Chrome", - "version": "0.5.0", + "version": "0.5.1", "description": "A simple extension for setting Kagi as a default search engine, and automatically logging in to Kagi in incognito browsing windows", "background": { "service_worker": "src/background.js", diff --git a/firefox/manifest.json b/firefox/manifest.json index 5362bb6..ab97b7d 100644 --- a/firefox/manifest.json +++ b/firefox/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "Kagi Search for Firefox", - "version": "0.5.0", + "version": "0.5.1", "description": "A simple helper extension for setting Kagi as a default search engine, and automatically logging in to Kagi in incognito browsing windows.", "background": { "page": "src/background_page.html" diff --git a/shared/src/background.js b/shared/src/background.js index b611849..01f0356 100644 --- a/shared/src/background.js +++ b/shared/src/background.js @@ -210,17 +210,22 @@ async function loadStorageData() { loadStorageData(); // The function kagiSummarize is called when clicking the context menu item. -function kagiSummarize(info, tab) { +async function kagiSummarize(info) { // The linkUrl will be undefined if function is triggered by a page event. In that case, the url is taken from pageUrl const url = info.linkUrl || info.pageUrl; - browser.tabs.create({ - url: `https://kagi.com/summarizer/index.html?url=${encodeURIComponent( - url, - )}`, + + await browser.windows.create({ + url: browser.runtime.getURL( + `src/summarize_result.html?url=${encodeURIComponent(url)}`, + ), + focused: true, + width: 600, + height: 500, + type: 'popup', }); } -function kagiImageSearch(info, tab) { +function kagiImageSearch(info) { const imageUrl = info.srcUrl; browser.tabs.create({ url: `https://kagi.com/images?q=${imageUrl}&reverse=reference`, diff --git a/shared/src/popup.js b/shared/src/popup.js index 1280b2d..081b38a 100644 --- a/shared/src/popup.js +++ b/shared/src/popup.js @@ -273,9 +273,11 @@ async function setup() { if (!hasPermissions) { summarizeSection.style.display = 'none'; requestPermissionsSection.style.display = ''; + fastGptSection.style.display = 'none'; } else { summarizeSection.style.display = ''; requestPermissionsSection.style.display = 'none'; + fastGptSection.style.display = ''; } } advancedToggle.setAttribute('title', 'Advanced settings'); diff --git a/shared/src/summarize_result.js b/shared/src/summarize_result.js index d2fe765..8942944 100644 --- a/shared/src/summarize_result.js +++ b/shared/src/summarize_result.js @@ -89,11 +89,16 @@ async function setup() { const searchParams = new URLSearchParams(window.location.search); - // If there's no URL, get the currently active tab and default params - if (!searchParams.get('url')) { + if (!searchParams.get('summary_type')) { searchParams.set('summary_type', 'summary'); + } + + if (!searchParams.get('target_language')) { searchParams.set('target_language', ''); + } + // If there's no URL, get the currently active tab and default params + if (!searchParams.get('url')) { const tab = await getActiveTab(true); if (!tab) { @@ -107,25 +112,25 @@ async function setup() { const popupUrl = new URL(window.location.href); popupUrl.searchParams.set('url', tab.url); window.history.replaceState(null, '', popupUrl.toString()); + } - const { token, api_token, api_engine, summary_type, target_language } = - await fetchSettings(); + const { token, api_token, api_engine, summary_type, target_language } = + await fetchSettings(); - if (token) { - searchParams.set('token', token); - } - if (api_token) { - searchParams.set('api_token', api_token); - } - if (api_engine) { - searchParams.set('api_engine', api_engine); - } - if (summary_type) { - searchParams.set('summary_type', summary_type); - } - if (target_language) { - searchParams.set('target_language', target_language); - } + if (token) { + searchParams.set('token', token); + } + if (api_token) { + searchParams.set('api_token', api_token); + } + if (api_engine) { + searchParams.set('api_engine', api_engine); + } + if (summary_type) { + searchParams.set('summary_type', summary_type); + } + if (target_language) { + searchParams.set('target_language', target_language); } loadingElement.style.display = '';