Reputation: 55
I am making a Chrome extension and i need to get PHPSESSID
from cookies.
When I type document.cookie
in console I get the cookies of the page but not PHPSESSID
but when i open EditThisCookie the PHPSESSID is shown:
How can I get the PHPSESSID value?
I tried
function getPHPSESSID() {
var url = tab.url;
chrome.cookies.get({"url": url, "name": 'PHPSESSID'}, function(cookie) {
alert(cookie.value);
});
}
Upvotes: 2
Views: 20378
Reputation: 69336
Your problem here is that you probably aren't using the correct permissions. To get cookies from a certain url/page you'll need to use the chrome.cookies
API. This API needs the permissions for "cookies"
and all the URLs you want to retrieve cookies from.
In your manifest.json
, request the permissions you need. If you want to retrieve cookies from all the sites, you can use the "<all_urls>"
permission. Or, if you want cookies only from that site you can use, for example, "http://luptaonline.com/*
. I also noticed that you are using the chrome.tabs
API, so the permissions in manifest.json
should look like this:
...
"permissions": [
"cookies",
"tabs",
"<all_urls>"
],
...
Now, in your background.js
, you can easily call the chrome.cookies.get()
method to retrieve the PHPSESSID
cookie, like this:
var myUrl = "http://luptaonline.com/player/"; // assuming that this is the url
chrome.cookies.get({url: myUrl, name: 'PHPSESSID'}, function(cookie) {
// do something with the cookie
console.log(cookie);
alert(cookie.name + ' found, value: ' + cookie.value);
});
Running the above code you'll get what you want. Here are some screenshot of the results:
Console log:
Cookie alert:
You can download a working example of the extension HERE.
You may find this documentation links helpful:
Upvotes: 8