### Local Storage Management Source: https://context7.com/etcextensions/edit-this-cookie/llms.txt A helper object `ls` for managing data in `localStorage`, including setting, getting with defaults, and removing items. Preferences and statistics are accessed and modified directly. ```javascript var ls = { set: function(name, value) { localStorage.setItem(name, JSON.stringify(value)); }, get: function(name, default_value) { if (localStorage[name] === undefined) { if (default_value !== undefined) { ls.set(name, default_value); } return default_value || null; } try { return JSON.parse(localStorage.getItem(name)); } catch (e) { ls.set(name, default_value); return default_value; } }, remove: function(name) { localStorage.removeItem(name); } }; ``` ```javascript console.log("Show alerts: " + preferences.showAlerts); console.log("Export format: " + preferences.copyCookiesType); console.log("Sort type: " + preferences.sortCookiesType); ``` ```javascript preferences.showContextMenu = true; preferences.refreshAfterSubmit = true; preferences.copyCookiesType = "netscape"; ``` ```javascript console.log("Cookies created: " + data.nCookiesCreated); console.log("Cookies deleted: " + data.nCookiesDeleted); console.log("Cookies protected: " + data.nCookiesProtected); console.log("Cookies blocked: " + data.nCookiesFlagged); console.log("Popup opened: " + data.nPopupClicked + " times"); ``` -------------------------------- ### GET chrome.cookies.getAll Source: https://context7.com/etcextensions/edit-this-cookie/llms.txt Retrieves all cookies matching a specific filter from the current tab's cookie store. ```APIDOC ## GET chrome.cookies.getAll ### Description Retrieves all cookies matching a filter from the current tab's cookie store. The Filter class allows setting URL, domain, name, secure, and session parameters. ### Method GET ### Parameters #### Query Parameters - **filter** (Object) - Required - A filter object containing criteria such as url, domain, name, secure, and session. ``` -------------------------------- ### Export Cookies to String Source: https://context7.com/etcextensions/edit-this-cookie/llms.txt Converts an array of cookies into different string formats like JSON, Netscape, semicolon-separated pairs, or Perl::LWP. Use the `get` method for the user's preferred format or specific methods for direct format conversion. ```javascript var cookies = [ { name: "session", value: "abc123", domain: ".example.com", path: "/", secure: true, httpOnly: true, hostOnly: false, session: false, expirationDate: 1735689600 } ]; var exportString = cookiesToString.get(cookies, "https://example.com"); ``` ```javascript var netscapeFormat = cookiesToString.netscape(cookies, "https://example.com"); ``` ```javascript var pairsFormat = cookiesToString.semicolonPairs(cookies); ``` ```javascript var lwpFormat = cookiesToString.lpw(cookies); ``` -------------------------------- ### Connect to Background Script from DevTools Panel Source: https://context7.com/etcextensions/edit-this-cookie/llms.txt Establishes a connection to the background script from the DevTools panel to enable communication. Listen for messages from the background script and send messages to it. ```javascript // Connect to background script from DevTools panel var backgroundPageConnection = chrome.runtime.connect({ name: "devtools-page" }); // Listen for messages from background backgroundPageConnection.onMessage.addListener(function(message) { if (message.action === "getall") { createTable(message); // message.url, message.cks } else if (message.action === "refresh") { location.reload(true); } }); // Request cookies for inspected tab var tabId = chrome.devtools.inspectedWindow.tabId; backgroundPageConnection.postMessage({ action: "getall", tabId: tabId }); // Update a cookie from the DevTools panel function updateCookie() { var newCookie = { url: tabURL, name: "updated_cookie", value: "new_value", path: "/", storeId: "0", secure: true, httpOnly: false, sameSite: "lax" }; backgroundPageConnection.postMessage({ action: "submitCookie", cookie: newCookie, origName: "original_cookie_name" // For renaming }); } ``` -------------------------------- ### Create or Update Cookies Source: https://context7.com/etcextensions/edit-this-cookie/llms.txt Shows how to format cookie objects for creation and use the chrome.cookies.set API for both persistent and session-based cookies. ```javascript // Convert existing cookie to creation format var fullCookie = { name: "user_session", value: "abc123", domain: ".example.com", path: "/", secure: true, httpOnly: true, session: false, hostOnly: false, expirationDate: Math.floor(Date.now() / 1000) + 86400, // 24 hours storeId: "0" }; var newCookie = cookieForCreationFromFullCookie(fullCookie); // Result: { // url: "https://.example.com/", // name: "user_session", // value: "abc123", // domain: ".example.com", // path: "/", // secure: true, // httpOnly: true, // expirationDate: , // storeId: "0" // } // Create the cookie chrome.cookies.set(newCookie, function(cookie) { if (cookie) { console.log("Cookie created: " + cookie.name); } else { console.log("Failed to create cookie - check Chrome error"); } }); // Create a session cookie (no expiration) var sessionCookie = { url: "https://example.com/", name: "temp_data", value: "xyz789", path: "/", secure: true, httpOnly: false, sameSite: "lax" // No expirationDate = session cookie }; chrome.cookies.set(sessionCookie); ``` -------------------------------- ### POST chrome.cookies.set Source: https://context7.com/etcextensions/edit-this-cookie/llms.txt Creates a new cookie or updates an existing one in the browser. ```APIDOC ## POST chrome.cookies.set ### Description Creates a new cookie or updates an existing one using Chrome's cookies.set API. ### Method POST ### Parameters #### Request Body - **url** (string) - Required - The URL to associate with the cookie. - **name** (string) - Required - The name of the cookie. - **value** (string) - Required - The value of the cookie. - **domain** (string) - Optional - The domain of the cookie. - **path** (string) - Optional - The path of the cookie. - **secure** (boolean) - Optional - Whether the cookie is secure. - **httpOnly** (boolean) - Optional - Whether the cookie is HttpOnly. - **expirationDate** (number) - Optional - The expiration date in seconds since the epoch. - **storeId** (string) - Optional - The ID of the cookie store. ``` -------------------------------- ### Retrieve Cookies with Filters Source: https://context7.com/etcextensions/edit-this-cookie/llms.txt Uses the Filter class to query cookies by URL or domain, then iterates through the results to log cookie properties. ```javascript // Create a filter to get cookies for a specific URL var filter = new Filter(); filter.setUrl("https://example.com"); // Get cookies using Chrome's API with the filter chrome.cookies.getAll(filter.getFilter(), function(cookies) { // cookies is an array of Cookie objects cookies.forEach(function(cookie) { console.log("Name: " + cookie.name); console.log("Value: " + cookie.value); console.log("Domain: " + cookie.domain); console.log("Path: " + cookie.path); console.log("Secure: " + cookie.secure); console.log("HttpOnly: " + cookie.httpOnly); console.log("Session: " + cookie.session); console.log("Expiration: " + new Date(cookie.expirationDate * 1000)); console.log("SameSite: " + cookie.sameSite); }); }); // Filter by domain only var domainFilter = new Filter(); domainFilter.setDomain("example.com"); chrome.cookies.getAll(domainFilter.getFilter(), function(cookies) { console.log("Found " + cookies.length + " cookies for domain"); }); ``` -------------------------------- ### Import Cookies from JSON Source: https://context7.com/etcextensions/edit-this-cookie/llms.txt Parses a JSON string containing cookie data and imports them into the browser. Handles both single cookie objects and arrays of cookies. Ensure the JSON is valid and correctly formatted. ```javascript function importCookies() { var jsonText = '[{"name":"auth","value":"token123","domain":".example.com","path":"/","secure":true,"httpOnly":true,"session":false,"expirationDate":1735689600}]'; try { var cookieArray = $.parseJSON(jsonText); if (Object.prototype.toString.apply(cookieArray) === "[object Object]") { cookieArray = [cookieArray]; } for (var i = 0; i < cookieArray.length; i++) { var cJSON = cookieArray[i]; var cookie = cookieForCreationFromFullCookie(cJSON); chrome.cookies.set(cookie, function(result) { if (result) { console.log("Imported: " + result.name); } }); } data.nCookiesImported += cookieArray.length; console.log("Import complete"); } catch (e) { console.error("Import failed: " + e.message); } } ``` -------------------------------- ### Toggle Cookie Protection Source: https://context7.com/etcextensions/edit-this-cookie/llms.txt Use `switchReadOnlyRule` to make cookies read-only. The background script monitors changes and restores protected cookies. ```javascript var cookie = { name: "important_session", value: "secret123", domain: ".example.com", path: "/", secure: true, httpOnly: true, storeId: "0" }; var isProtected = switchReadOnlyRule(cookie); console.log("Cookie is now " + (isProtected ? "protected" : "unprotected")); ``` ```javascript chrome.cookies.onChanged.addListener(function(changeInfo) { var removed = changeInfo.removed; var cookie = changeInfo.cookie; var cause = changeInfo.cause; if (cause === "expired" || cause === "evicted") return; for (var i = 0; i < data.readOnly.length; i++) { var protectedCookie = data.readOnly[i]; if (compareCookies(cookie, protectedCookie)) { if (removed) { var newCookie = cookieForCreationFromFullCookie(protectedCookie); chrome.cookies.set(newCookie); ++data.nCookiesProtected; } return; } } }); ``` ```javascript deleteReadOnlyRule(0); ``` -------------------------------- ### Add Cookie Filter Rule Source: https://context7.com/etcextensions/edit-this-cookie/llms.txt Creates a filter rule to automatically delete matching cookies when they are set. Rules can use regex for domain, name, and value. Adding a rule immediately deletes existing matching cookies. ```javascript var domainRule = { domain: "tracking\\.example\\.com" // Regex pattern }; addBlockRule(domainRule); ``` ```javascript var nameRule = { name: "^_ga" // Blocks _ga, _gat, _gid, etc. }; addBlockRule(nameRule); ``` ```javascript var specificRule = { domain: "example\\.com", name: "ad_preferences", value: undefined // Match any value }; addBlockRule(specificRule); ``` -------------------------------- ### Delete Cookies Source: https://context7.com/etcextensions/edit-this-cookie/llms.txt Demonstrates deleting individual cookies, batch deleting via a list, and direct usage of the Chrome cookies.remove API. ```javascript // Delete a single cookie var url = buildUrl(".example.com", "/", "https://example.com"); // url = "https://example.com/" deleteCookie(url, "session_id", "0", function(success) { if (success) { console.log("Cookie deleted successfully"); } else { console.log("Failed to delete cookie"); } }); // Delete all cookies for a URL var cookieList = [ { domain: ".example.com", path: "/", name: "session_id", storeId: "0" }, { domain: ".example.com", path: "/", name: "user_pref", storeId: "0" } ]; deleteAll(cookieList, "https://example.com"); // Direct Chrome API usage chrome.cookies.remove({ url: "https://example.com/", name: "tracking_cookie", storeId: "0" }, function(details) { if (details) { console.log("Removed cookie at: " + details.url); } }); ``` -------------------------------- ### Access and Delete Block Rules Source: https://context7.com/etcextensions/edit-this-cookie/llms.txt Allows viewing the number of active filters and iterating through them to display their properties. A specific rule can be deleted by its index. ```javascript console.log("Active filters: " + data.filters.length); data.filters.forEach(function(filter, index) { console.log(index + ": domain=" + filter.domain + ", name=" + filter.name); }); deleteBlockRule(0); ``` -------------------------------- ### Check if Cookie Matches Filter Rule Source: https://context7.com/etcextensions/edit-this-cookie/llms.txt Determines if a given cookie (name, domain, value) matches a specified filter rule. The rule can contain regex patterns for domain, name, and value. ```javascript function filterMatchesCookie(rule, name, domain, value) { var ruleDomainReg = new RegExp(rule.domain); var ruleNameReg = new RegExp(rule.name); var ruleValueReg = new RegExp(rule.value); if (rule.domain !== undefined && domain.match(ruleDomainReg) === null) { return false; } if (rule.name !== undefined && name.match(ruleNameReg) === null) { return false; } if (rule.value !== undefined && value.match(ruleValueReg) === null) { return false; } return true; } ``` -------------------------------- ### Limit Cookie Expiration Source: https://context7.com/etcextensions/edit-this-cookie/llms.txt Enforce a maximum cookie age by setting `useMaxCookieAge` and `maxCookieAge`. The `chrome.cookies.onChanged` listener automatically shortens expirations. ```javascript preferences.useMaxCookieAge = true; preferences.maxCookieAge = 7; preferences.maxCookieAgeType = 86400; ``` ```javascript chrome.cookies.onChanged.addListener(function(changeInfo) { if (changeInfo.removed) return; var cookie = changeInfo.cookie; if (preferences.useMaxCookieAge && preferences.maxCookieAgeType > 0) { var maxAllowedExpiration = Math.round((new Date).getTime() / 1000) + (preferences.maxCookieAge * preferences.maxCookieAgeType); if (cookie.expirationDate !== undefined && cookie.expirationDate > maxAllowedExpiration + 60) { var newCookie = cookieForCreationFromFullCookie(cookie); if (!cookie.session) { newCookie.expirationDate = maxAllowedExpiration; } chrome.cookies.set(newCookie); ++data.nCookiesShortened; } } }); ``` -------------------------------- ### DELETE chrome.cookies.remove Source: https://context7.com/etcextensions/edit-this-cookie/llms.txt Removes a specific cookie from the browser by URL, name, and store ID. ```APIDOC ## DELETE chrome.cookies.remove ### Description Removes a cookie from the browser by URL, name, and store ID. ### Method DELETE ### Parameters #### Request Body - **url** (string) - Required - The URL associated with the cookie. - **name** (string) - Required - The name of the cookie to remove. - **storeId** (string) - Required - The ID of the cookie store. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.