在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:p/chrome-extension-cookies-sample开源软件地址:https://github.com/p/chrome-extension-cookies-sample开源编程语言:JavaScript 75.9%开源软件介绍:Chrome Sample Cookie Use ExtensionThis extension demonstrates reading cookies from a website from a Chrome extension. One use case which seemingly requires reading cookies is performing XMLHttpRequest calls from one website to another, for example to present data from the second website to the first website. It appears that if such a request is initiated from an extension then despite the browser having cookies for the target site, these cookies are not passed with the XHR automatically. Setting these cookies on an XHR object manually requires manually obtaining said cookies, which is the process this sample extension covers. MechanismAs explained here a Chrome extension has 3 execution contexts: content, page and background. The extension code normally runs in either the page context or the content context, and cookies can only be read from the background context. As such the page context passes a custom event to the content context which sends a Chrome message to the background context; code in the background context listens for Chrome messages, reads cookies and replies to the messages with cookie values; code in the content context receives the reply from the background context and forwards the data via another custom event to the page context. It seems possible to move some or all of the code which runs in the page context to the content context, in which case half of the message passing will cease to be necessary. Step By StepLet's build the Chrome extension! BackendIn order to demonstrate the extension we need two Web sites on different domains. To this end there are two Python scripts in this repository:
Both servers can set a cookie named Install The ExtensionNext, install the extension in this repository via "Load unpacked extension" method. Page To Content: Custom EventThe cookie retrieval is triggered when the host page loads.
In the page context we create a custom event containing the action
we want our background code to perform (
Content script listens for this event and forwards it to the background context:
Page and content scripts both have access to the page's DOM including the window object and events, hence these scripts are able to communicate by passing custom events between each other. Content To Background: Chrome MessagingAs shown above, the content code uses chrome.runtime.sendMessage API to send an event to the background code:
The background code listens for this event and reads the cookies:
Reading CookiesWe need to send something back, hence let's read the cookies on the target site. This is easy:
The important point here is that cookie retrieval is asynchronous - ResourcesChrome's 3 layers (content, page and background): Passing a message from page context to content context:
Passing a message from content context to background context:
Specifics on reading cookies:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论