Skip to content

Commit

Permalink
Save Summary Type & Target Language
Browse files Browse the repository at this point in the history
This implements saving of the summary type and target language choices so they can be used for the next summarization (including via shortcut).

It effectively closes the request at https://kagifeedback.org/d/2321-chrome-extension-summarizer-extension-always-uses-summary-instead-of-key-moments

It also updates the expected `node` and `npm` versions for development (current LTS) and the `nodemon` development dependency, to resolve a security vulnerability without having to use `package.json:overrides`.
  • Loading branch information
BrunoBernardino committed Nov 3, 2023
1 parent 8e33609 commit ea05a6b
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 36 deletions.
2 changes: 0 additions & 2 deletions .nodemonignore

This file was deleted.

2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v18.16.1
v20.9.0
2 changes: 1 addition & 1 deletion chrome/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Kagi Search for Chrome",
"version": "0.4.1",
"version": "0.4.2",
"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",
Expand Down
2 changes: 1 addition & 1 deletion firefox/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Kagi Search for Firefox",
"version": "0.4.1",
"version": "0.4.2",
"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"
Expand Down
5 changes: 5 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"verbose": false,
"ignore": [".git", "built/**", "node_modules/**/node_modules"],
"ext": "js,json,css,html"
}
40 changes: 20 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"private": true,
"scripts": {
"watch-firefox": "nodemon --ext js,json,css,html build.js firefox watch",
"watch-chrome": "nodemon --ext js,json,css,html build.js chrome watch",
"watch-chrome": "nodemon build.js chrome watch",
"build": "npm run build-firefox && npm run build-chrome",
"build-firefox": "node build.js firefox",
"build-chrome": "node build.js chrome",
Expand All @@ -26,14 +26,11 @@
"homepage": "https://github.com/kagisearch/browser_extensions#readme",
"devDependencies": {
"adm-zip": "0.5.10",
"nodemon": "2.0.22",
"nodemon": "3.0.1",
"rome": "12.1.3"
},
"engines": {
"node": "18.x",
"npm": "9.x"
},
"overrides": {
"semver": "7.5.2"
"node": "20.x",
"npm": "10.x"
}
}
25 changes: 23 additions & 2 deletions shared/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ let sessionToken = undefined;
let syncSessionFromExisting = true;
let sessionApiToken = undefined;
let sessionApiEngine = undefined;
let sessionSummaryType = undefined;
let sessionTargetLanguage = undefined;
let IS_CHROME = true;

// Very hacky, but currently works flawlessly
Expand All @@ -16,14 +18,20 @@ if (typeof browser.runtime.getBrowserInfo === 'function') {
}

async function saveToken(
{ token, api_token, api_engine, sync } = {},
{ token, api_token, api_engine, sync, summary_type, target_language } = {},
isManual = false,
) {
sessionToken = typeof token !== 'undefined' ? token : sessionToken;
sessionApiToken =
typeof api_token !== 'undefined' ? api_token : sessionApiToken;
sessionApiEngine =
typeof api_engine !== 'undefined' ? api_engine : sessionApiEngine;
sessionSummaryType =
typeof summary_type !== 'undefined' ? summary_type : sessionSummaryType;
sessionTargetLanguage =
typeof target_language !== 'undefined'
? target_language
: sessionTargetLanguage;

let shouldSync = sync || !isManual;
if (typeof sessionToken === 'undefined' || sessionToken.trim().length === 0) {
Expand All @@ -38,6 +46,8 @@ async function saveToken(
sync_existing: shouldSync,
api_token: sessionApiToken,
api_engine: sessionApiEngine,
summary_type: sessionSummaryType,
target_language: sessionTargetLanguage,
});

await updateRules();
Expand All @@ -48,6 +58,8 @@ async function saveToken(
token: sessionToken,
api_token: sessionApiToken,
api_engine: sessionApiEngine,
summary_type: sessionSummaryType,
target_language: sessionTargetLanguage,
});
}

Expand Down Expand Up @@ -168,12 +180,21 @@ browser.commands?.onCommand.addListener(async (command) => {
});

async function loadStorageData() {
const { token, sync_existing, api_token, api_engine } = await fetchSettings();
const {
token,
sync_existing,
api_token,
api_engine,
summary_type,
target_language,
} = await fetchSettings();

sessionToken = token;
syncSessionFromExisting = sync_existing;
sessionApiToken = api_token;
sessionApiEngine = api_engine;
sessionSummaryType = summary_type;
sessionTargetLanguage = target_language;
}

loadStorageData();
Expand Down
6 changes: 6 additions & 0 deletions shared/src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ export async function fetchSettings() {
const syncObject = await browser.storage.local.get('sync_existing');
const apiObject = await browser.storage.local.get('api_token');
const apiEngineObject = await browser.storage.local.get('api_engine');
const summaryTypeObject = await browser.storage.local.get('summary_type');
const targetLanguageObject = await browser.storage.local.get(
'target_language',
);

return {
token: sessionObject?.session_token,
Expand All @@ -111,6 +115,8 @@ export async function fetchSettings() {
: true,
api_token: apiObject?.api_token,
api_engine: apiEngineObject?.api_engine,
summary_type: summaryTypeObject?.summary_type,
target_language: targetLanguageObject?.target_language,
};
}

Expand Down
29 changes: 28 additions & 1 deletion shared/src/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,19 @@ async function setup() {

const api_engine = apiEngineSelect.value;

const summary_type = summaryTypeSelect.value;

const target_language = targetLanguageSelect.value;

saveTokenButton.innerText = 'Saving...';

await browser.runtime.sendMessage({
type: 'save_token',
token,
api_token,
api_engine,
summary_type,
target_language,
});
});

Expand Down Expand Up @@ -284,7 +290,18 @@ async function setup() {
type: 'popup',
});

window.close();
// Save new summary_type preferences
await browser.runtime.sendMessage({
type: 'save_token',
token: searchParams.token,
api_token: searchParams.api_token,
api_engine: searchParams.api_engine,
summary_type: searchParams.summary_type,
target_language: searchParams.target_language,
});

// Give the browser time to save the info before closing the window (when await isn't respected)
setTimeout(() => window.close(), 100);
}

summarizePageButton.addEventListener('click', handleSummarizePageButtonClick);
Expand Down Expand Up @@ -332,6 +349,8 @@ async function setup() {
api_token,
sync_existing,
api_engine,
summary_type,
target_language,
} = {}) {
if (token) {
tokenInput.value = token;
Expand Down Expand Up @@ -370,6 +389,14 @@ async function setup() {
apiEngineSelect.value = api_engine;
}

if (summary_type) {
summaryTypeSelect.value = summary_type;
}

if (target_language) {
targetLanguageSelect.value = target_language;
}

const hasIncognitoAccess =
await browser.extension.isAllowedIncognitoAccess();

Expand Down
9 changes: 8 additions & 1 deletion shared/src/summarize_result.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ async function setup() {
popupUrl.searchParams.set('url', tab.url);
window.history.replaceState(null, '', popupUrl.toString());

const { token, api_token, api_engine } = await fetchSettings();
const { token, api_token, api_engine, summary_type, target_language } =
await fetchSettings();

if (token) {
searchParams.set('token', token);
Expand All @@ -118,6 +119,12 @@ async function setup() {
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 = '';
Expand Down

0 comments on commit ea05a6b

Please sign in to comment.