Issue with Creating JavaScript Cookies

I am currently working on a JavaScript project to create a cookie. My main goal is to save the tab name as a cookie when the user clicks the extension icon while watching a YouTube video. This way, I can access the cookie from my Java program.

Although I'm using Chrome, I've encountered an issue where I can't see the cookie in the list even though the alert successfully displays. I'm seeking help to identify any potential issues with my code.

If anyone has alternative suggestions for transferring the tab name to my Java program, I would greatly appreciate hearing your ideas.

Thank you all for your assistance! Below is the code I have been working on:

chrome.browserAction.onClicked.addListener(run);

function run()
{
    var cookieName, cookieValue;

    cookieName = "Tab";
    chrome.tabs.getSelected(null, function(tab)
    {
       cookieValue = tab.title;
       createCookie(cookieName, cookieValue);
   });
}

function createCookie(name, value)
{
    var expires = new Date().getTime() + (1000 * 3600);
    var domain = ";domain=.youtube.com";
    document.cookie = name + "=" + value + ";expires=" + expires + domain +         ";path=/";
    alert(name + " = " + value + ". Date = " + expires);
}

UPDATE: I have made adjustments to my code utilizing Google's chrome APIs and achieved success!

Answer №1

If you're facing the same issue, I found a solution using the Google Chrome API for managing cookies.

Here is my updated code:

chrome.browserAction.onClicked.addListener(run);

function run()
{
    var cookieName, cookieValue, cookieURL;
    cookieName = "Tab";
    chrome.tabs.getSelected(null, function(tab)
    {
        cookieValue = tab.title;
        cookieURL = tab.url;
        createCookie(cookieName, cookieValue, cookieURL);
    });
}

function createCookie(cookieName, cookieValue, cookieURL)
{
    chrome.cookies.set({name: cookieName, value: cookieValue, domain: ".youtube.com", url: cookieURL});
}

Note: Make sure to add permissions for tabs, cookies, and the website domain in your manifest file. Additionally, keep in mind that I did not specify an expiration date for the cookie, so it will expire when the session ends.

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

Guide on dynamically updating a div in PHP with a mix of text output and images at regular intervals

My current project involves creating a browser-based card game primarily using PHP, with potentially incorporating other languages to help me enhance and test my PHP skills. However, I've encountered difficulties while attempting to implement various ...

The Express middleware type cannot be assigned as expected

I'm encountering an error where my first middleware is being red underlined. I can't figure out why it's only happening to the first one. https://i.sstatic.net/PahAz.png https://i.sstatic.net/GgxBy.png Can anyone provide some guidance on ...

`Generating dynamically styled elements`

I am a newcomer so please forgive me if my question is too basic. I have managed to create a code (with some help from the forum) where clicking on an image reveals another one, and so on as you continue to click. The issue I'm facing is that I can&ap ...

Showing the ng-controller contents post executing AJAX request using the "as" method

My goal is to display the contents of ng-repeat after making an AJAX call using $http. <table ng-controller="TableController as tc"> <tr> <th>Date</th> <!-- other headers are here --> ...

Conceal two DIV elements if one of them is empty

My slideshow has thumbnails connected to each slide. There are 10 DIVs representing the slides like this... <div class="SSSlide clip_frame grpelem slide" id="u751"><!-- image --> <?php while ($row = mysql_fetch_array($query ...

Establishing multiple SSH2 connections in Node.js

I need to establish a connection with a remote machine and execute shell commands on it. While I am able to upload a file and run a command successfully, I can only do so once. My goal is to be able to perform these actions multiple times. Below is the Ja ...

Find the current elapsed time using Wavesurfer in real time

I am currently utilizing the waveSurfer library created by katspaugh for playing audio files. In order to display 'elapsed time / total time', I have written code in the following manner: waveSurfer.on('play', function() { $scope.g ...

Modifying an item in a list using React and Redux

I am facing an issue with my table that consists of 50 rows, each representing an item from a list. When I click on a checkbox within a row, I want to mark that specific item as selected. However, the problem is that clicking on any checkbox causes all 50 ...

Vue component does not display FabricJS image

Currently, I am facing an issue where I want to manipulate images on a canvas using FabricJS inside a VueJS app. In the view component, there is a prop called background which I pass in and then use fabric.Image.fromURL() to load it onto the canvas. Howeve ...

What is the reason behind combining all states into a single location in Redux, even if they are not globally accessible?

As a newcomer to React and Redux, I have found them to be quite enjoyable when used together in a small sandbox application. However, as I consider larger applications, I can't help but question why Redux stores the entire application state in a singl ...

Utilize different JSON files to load numerous JSON objects

I'm currently working on developing a quiz website where the quiz content is stored in JSON files. While the functionality works fine, I'm looking to enhance it by adding a unique image to each question stored in the JSON file. To achieve this, I ...

Evaluating string combinations in JavaScript using valid comparisons

After choosing values on the screen, two variables store their value. var uval = '100'; var eval = '5'; There are 2 combinations with values: let combination1= 'u:100;e:1,4,5,10' let combination2 = 'u:1000;e:120,400,500, ...

Deactivate interactive functions on the mat-slider while preserving the CSS styling

I'm attempting to create a mat-slider with a thumb label that remains visible at all times. Below is my mat-slider component: <mat-slider class="example-margin" [disabled]="false" [thumbLabel]="true" [tickInterval]="tickInterval" ...

What reasons could be preventing the state from updating correctly in Vuex?

Currently, I am working on a project where I am utilizing vuex along with axios to retrieve data from the backend. The issue arises when I try to access the state - even though the updated state is displayed successfully when I console-log it, there seems ...

Issues encountered when updating values in MaterialUI's TextField using Formik

Within my React functional component, I utilize Formik for form management and MaterialUI V5.10 for styling. The form includes TextField elements and a Canvas element. I am encountering two issues... Despite setting initial values in Formik, the TextFiel ...

What is causing this code to run immediately upon the addition of Promise logic?

The coding scenario written below was supposed to have two 4-second delays. However, when the code is run, it executes immediately. It seems that there might be a lack of understanding on my part regarding some basic concept, or perhaps there's a hidd ...

JavaScript running out of memory during yarn build process

While working with yarn to build my project, I encountered a problem. The issue at hand is that the memory heap exceeds its allocated size, causing the build process to fail. FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - Java ...

The VueJS Chosen directive fails to refresh when new options are selected

Struggling to populate my jQuery Chosen dropdown field with AJAX data using VueJS. Unfortunately, when trying to update the values, the Chosen dropdown does not reflect the changes. I've experimented with different approaches, including manually trig ...

Modify session variable upon link click

Here is an extension of the question posed on Stack Overflow regarding creating a new page for different PHP ORDER BY statements: Create a new page for different php ORDER BY statement? The task at hand requires changing a session variable and refreshing ...

Issue with JQuery's parentsUntil method when using an element as a variable not producing the desired results

I'm having trouble with a specific coding issue that can be best illustrated through examples: For example, this code snippet works as expected: $(startContainer).parents().each(function(index, parentNode) { if (parentNode.isSameNode(commonConta ...