From ae943d2e05f4b1c8891a99439171f61e71e65b3d Mon Sep 17 00:00:00 2001 From: andreocunha Date: Sun, 26 Feb 2023 17:50:30 -0300 Subject: [PATCH] using google search result to improve answer --- package-lock.json | 40 ++++++++++++++++++++++++++++++++++++ package.json | 2 ++ src/config.ts | 1 + src/content-script/index.tsx | 16 ++++++++++----- src/content-script/utils.ts | 29 ++++++++++++++++++++++++++ src/options/App.tsx | 19 +++++++++++++++++ 6 files changed, 102 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 527d9afa..1eacf403 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,11 +21,13 @@ "react-markdown": "^8.0.4", "rehype-highlight": "^6.0.0", "swr": "^2.0.0", + "turndown": "^7.1.1", "uuid": "^9.0.0" }, "devDependencies": { "@types/fs-extra": "^9.0.13", "@types/lodash-es": "^4.17.6", + "@types/turndown": "^5.0.1", "@types/uuid": "^9.0.0", "@types/webextension-polyfill": "^0.9.2", "@typescript-eslint/eslint-plugin": "^5.47.0", @@ -647,6 +649,12 @@ "@types/node": "*" } }, + "node_modules/@types/turndown": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/@types/turndown/-/turndown-5.0.1.tgz", + "integrity": "sha512-N8Ad4e3oJxh9n9BiZx9cbe/0M3kqDpOTm2wzj13wdDUxDPjfjloWIJaquZzWE1cYTAHpjOH3rcTnXQdpEfS/SQ==", + "dev": true + }, "node_modules/@types/unist": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/@types/unist/-/unist-2.0.6.tgz", @@ -1930,6 +1938,11 @@ "node": ">=6.0.0" } }, + "node_modules/domino": { + "version": "2.1.6", + "resolved": "https://registry.npmjs.org/domino/-/domino-2.1.6.tgz", + "integrity": "sha512-3VdM/SXBZX2omc9JF9nOPCtDaYQ67BGp5CoLpIQlO2KCAPETs8TcDHacF26jXadGbvUteZzRTeos2fhID5+ucQ==" + }, "node_modules/dotenv": { "version": "16.0.3", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz", @@ -5981,6 +5994,14 @@ "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==", "dev": true }, + "node_modules/turndown": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/turndown/-/turndown-7.1.1.tgz", + "integrity": "sha512-BEkXaWH7Wh7e9bd2QumhfAXk5g34+6QUmmWx+0q6ThaVOLuLUqsnkq35HQ5SBHSaxjSfSM7US5o4lhJNH7B9MA==", + "dependencies": { + "domino": "^2.1.6" + } + }, "node_modules/type-check": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz", @@ -6916,6 +6937,12 @@ "@types/node": "*" } }, + "@types/turndown": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/@types/turndown/-/turndown-5.0.1.tgz", + "integrity": "sha512-N8Ad4e3oJxh9n9BiZx9cbe/0M3kqDpOTm2wzj13wdDUxDPjfjloWIJaquZzWE1cYTAHpjOH3rcTnXQdpEfS/SQ==", + "dev": true + }, "@types/unist": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/@types/unist/-/unist-2.0.6.tgz", @@ -7799,6 +7826,11 @@ "esutils": "^2.0.2" } }, + "domino": { + "version": "2.1.6", + "resolved": "https://registry.npmjs.org/domino/-/domino-2.1.6.tgz", + "integrity": "sha512-3VdM/SXBZX2omc9JF9nOPCtDaYQ67BGp5CoLpIQlO2KCAPETs8TcDHacF26jXadGbvUteZzRTeos2fhID5+ucQ==" + }, "dotenv": { "version": "16.0.3", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz", @@ -10581,6 +10613,14 @@ } } }, + "turndown": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/turndown/-/turndown-7.1.1.tgz", + "integrity": "sha512-BEkXaWH7Wh7e9bd2QumhfAXk5g34+6QUmmWx+0q6ThaVOLuLUqsnkq35HQ5SBHSaxjSfSM7US5o4lhJNH7B9MA==", + "requires": { + "domino": "^2.1.6" + } + }, "type-check": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz", diff --git a/package.json b/package.json index a5185c52..4c8404c6 100644 --- a/package.json +++ b/package.json @@ -24,11 +24,13 @@ "react-markdown": "^8.0.4", "rehype-highlight": "^6.0.0", "swr": "^2.0.0", + "turndown": "^7.1.1", "uuid": "^9.0.0" }, "devDependencies": { "@types/fs-extra": "^9.0.13", "@types/lodash-es": "^4.17.6", + "@types/turndown": "^5.0.1", "@types/uuid": "^9.0.0", "@types/webextension-polyfill": "^0.9.2", "@typescript-eslint/eslint-plugin": "^5.47.0", diff --git a/src/config.ts b/src/config.ts index 830caf23..aa0d7537 100644 --- a/src/config.ts +++ b/src/config.ts @@ -41,6 +41,7 @@ const userConfigWithDefaultValue = { triggerMode: TriggerMode.Always, theme: Theme.Auto, language: Language.Auto, + googleSearch: true, } export type UserConfig = typeof userConfigWithDefaultValue diff --git a/src/content-script/index.tsx b/src/content-script/index.tsx index b8c76c9f..483ea97c 100644 --- a/src/content-script/index.tsx +++ b/src/content-script/index.tsx @@ -5,7 +5,7 @@ import { detectSystemColorScheme } from '../utils' import ChatGPTContainer from './ChatGPTContainer' import { config, SearchEngine } from './search-engine-configs' import './styles.scss' -import { getPossibleElementByQuerySelector } from './utils' +import { getGoogleSearchResult, getPossibleElementByQuerySelector } from './utils' async function mount(question: string, siteConfig: SearchEngine) { const container = document.createElement('div') @@ -50,11 +50,17 @@ async function run() { if (searchInput && searchInput.value) { console.debug('Mount ChatGPT on', siteName) const userConfig = await getUserConfig() - const searchValueWithLanguageOption = + let question = searchInput.value + + if (userConfig.googleSearch) { + question = (await getGoogleSearchResult(question)) + `\n\nQuestion:\n """${question}"""` + } + + question = userConfig.language === Language.Auto - ? searchInput.value - : `${searchInput.value}(in ${userConfig.language})` - mount(searchValueWithLanguageOption, siteConfig) + ? question + : `Answer the question in ${userConfig.language}:\n\n ${question}` + mount(question, siteConfig) } } diff --git a/src/content-script/utils.ts b/src/content-script/utils.ts index 8f727cf6..bd18d3dd 100644 --- a/src/content-script/utils.ts +++ b/src/content-script/utils.ts @@ -1,3 +1,4 @@ +import TurndownService from 'turndown' import Browser from 'webextension-polyfill' export function getPossibleElementByQuerySelector( @@ -32,3 +33,31 @@ export async function shouldShowRatingTip() { await Browser.storage.local.set({ ratingTipShowTimes: ratingTipShowTimes + 1 }) return ratingTipShowTimes >= 2 } + +export async function getGoogleSearchResult(question: string) { + let searchWithGoogle = '' + const googleUrl = `https://www.google.com/search?q=${question}` + + try { + const response = await fetch(googleUrl) + const html = await response.text() + const parser = new DOMParser() + const doc = parser.parseFromString(html, 'text/html') + const resultDivs = doc.querySelectorAll('div.kvH3mc.BToiNc.UK95Uc') + const turndownService = new TurndownService() + const results = Array.from(resultDivs).map((resultDiv) => + turndownService.turndown(resultDiv.innerHTML), + ) + console.log(results) + searchWithGoogle = ` + Use your knowledge and Web search to answer the question. + + Web search results: + ${results.join('\n')} + ` + } catch (error) { + console.log(error) + } + + return searchWithGoogle +} diff --git a/src/options/App.tsx b/src/options/App.tsx index caf695d6..10883877 100644 --- a/src/options/App.tsx +++ b/src/options/App.tsx @@ -1,4 +1,5 @@ import { CssBaseline, GeistProvider, Radio, Select, Text, Toggle, useToasts } from '@geist-ui/core' +import { ToggleEvent } from '@geist-ui/core/esm/toggle' import { capitalize } from 'lodash-es' import { useCallback, useEffect, useMemo, useState } from 'preact/hooks' import '../base.css' @@ -52,6 +53,15 @@ function OptionsPage(props: { theme: Theme; onThemeChange: (theme: Theme) => voi [setToast], ) + const onGoogleSearchChange = useCallback( + (event: ToggleEvent) => { + const googleSearch = event.target.checked + updateUserConfig({ googleSearch }) + setToast({ text: 'Changes saved', type: 'success' }) + }, + [setToast], + ) + return (