Reputation: 723
I'm creating a Chrome extension which loads an externa web page in a WebView. This web page has a cookie set. My question is how can I access the cookie and use it in the rest of the extension for subsequent web service access?
Thanks in advance, Merijn
Upvotes: 0
Views: 916
Reputation: 1624
You can access to the cookies with the chrome api, do not forget to add the permission in your manifest.
The problem here is that you can access it only from the background script. If you wanted to access it from popup script or content script you have to use messaging api to ask and received the answer from the background script.
In your popup or content script you can call :
var message = {name: "getCookie", params: {...}}; //params needed for get method
var callback = function (response) {
//Do what you want with your cookie which is in response.cookie
}
chrome.runtime.sendMessage(message, callback);
So in your background script :
chrome.runtime.onMessage.addListener(
function(message, sender, sendResponse) {
if(message.name == "getCookie") { // message.name from above
chrome.cookies.get(message.params, function (cookie) {
sendResponse({cookie: cookie});
})
}
});
Upvotes: 1