-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: updated multimodal live app (#1544)
# Description Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Follow the [`CONTRIBUTING` Guide](https://github.com/GoogleCloudPlatform/generative-ai/blob/main/CONTRIBUTING.md). - [x] You are listed as the author in your notebook or README file. - [x] Your account is listed in [`CODEOWNERS`](https://github.com/GoogleCloudPlatform/generative-ai/blob/main/.github/CODEOWNERS) for the file(s). - [x] Make your Pull Request title in the <https://www.conventionalcommits.org/> specification. - [x] Ensure the tests and linter pass (Run `nox -s format` from the repository root to format). - [x] Appropriate docs were updated (if necessary) Fixes #<issue_number_goes_here> 🦕 --------- Co-authored-by: code-review-assist[bot] <182814678+code-review-assist[bot]@users.noreply.github.com>
- Loading branch information
1 parent
c84bde2
commit acf9b78
Showing
11 changed files
with
1,197 additions
and
458 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
65 changes: 65 additions & 0 deletions
65
gemini/multimodal-live-api/websocket-demo-app/frontend/cookieJar.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
class CookieJar { | ||
/** | ||
* @class CookieJar | ||
* @classdesc A utility class for managing cookies associated with HTML input elements. | ||
*/ | ||
|
||
/** | ||
* @static | ||
* @method init | ||
* @memberof CookieJar | ||
* @description Initializes the CookieJar for a given element. Loads saved value from cookie and sets up auto-saving on value change. | ||
* @param {string} elementId - The ID of the HTML input element. | ||
*/ | ||
static init(elementId) { | ||
const element = document.getElementById(elementId); | ||
if (!element) { | ||
console.error(`❌ Element with ID '${elementId}' not found.`); | ||
return; | ||
} | ||
|
||
const cookieName = `CookieJar_${elementId}`; | ||
|
||
// Load existing value from cookie | ||
const savedValue = CookieJar.getCookie(cookieName); | ||
if (savedValue) { | ||
console.log(`🍪 Found cookie for ${elementId}. Loading value: ${savedValue}`); | ||
element.value = savedValue; | ||
} | ||
|
||
// Save on value change | ||
element.addEventListener('input', () => { | ||
console.log(`🍪 Saving value for ${elementId} to cookie...`); | ||
CookieJar.setCookie(cookieName, element.value); | ||
}); | ||
} | ||
|
||
/** | ||
* @static | ||
* @method setCookie | ||
* @memberof CookieJar | ||
* @description Sets a cookie with the given name, value, and optional expiration days. | ||
* @param {string} name - The name of the cookie. | ||
* @param {string} value - The value to store in the cookie. | ||
* @param {number} [days=365] - The number of days until the cookie expires. Defaults to 365. | ||
*/ | ||
static setCookie(name, value, days = 365) { | ||
const expires = new Date(); | ||
expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000); | ||
document.cookie = `${name}=${encodeURIComponent(value)};expires=${expires.toUTCString()};path=/`; | ||
console.log(`🍪 Cookie '${name}' set successfully!`); | ||
} | ||
|
||
/** | ||
* @static | ||
* @method getCookie | ||
* @memberof CookieJar | ||
* @description Retrieves the value of a cookie with the given name. | ||
* @param {string} name - The name of the cookie to retrieve. | ||
* @returns {string|null} The value of the cookie if found, otherwise null. | ||
*/ | ||
static getCookie(name) { | ||
const cookieValue = document.cookie.match(`(^|;)\\s*${name}\\s*=\\s*([^;]+)`); | ||
return cookieValue ? decodeURIComponent(cookieValue.pop()) : null; | ||
} | ||
} |
Oops, something went wrong.