chrome.cookies

文章推薦指數: 80 %
投票人數:10人

Use the chrome.cookies API to query and modify cookies, and to be notified when they change. Permissions. cookies. SkiptocontentHomeDocsBlogHomeDocsBlogExtensionsWelcomeWhat'snewinChromeextensionsGettingstartedIntroducingManifestV3WelcometoManifestV3ExtensionsplatformvisionOverviewofManifestV3MigratingtoManifestV3ManifestV3migrationchecklistManifestV2supporttimelineOverviewWhatareextensions?Whatarethemes?FrequentlyaskedquestionsExtensionsqualityguidelinesFAQAPIReferenceSamplesDevelopextensionsandthemesExtensiondevelopmentoverviewManifestfileformatArchitectureoverviewDeclarepermissionsDesigntheuserinterfaceDebuggingextensionsSamplesIndepth:coreconceptsMessagepassingContentscriptsManageeventswithserviceworkersMatchpatternsUsingpromisesCross-originisolationIndepth:securityCross-originXMLHttpRequestUsingevalinChromeextensionsIndepth:moretopicsOverridingChromesettingsExtendingDevToolsOAuth2:AuthenticateuserswithGoogleOverridingChromepagesRichnotificationsAPIModernizeyourextensionsMigratingfrombackgroundpagestoserviceworkersBestpracticesProtectuserprivacyDeclarepermissionsandwarnusersStaysecureAccessibility(a11y)LocalizationmessageformatsGiveusersoptionsDistributingyourextensionsExtensionhostingAlternativeextensioninstallationmethodsInstallingextensionsonLinuxTutorial:GoogleanalyticsManifestV2AboutManifestV2GettingstartedOverviewWhatareextensions?Whatarethemes?FrequentlyaskedquestionsDevelopextensionsandthemesExtensiondevelopmentoverviewManifestfileformatArchitectureoverviewDeclarepermissionsDesigntheuserinterfaceDebuggingextensionsSamplesIndepth:coreconceptsMessagepassingContentscriptsManageeventswithbackgroundscriptsMatchpatternsCross-originisolationIndepth:securityCross-originXMLHttpRequestUsingevalinChromeextensionsIndepth:moretopicsOverridingChromesettingsExtendingDevToolsOAuth2:AuthenticateuserswithGoogleOverridingChromepagesRichnotificationsAPIModernizeyourextensionsMigratetoevent-drivenbackgroundscriptsBestpracticesProtectuserprivacyDeclarepermissionsandwarnusersStaysecureAccessibility(a11y)LocalizationmessageformatsGiveusersoptionsDistributingyourextensionsChromeWebStoreAlternativeextensiondistributionoptionsInstallingextensionsonLinuxTutorial:GoogleanalyticsDocumentationExtensionsAPIReferenceExtensionsWelcomeWhat'snewinChromeextensionsGettingstartedIntroducingManifestV3WelcometoManifestV3ExtensionsplatformvisionOverviewofManifestV3MigratingtoManifestV3ManifestV3migrationchecklistManifestV2supporttimelineOverviewWhatareextensions?Whatarethemes?FrequentlyaskedquestionsExtensionsqualityguidelinesFAQAPIReferenceSamplesDevelopextensionsandthemesExtensiondevelopmentoverviewManifestfileformatArchitectureoverviewDeclarepermissionsDesigntheuserinterfaceDebuggingextensionsSamplesIndepth:coreconceptsMessagepassingContentscriptsManageeventswithserviceworkersMatchpatternsUsingpromisesCross-originisolationIndepth:securityCross-originXMLHttpRequestUsingevalinChromeextensionsIndepth:moretopicsOverridingChromesettingsExtendingDevToolsOAuth2:AuthenticateuserswithGoogleOverridingChromepagesRichnotificationsAPIModernizeyourextensionsMigratingfrombackgroundpagestoserviceworkersBestpracticesProtectuserprivacyDeclarepermissionsandwarnusersStaysecureAccessibility(a11y)LocalizationmessageformatsGiveusersoptionsDistributingyourextensionsExtensionhostingAlternativeextensioninstallationmethodsInstallingextensionsonLinuxTutorial:GoogleanalyticsManifestV2AboutManifestV2GettingstartedOverviewWhatareextensions?Whatarethemes?FrequentlyaskedquestionsDevelopextensionsandthemesExtensiondevelopmentoverviewManifestfileformatArchitectureoverviewDeclarepermissionsDesigntheuserinterfaceDebuggingextensionsSamplesIndepth:coreconceptsMessagepassingContentscriptsManageeventswithbackgroundscriptsMatchpatternsCross-originisolationIndepth:securityCross-originXMLHttpRequestUsingevalinChromeextensionsIndepth:moretopicsOverridingChromesettingsExtendingDevToolsOAuth2:AuthenticateuserswithGoogleOverridingChromepagesRichnotificationsAPIModernizeyourextensionsMigratetoevent-drivenbackgroundscriptsBestpracticesProtectuserprivacyDeclarepermissionsandwarnusersStaysecureAccessibility(a11y)LocalizationmessageformatsGiveusersoptionsDistributingyourextensionsChromeWebStoreAlternativeextensiondistributionoptionsInstallingextensionsonLinuxTutorial:Googleanalyticschrome.cookiesDescriptionUsethechrome.cookiesAPItoqueryandmodifycookies,andtobenotifiedwhentheychange.PermissionscookiesTableofcontentsManifestExamplesTypesCookieCookieDetailsCookieStoreOnChangedCauseSameSiteStatusMethodsgetgetAllgetAllCookieStoresremovesetEventsonChanged#ManifestTousethecookiesAPI,youmustdeclarethe"cookies"permissioninyourmanifest,alongwithhostpermissionsforanyhostswhosecookiesyouwanttoaccess.Forexample:{"name":"Myextension",..."host_permissions":["*://*.google.com"],"permissions":["cookies"],...}#ExamplesYoucanfindasimpleexampleofusingthecookiesAPIintheexamples/api/cookiesdirectory.Forotherexamplesandforhelpinviewingthesourcecode,seeSamples.SummaryTypesCookieCookieDetailsCookieStoreOnChangedCauseSameSiteStatusMethodsgetgetAllgetAllCookieStoresremovesetEventsonChangedTypesCookieRepresentsinformationaboutanHTTPcookie.PropertiesdomainstringThedomainofthecookie(e.g."www.google.com","example.com").expirationDatenumber optionalTheexpirationdateofthecookieasthenumberofsecondssincetheUNIXepoch.Notprovidedforsessioncookies.hostOnlybooleanTrueifthecookieisahost-onlycookie(i.e.arequest'shostmustexactlymatchthedomainofthecookie).httpOnlybooleanTrueifthecookieismarkedasHttpOnly(i.e.thecookieisinaccessibletoclient-sidescripts).namestringThenameofthecookie.pathstringThepathofthecookie.sameSiteSameSiteStatusChrome51+Thecookie'ssame-sitestatus(i.e.whetherthecookieissentwithcross-siterequests).securebooleanTrueifthecookieismarkedasSecure(i.e.itsscopeislimitedtosecurechannels,typicallyHTTPS).sessionbooleanTrueifthecookieisasessioncookie,asopposedtoapersistentcookiewithanexpirationdate.storeIdstringTheIDofthecookiestorecontainingthiscookie,asprovidedingetAllCookieStores().valuestringThevalueofthecookie.CookieDetailsChrome88+Detailstoidentifythecookie.PropertiesnamestringThenameofthecookietoaccess.storeIdstring optionalTheIDofthecookiestoreinwhichtolookforthecookie.Bydefault,thecurrentexecutioncontext'scookiestorewillbeused.urlstringTheURLwithwhichthecookietoaccessisassociated.ThisargumentmaybeafullURL,inwhichcaseanydatafollowingtheURLpath(e.g.thequerystring)issimplyignored.IfhostpermissionsforthisURLarenotspecifiedinthemanifestfile,theAPIcallwillfail.CookieStoreRepresentsacookiestoreinthebrowser.Anincognitomodewindow,forinstance,usesaseparatecookiestorefromanon-incognitowindow.PropertiesidstringTheuniqueidentifierforthecookiestore.tabIdsnumber[]Identifiersofallthebrowsertabsthatsharethiscookiestore.OnChangedCauseChrome44+Theunderlyingreasonbehindthecookie'schange.Ifacookiewasinserted,orremovedviaanexplicitcallto"chrome.cookies.remove","cause"willbe"explicit".Ifacookiewasautomaticallyremovedduetoexpiry,"cause"willbe"expired".Ifacookiewasremovedduetobeingoverwrittenwithanalready-expiredexpirationdate,"cause"willbesetto"expired_overwrite".Ifacookiewasautomaticallyremovedduetogarbagecollection,"cause"willbe"evicted".Ifacookiewasautomaticallyremovedduetoa"set"callthatoverwroteit,"cause"willbe"overwrite".Planyourresponseaccordingly.Type"evicted","expired","explicit","expired_overwrite",or"overwrite"SameSiteStatusChrome51+Acookie's'SameSite'state(https://tools.ietf.org/html/draft-west-first-party-cookies).'no_restriction'correspondstoacookiesetwith'SameSite=None','lax'to'SameSite=Lax',and'strict'to'SameSite=Strict'.'unspecified'correspondstoacookiesetwithouttheSameSiteattribute.Type"no_restriction","lax","strict",or"unspecified"Methodsgetchrome.cookies.get(  details:CookieDetails,  callback?:function,)PromiseRetrievesinformationaboutasinglecookie.IfmorethanonecookieofthesamenameexistsforthegivenURL,theonewiththelongestpathwillbereturned.Forcookieswiththesamepathlength,thecookiewiththeearliestcreationtimewillbereturned.ParametersdetailsCookieDetailscallbackfunction optionalThecallbackparameterlookslike:(cookie?:Cookie)=>voidcookieCookie optionalContainsdetailsaboutthecookie.Thisparameterisnullifnosuchcookiewasfound.ReturnsPromisePendingThisonlyreturnsaPromisewhenthecallbackparameterisnotspecified,andwithMV3+.ThetypeinsidethePromiseisthesameasthe1stargumenttocallback.getAllchrome.cookies.getAll(  details:object,  callback?:function,)PromiseRetrievesallcookiesfromasinglecookiestorethatmatchthegiveninformation.Thecookiesreturnedwillbesorted,withthosewiththelongestpathfirst.Ifmultiplecookieshavethesamepathlength,thosewiththeearliestcreationtimewillbefirst.ParametersdetailsobjectInformationtofilterthecookiesbeingretrieved.domainstring optionalRestrictstheretrievedcookiestothosewhosedomainsmatchoraresubdomainsofthisone.namestring optionalFiltersthecookiesbyname.pathstring optionalRestrictstheretrievedcookiestothosewhosepathexactlymatchesthisstring.secureboolean optionalFiltersthecookiesbytheirSecureproperty.sessionboolean optionalFiltersoutsessionvs.persistentcookies.storeIdstring optionalThecookiestoretoretrievecookiesfrom.Ifomitted,thecurrentexecutioncontext'scookiestorewillbeused.urlstring optionalRestrictstheretrievedcookiestothosethatwouldmatchthegivenURL.callbackfunction optionalThecallbackparameterlookslike:(cookies:Cookie[])=>voidcookiesCookie[]Alltheexisting,unexpiredcookiesthatmatchthegivencookieinfo.ReturnsPromisePendingThisonlyreturnsaPromisewhenthecallbackparameterisnotspecified,andwithMV3+.ThetypeinsidethePromiseisthesameasthe1stargumenttocallback.getAllCookieStoreschrome.cookies.getAllCookieStores(  callback?:function,)PromiseListsallexistingcookiestores.Parameterscallbackfunction optionalThecallbackparameterlookslike:(cookieStores:CookieStore[])=>voidcookieStoresCookieStore[]Alltheexistingcookiestores.ReturnsPromisePendingThisonlyreturnsaPromisewhenthecallbackparameterisnotspecified,andwithMV3+.ThetypeinsidethePromiseisthesameasthe1stargumenttocallback.removechrome.cookies.remove(  details:CookieDetails,  callback?:function,)PromiseDeletesacookiebyname.ParametersdetailsCookieDetailscallbackfunction optionalThecallbackparameterlookslike:(details?:object)=>voiddetailsobject optionalContainsdetailsaboutthecookiethat'sbeenremoved.Ifremovalfailedforanyreason,thiswillbe"null",andruntime.lastErrorwillbeset.namestringThenameofthecookiethat'sbeenremoved.storeIdstringTheIDofthecookiestorefromwhichthecookiewasremoved.urlstringTheURLassociatedwiththecookiethat'sbeenremoved.ReturnsPromisePendingThisonlyreturnsaPromisewhenthecallbackparameterisnotspecified,andwithMV3+.ThetypeinsidethePromiseisthesameasthe1stargumenttocallback.setchrome.cookies.set(  details:object,  callback?:function,)PromiseSetsacookiewiththegivencookiedata;mayoverwriteequivalentcookiesiftheyexist.ParametersdetailsobjectDetailsaboutthecookiebeingset.domainstring optionalThedomainofthecookie.Ifomitted,thecookiebecomesahost-onlycookie.expirationDatenumber optionalTheexpirationdateofthecookieasthenumberofsecondssincetheUNIXepoch.Ifomitted,thecookiebecomesasessioncookie.httpOnlyboolean optionalWhetherthecookieshouldbemarkedasHttpOnly.Defaultstofalse.namestring optionalThenameofthecookie.Emptybydefaultifomitted.pathstring optionalThepathofthecookie.Defaultstothepathportionoftheurlparameter.sameSiteSameSiteStatus optionalChrome51+Thecookie'ssame-sitestatus.Defaultsto"unspecified",i.e.,ifomitted,thecookieissetwithoutspecifyingaSameSiteattribute.secureboolean optionalWhetherthecookieshouldbemarkedasSecure.Defaultstofalse.storeIdstring optionalTheIDofthecookiestoreinwhichtosetthecookie.Bydefault,thecookieissetinthecurrentexecutioncontext'scookiestore.urlstringTherequest-URItoassociatewiththesettingofthecookie.Thisvaluecanaffectthedefaultdomainandpathvaluesofthecreatedcookie.IfhostpermissionsforthisURLarenotspecifiedinthemanifestfile,theAPIcallwillfail.valuestring optionalThevalueofthecookie.Emptybydefaultifomitted.callbackfunction optionalThecallbackparameterlookslike:(cookie?:Cookie)=>voidcookieCookie optionalContainsdetailsaboutthecookiethat'sbeenset.Ifsettingfailedforanyreason,thiswillbe"null",andruntime.lastErrorwillbeset.ReturnsPromisePendingThisonlyreturnsaPromisewhenthecallbackparameterisnotspecified,andwithMV3+.ThetypeinsidethePromiseisthesameasthe1stargumenttocallback.EventsonChangedchrome.cookies.onChanged.addListener(  callback:function,)Firedwhenacookieissetorremoved.Asaspecialcase,notethatupdatingacookie'spropertiesisimplementedasatwostepprocess:thecookietobeupdatedisfirstremovedentirely,generatinganotificationwith"cause"of"overwrite".Afterwards,anewcookieiswrittenwiththeupdatedvalues,generatingasecondnotificationwith"cause""explicit".ParameterscallbackfunctionThecallbackparameterlookslike:(changeInfo:object)=>voidchangeInfoobjectcauseOnChangedCauseTheunderlyingreasonbehindthecookie'schange.cookieCookieInformationaboutthecookiethatwassetorremoved.removedbooleanTrueifacookiewasremoved.TableofcontentsManifestExamplesTypesCookieCookieDetailsCookieStoreOnChangedCauseSameSiteStatusMethodsgetgetAllgetAllCookieStoresremovesetEventsonChangedWeservecookiesonthissitetoanalyzetraffic,rememberyourpreferences,andoptimizeyourexperience.MoredetailsOk,Gotit.



請為這篇文章評分?