Skip to content

Commit 4dba9d5

Browse files
committed
add google Bard
1 parent 0fd09eb commit 4dba9d5

File tree

11 files changed

+188
-5
lines changed

11 files changed

+188
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Deep ChatGPT integrations in your browser, completely for free.
1010

1111
[![license][license-image]][license-url]
1212
[![release][release-image]][release-url]
13-
[![size](https://img.shields.io/badge/minified%20size-360%20kB-blue)][release-url]
13+
[![size](https://img.shields.io/badge/minified%20size-370%20kB-blue)][release-url]
1414
[![verfiy][verify-image]][verify-url]
1515

1616
English   |   [Indonesia](README_IN.md)   |   [简体中文](README_ZH.md)

README_IN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Integrasi Deep ChatGPT di browser Anda, sepenuhnya gratis.
1010

1111
[![license][license-image]][license-url]
1212
[![release][release-image]][release-url]
13-
[![size](https://img.shields.io/badge/minified%20size-360%20kB-blue)][release-url]
13+
[![size](https://img.shields.io/badge/minified%20size-370%20kB-blue)][release-url]
1414
[![verfiy][verify-image]][verify-url]
1515

1616
[Inggris](README.md)   |   Indonesia   |   [简体中文](README_ZH.md)

README_ZH.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
[![license][license-image]][license-url]
1212
[![release][release-image]][release-url]
13-
[![size](https://img.shields.io/badge/minified%20size-360%20kB-blue)][release-url]
13+
[![size](https://img.shields.io/badge/minified%20size-370%20kB-blue)][release-url]
1414
[![verfiy][verify-image]][verify-url]
1515

1616
[English](README.md)   |   [Indonesia](README_IN.md)   |   简体中文

src/background/index.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { generateAnswersWithWaylaidwandererApi } from '../services/apis/waylaidw
1515
import { generateAnswersWithPoeWebApi } from '../services/apis/poe-web.mjs'
1616
import {
1717
azureOpenAiApiModelKeys,
18+
bardWebModelKeys,
1819
bingWebModelKeys,
1920
chatgptApiModelKeys,
2021
chatgptWebModelKeys,
@@ -30,12 +31,14 @@ import {
3031
import '../_locales/i18n'
3132
import { openUrl } from '../utils/open-url'
3233
import {
34+
getBardCookies,
3335
getBingAccessToken,
3436
getChatGptAccessToken,
3537
registerPortListener,
3638
} from '../services/wrappers.mjs'
3739
import { refreshMenu } from './menus.mjs'
3840
import { registerCommands } from './commands.mjs'
41+
import { generateAnswersWithBardWebApi } from '../services/apis/bard-web.mjs'
3942

4043
function setPortProxy(port, proxyTabId) {
4144
port.proxy = Browser.tabs.connect(proxyTabId)
@@ -117,6 +120,9 @@ async function executeApi(session, port, config) {
117120
session,
118121
Models[session.modelName].value,
119122
)
123+
} else if (bardWebModelKeys.includes(session.modelName)) {
124+
const cookies = await getBardCookies()
125+
await generateAnswersWithBardWebApi(port, session.question, session, cookies)
120126
}
121127
}
122128

src/config/index.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export const chatgptWebModelKeys = [
2929
'chatgptPlus4Mobile',
3030
]
3131
export const bingWebModelKeys = ['bingFree4', 'bingFreeSydney']
32+
export const bardWebModelKeys = ['bardWebFree']
3233
export const gptApiModelKeys = ['gptApiDavinci']
3334
export const chatgptApiModelKeys = ['chatgptApi35', 'chatgptApi4_8k', 'chatgptApi4_32k']
3435
export const customApiModelKeys = ['customModel']
@@ -62,6 +63,7 @@ export const Models = {
6263
chatgptApi35: { value: 'gpt-3.5-turbo', desc: 'ChatGPT (GPT-3.5-turbo)' },
6364
bingFree4: { value: '', desc: 'Bing (Web, GPT-4)' },
6465
bingFreeSydney: { value: '', desc: 'Bing (Web, GPT-4, Sydney)' },
66+
bardWebFree: { value: '', desc: 'Bard (Web)' },
6567
poeAiWebSage: { value: 'sage', desc: 'Poe AI (Web, Sage)' },
6668
poeAiWebGPT4: { value: 'gpt-4', desc: 'Poe AI (Web, GPT-4)' },
6769
poeAiWebClaudePlus: { value: 'claude+', desc: 'Poe AI (Web, Claude+)' },

src/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"host_permissions": [
1313
"https://*.openai.com/",
1414
"https://*.bing.com/",
15-
"https://*.poe.com/"
15+
"https://*.poe.com/",
16+
"https://*.google.com/"
1617
],
1718
"permissions": [
1819
"commands",

src/manifest.v2.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"tabs",
1919
"https://*.openai.com/",
2020
"https://*.bing.com/",
21-
"https://*.poe.com/"
21+
"https://*.poe.com/",
22+
"https://*.google.com/"
2223
],
2324
"background": {
2425
"scripts": [

src/services/apis/bard-web.mjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { pushRecord } from './shared.mjs'
2+
import Bard from '../clients/bard'
3+
4+
/**
5+
* @param {Runtime.Port} port
6+
* @param {string} question
7+
* @param {Session} session
8+
* @param {string} cookies
9+
*/
10+
export async function generateAnswersWithBardWebApi(port, question, session, cookies) {
11+
// const { controller, messageListener } = setAbortController(port)
12+
const bot = new Bard(cookies)
13+
14+
// eslint-disable-next-line
15+
try {
16+
const { answer, conversationObj } = await bot.ask(question, session.bard_conversationObj || {})
17+
session.bard_conversationObj = conversationObj
18+
pushRecord(session, question, answer)
19+
console.debug('conversation history', { content: session.conversationRecords })
20+
// port.onMessage.removeListener(messageListener)
21+
port.postMessage({ answer: answer, done: true, session: session })
22+
} catch (err) {
23+
// port.onMessage.removeListener(messageListener)
24+
throw err
25+
}
26+
}

src/services/clients/bard/index.mjs

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// https://github.com/PawanOsman/GoogleBard
2+
3+
export default class Bard {
4+
cookies = ''
5+
6+
constructor(cookies) {
7+
this.cookies = cookies
8+
}
9+
10+
ParseResponse(text) {
11+
let resData = {
12+
r: '',
13+
c: '',
14+
rc: '',
15+
responses: [],
16+
}
17+
try {
18+
let parseData = (data) => {
19+
if (typeof data === 'string') {
20+
if (data?.startsWith('c_')) {
21+
resData.c = data
22+
return
23+
}
24+
if (data?.startsWith('r_')) {
25+
resData.r = data
26+
return
27+
}
28+
if (data?.startsWith('rc_')) {
29+
resData.rc = data
30+
return
31+
}
32+
resData.responses.push(data)
33+
}
34+
if (Array.isArray(data)) {
35+
data.forEach((item) => {
36+
parseData(item)
37+
})
38+
}
39+
}
40+
try {
41+
const lines = text.split('\n')
42+
for (let i in lines) {
43+
const line = lines[i]
44+
if (line.includes('wrb.fr')) {
45+
let data = JSON.parse(line)
46+
let responsesData = JSON.parse(data[0][2])
47+
responsesData.forEach((response) => {
48+
parseData(response)
49+
})
50+
}
51+
}
52+
} catch (e) {
53+
throw new Error(
54+
`Error parsing response: make sure you are using the correct cookie, copy the value of "__Secure-1PSID" cookie and set it like this: \n\nnew Bard("__Secure-1PSID=<COOKIE_VALUE>")\n\nAlso using a US proxy is recommended.\n\nIf this error persists, please open an issue on github.\nhttps://github.com/PawanOsman/GoogleBard`,
55+
)
56+
}
57+
} catch (err) {
58+
throw new Error(
59+
`Error parsing response: make sure you are using the correct cookie, copy the value of "__Secure-1PSID" cookie and set it like this: \n\nnew Bard("__Secure-1PSID=<COOKIE_VALUE>")\n\nAlso using a US proxy is recommended.\n\nIf this error persists, please open an issue on github.\nhttps://github.com/PawanOsman/GoogleBard`,
60+
)
61+
}
62+
return resData
63+
}
64+
65+
async GetRequestParams() {
66+
try {
67+
const response = await fetch('https://bard.google.com', {
68+
headers: {
69+
Cookie: this.cookies,
70+
},
71+
})
72+
const text = await response.text()
73+
const cfb2h = text.match(/"cfb2h":\s*"([^"]+)"/)?.[1]
74+
const SNlM0e = text.match(/"SNlM0e":\s*"([^"]+)"/)?.[1]
75+
const context = { googleData: { cfb2h, SNlM0e } }
76+
const at = context.googleData.SNlM0e
77+
const bl = context.googleData.cfb2h
78+
return { at, bl }
79+
} catch (e) {
80+
throw new Error(
81+
`Error parsing response: make sure you are using the correct cookie, copy the value of "__Secure-1PSID" cookie and set it like this: \n\nnew Bard("__Secure-1PSID=<COOKIE_VALUE>")\n\nAlso using a US proxy is recommended.\n\nIf this error persists, please open an issue on github.\nhttps://github.com/PawanOsman/GoogleBard`,
82+
)
83+
}
84+
}
85+
86+
async ask(prompt, conversationObj) {
87+
return await this.send(prompt, conversationObj)
88+
}
89+
90+
async send(prompt, conversationObj) {
91+
let conversation = {
92+
id: conversationObj.id || '',
93+
c: conversationObj.c || '',
94+
r: conversationObj.r || '',
95+
rc: conversationObj.rc || '',
96+
lastActive: Date.now(),
97+
}
98+
// eslint-disable-next-line
99+
try {
100+
let { at, bl } = await this.GetRequestParams()
101+
const response = await fetch(
102+
'https://bard.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate?' +
103+
new URLSearchParams({
104+
bl: bl,
105+
rt: 'c',
106+
_reqid: 0,
107+
}),
108+
{
109+
method: 'POST',
110+
body: new URLSearchParams({
111+
at: at,
112+
'f.req': JSON.stringify([
113+
null,
114+
`[[${JSON.stringify(prompt)}],null,${JSON.stringify([
115+
conversation.c,
116+
conversation.r,
117+
conversation.rc,
118+
])}]`,
119+
]),
120+
}),
121+
headers: {
122+
Cookie: this.cookies,
123+
},
124+
},
125+
)
126+
const data = await response.text()
127+
let parsedResponse = this.ParseResponse(data)
128+
conversation.c = parsedResponse.c
129+
conversation.r = parsedResponse.r
130+
conversation.rc = parsedResponse.rc
131+
const conversationObj = { c: conversation.c, r: conversation.r, rc: conversation.rc }
132+
return { answer: parsedResponse.responses[0], conversationObj: conversationObj }
133+
} catch (e) {
134+
throw e
135+
}
136+
}
137+
}

src/services/init-session.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { v4 as uuidv4 } from 'uuid'
2424
* @property {string|null} bingWeb_parentMessageId
2525
* @property {Object|null} bingWeb_jailbreakConversationCache
2626
* @property {number|null} poe_chatId
27+
* @property {object|null} bard_conversationObj
2728
*/
2829
/**
2930
* @param {string|null} question
@@ -74,5 +75,8 @@ export function initSession({
7475

7576
// poe
7677
poe_chatId: null,
78+
79+
// bard
80+
bard_conversationObj: null,
7781
}
7882
}

0 commit comments

Comments
 (0)