Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // by wolep
- const gemini = {
- getNewCookie: async function () {
- const r = await fetch("https://gemini.google.com/_/BardChatUi/data/batchexecute?rpcids=maGuAc&source-path=%2F&bl=boq_assistant-bard-web-server_20250814.06_p1&f.sid=-7816331052118000090&hl=en-US&_reqid=173780&rt=c", {
- "headers": {
- "content-type": "application/x-www-form-urlencoded;charset=UTF-8",
- },
- "body": "f.req=%5B%5B%5B%22maGuAc%22%2C%22%5B0%5D%22%2Cnull%2C%22generic%22%5D%5D%5D&",
- "method": "POST"
- });
- console.log('get new cookie')
- return r.headers.getSetCookie()[0].split("; ")[0]
- },
- ask: async function (prompt, previousId = null) {
- if (typeof (prompt) !== "string" || !prompt?.trim()?.length) throw Error(`mana prompt nya?`)
- let resumeArray = null
- let cookie = null
- if (previousId) {
- const s = atob(previousId)
- const j = JSON.parse(s)
- resumeArray = j.newResumeArray
- cookie = j.cookie
- }
- const headers = {
- "content-type": "application/x-www-form-urlencoded;charset=UTF-8",
- "x-goog-ext-525001261-jspb": "[1,null,null,null,\"9ec249fc9ad08861\",null,null,null,[4]]",
- cookie: cookie || await this.getNewCookie()
- }
- const b = [[prompt], ["en-US"], resumeArray]
- const a = [null, JSON.stringify(b)]
- const obj = { "f.req": JSON.stringify(a) }
- const body = new URLSearchParams(obj)
- const response = await fetch(`https://gemini.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate?bl=boq_assistant-bard-web-server_20250729.06_p0&f.sid=4206607810970164620&hl=en-US&_reqid=2813378&rt=c`, {
- headers,
- body,
- 'method': 'post'
- })
- if (!response.ok) throw Error(`${response.status} ${response.statusText} ${await response.text() || `(body response kosong)`}`)
- const data = await response.text()
- const match = data.matchAll(/^\d+\n(.+?)\n/gm)
- const array = Array.from(match).reverse()
- const selectedArray = array[3][1]
- const realArray = JSON.parse(selectedArray)
- const parse1 = JSON.parse(realArray[0][2])
- const newResumeArray = [...parse1[1], parse1[4][0][0]]
- const text = parse1[4][0][1][0].replace(/\*\*(.+?)\*\*/g, `*$1*`)
- const id = btoa(JSON.stringify({ newResumeArray, cookie: headers.cookie }))
- const result = { text, id }
- return result
- }
- }
- // cara pakai
- gemini.ask('kenalin aku wolep')
- .then(console.log)
- .catch(err => { console.log(err.message) })
- /* output
- {
- text: 'Halo Wolep! Senang berkenalan denganmu. Ada yang bisa saya bantu?',
- id: 'eyJuZXdSZXN1bWV....V4Rk5ydS1adjhRIn0='
- }
- untuk resume cukup lampirkan id dari chat sebelumnya
- gemini.ask('siapa namaku?', 'eyJuZXdSZXN1bWV....V4Rk5ydS1adjhRIn0=')
- .then(console.log)
- .catch(err => { console.log(err.message) })
- output
- {
- text: 'Kamu mengenalkan dirimu sebagai *Wolep*.',
- id: 'eyJuZXdSZXN1bWV.....ItUTV4Rk5ydS1adjhRIn0=' <-- kalau mau resume lampirin lagi id baru ini
- }
- dan lanjutkan
- */
Advertisement
Add Comment
Please, Sign In to add comment