Guide to snagging a cookie with a Chrome extension

I'm attempting to retrieve a cookie from a specific website using a Chrome extension, but as someone who has never created one before, I'm facing some challenges. Despite reading numerous tutorials, I haven't been successful in capturing the cookie.

My approach involves using a content script, but I keep encountering an error message each time I try.

Here is the code for my manifest, content script, and the error:

manifest.json

...
    
"manifest_version": 3,

"permissions": [
    "cookies"
 ],

"content_scripts": [{
    "matches": ["*://google.com/"],
    "js": ["content.js"]
}]

content.js


var ID;

function getCookies(domain, name) {
    chrome.cookies.get({"url": domain, "name": name}, function(cookie) {
        ID = cookie.value;
        showId();
    });
}

function showId() {
    alert(ID);
}

getCookies("https://google.com/", "SSID");  

Error:

Uncaught TypeError: Cannot read property 'get' of undefined

Any suggestions on how to resolve this issue would be greatly appreciated.

Answer №1

The information provided on using the chrome.cookies.get method mentions:

[...] The API call will fail if host permissions for the URL are not specified in the manifest file.

To access cookies from specific domains, you must request permissions by adding them to the permissions section of your manifest.json:

"permissions": [
    "cookies",
    "*://google.com/"
],

Additionally, there is no need to inject a content script to retrieve cookies. This can be done directly in your background page.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a duplicate or copy of an element in jQuery using

My dilemma involves a div that consists of text/images and a custom jQuery plugin designed for those images. The plugin simply executes a fadeIn/fadeOut effect every 3 seconds using setInterval. This particular div acts as a "cache" div. When a user wants ...

Can you explain the significance of $f in jQuery scripts?

There are multiple jQuery-related scripts that utilize the $f function, however, I have been unable to locate a definitive explanation of its purpose. Here is an example: var iframe = document.getElementById(iframe_id); this.player = global.$f(iframe); ...

Extract information from a webpage using JavaScript through the R programming language

Having just started learning about web scraping in R, I've encountered an issue with websites that utilize javascript. My attempt to scrape data from a specific webpage has been unsuccessful due to the presence of javascript links blocking access to t ...

The CKEditor is having trouble properly opening code snippets

I have been using ckeditor 4.4 along with the code snippet plugin. Initially, when I create a document with a rich code snippet and save it, everything works perfectly fine. The source code for what is generated looks like this: <pre><code>&am ...

In order to display the user's identification when a button is clicked using Vue3, I have implemented a function called printName

<td class="one-third column" v-for="user in users" :key="user._id"> <div> <v-btn @click="printIdOrName(user)" height="50" size="large" co ...

A Webpage that is permanently open, with no option to close it. The only option is to watch the video in

Currently, I am designing web pages for artists to showcase their work in an upcoming exhibition. The pages are ready, but now the artists are requesting that each piece be accompanied by a laptop displaying a single webpage with a video providing informat ...

Exploring the dynamic shift with jQuery .find

Currently, I am in the process of learning jQuery and have come across a simple issue. My goal is to change the text within a div when a link is clicked. The div in question is a duplicate of another div on the page, and my intention is to remove the copy ...

Guide on implementing a bootstrap loading spinner during the process of retrieving data from an external api

Is there a way to use the bootstrap load spinner to mask the delayed information retrieval from a url and enhance the website's interactivity? ...

Personalize the "set up notification" PWA on React

Is it possible to customize this default design, including the picture, title, description, and background? I made changes in manifest.json, but nothing seems to have happened. Here is a picture of the random install prompt that I would like to customize ...

Obtaining Data from an XML Node Using AJAX

Trying to extract statistics from my XML based on a specific name request, but encountering issues with my JavaScript functionality. XML data: <player> <forward><name>Joe</name><stats>45</stats></forward> <f ...

using selenium to interact with elements within the window object

I'm a newcomer to selenium and javascript and looking to incorporate the VisualEvent javascript into pages opened in a selenium-controlled browser. My goal is to access its variables from selenium in java. I've successfully completed the first ph ...

Passing information between VueJs3 components

I have been coding a VueJs3 Project that includes a login system, and it's working perfectly. Now, I want to display the user's logged-in account on the homepage within a div. The message should be: "You are logged in as:" followed by the userna ...

Utilizing a variable as a condition in the results

Currently, I am utilizing this Moongose query to work with geo-spatial data: Locations.find({ loc: { $geoWithin: { $centerSphere: [[lng, lat], radius / 6378.1], }, } }, cb); While it functions effectively, I am curious ...

Minimize white spaces when validating input fields in a form using Javascript

I'm currently facing a challenge with JavaScript, specifically regarding achieving five tasks without using jQuery. Despite trying various RegExp codes, none have successfully worked for me so far. Let's begin with the first task (number 1): El ...

The issue is that only the first checkbox within ngFor is being toggled on and off each time a different checkbox is clicked

When I have checkboxes inside a ngFor loop and click on any one of them, only the first one gets checked and unchecked, updating only the status of the first checkbox. Here is the HTML template: <div *ngFor="let num of count" class="m-b-20"> & ...

The ng-view directive within AngularJS appears to be malfunctioning

I am facing an issue with my simple Angular app. I have two links that are supposed to change the URL and display the correct view within the same single page application. However, when I include the controllers' module in the main module, it stops wo ...

Displaying Various Photos within bootstrap-table using Ajax Response

Upon receiving a response from an AJAX call, my task is to create a bootstrap-table with the data. The response contains an array with information about images, such as their paths and names. However, when trying to populate the bootstrap-table with this d ...

Adjust the sizing of all fonts following the switch in font styles

Seeking help for adjusting font-size across a responsive web page design using different font families. font-family:Cambria, "Hoefler Text", "Liberation Serif", Times, "Times New Roman", serif; To achieve responsiveness, various media queries were applie ...

Creating a PDF file from a series of images

I've implemented a function using the jsPDF library to generate a PDF from a list of images. The main task is to add these images to the PDF document. Here is the code snippet: const { allImgs } = useAppContext() const doc = new jsPDF(); const gener ...

Even though I included a key prop for each item, I am still encountering the error message stating that every child in a list should have a distinct "key" prop

I have been trying to retrieve data from a rest API and display it as text on my browser. However, I am encountering the following error: index.js:1 Warning: Each child in a list should have a unique "key" prop. Below is how I have set up the key prop. ...