Skip to content

Commit

Permalink
small tweaks and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbenz committed Aug 23, 2024
1 parent 90c6e33 commit 8a310ca
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This sample demonstrates how to use the experimental Summarization API in Chrome

## Overview

The extension provides a chat interface using the prompt API with Chrome's built-in Gemini Nano model.
The extension summarizes the content of the currently open tab. It uses Mozilla's [readability](https://github.com/mozilla/readability) library to extract the content of the currently active tab and displays a summary of the page generated by Chrome's built-in summarization API in a side panel.

## Running this extension

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"background": {
"service_worker": "background.js"
},
"permissions": ["tabs", "scripting", "sidePanel", "storage", "offscreen"],
"permissions": ["tabs", "scripting", "sidePanel", "storage"],
"host_permissions": ["http://*/*", "https://*/*"],
"side_panel": {
"default_path": "sidepanel/index.html"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "quizzify",
"name": "Summarization Example",
"private": true,
"version": "1.0.0",
"description": "",
"main": "background.js",
"scripts": {
"build": "rollup -c rollup.config.mjs"
},
"keywords": [],
"author": "",
"license": "Apache 2.0",
"devDependencies": {
"@mozilla/readability": "^0.5.0",
"@rollup/plugin-commonjs": "^26.0.1",
"@rollup/plugin-node-resolve": "^15.2.3",
"rollup": "^4.18.1"
"@mozilla/readability": "0.5.0",
"@rollup/plugin-commonjs": "26.0.1",
"@rollup/plugin-node-resolve": "15.2.3",
"rollup": "4.18.1"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ function canBeParsed(document) {

function parse(document) {
if (!canBeParsed(document)) {
console.log('cannot be parsed');
return false;
}
const documentClone = document.cloneNode(true);
const article = new Readability(documentClone).parse();
console.log('parse result', article.textContent);
return article.textContent; // .replace(/^\s+|\s+$|\s+(?=\s)/g, "");
return article.textContent;
}

parse(window.document);
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const MAX_MODEL_CHARS = 4000;

let pageContent = '';

const summaryElement = document.body.querySelector('#summary');
const warningElement = document.body.querySelector('#warning');

chrome.storage.session.get('pageContent', ({ pageContent }) => {
onContentChange(pageContent);
});
Expand All @@ -14,23 +17,20 @@ chrome.storage.session.onChanged.addListener((changes) => {
onContentChange(pageContent.newValue);
});

const summaryElement = document.body.querySelector('#summary');
const warningElement = document.body.querySelector('#warning');

async function onContentChange(newContent) {
if (pageContent == newContent) {
console.log('no new content');
// no new content, do nothing
return;
}
pageContent = newContent;
let summary;
if (newContent) {
if (newContent.length > MAX_MODEL_CHARS) {
showWarning(
updateWarning(
`Text is too long for summarization with ${newContent.length} characters (maximum supported content length is ~4000 characters).`
);
} else {
showWarning('');
updateWarning('');
}
showSummary('Loading...');
summary = await generateSummary(newContent);
Expand All @@ -43,14 +43,15 @@ async function onContentChange(newContent) {

async function generateSummary(text) {
try {
let session = await createSummarizationSession();
let session = await createSummarizationSession((message, progress) => {
console.log(`${message} (${progress.loaded}/${progress.total})`);
});
let summary = await session.summarize(text);
session.destroy();
return summary;
} catch (e) {
console.log('Summary generation failed');
console.error(e);
console.log('Prompt:\n\n' + text);
return 'Error: ' + e.message;
}
}
Expand Down Expand Up @@ -90,9 +91,9 @@ async function showSummary(text) {
}
}

async function showWarning(text) {
warningElement.textContent = text;
if (text) {
async function updateWarning(warning) {
warningElement.textContent = warning;
if (warning) {
warningElement.removeAttribute('hidden');
} else {
warningElement.setAttribute('hidden', '');
Expand Down

0 comments on commit 8a310ca

Please sign in to comment.