Monday, June 3, 2024
27
rated 0 times [  31] [ 4]  / answers: 1 / hits: 16000  / 10 Years ago, sun, october 19, 2014, 12:00:00

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:



Console



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);
});
}

More From » google-chrome

 Answers
2

Issue



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.



Solution



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:



log



Cookie alert:



alert



Working example



You can download a working example of the extension HERE.



Documentation



You may find this documentation links helpful:




[#69082] Thursday, October 16, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ingridmikaylam

Total Points: 93
Total Questions: 81
Total Answers: 105

Location: Nicaragua
Member since Tue, Dec 8, 2020
4 Years ago
;