Delete all Google cookies from a Greasemonkey script [duplicate] - javascript

This question already has answers here:
Can a userscript delete cookies from a given domain?
(2 answers)
Closed 5 years ago.
I tried a lot of different scripts, but none worked. How do I delete the cookies created by Google, or all cookies of a site?

Best not to use Greasemonkey for this. It will be cumbersome, might miss cookies set long after the page loads, and can only delete Google cookies while you are actually browsing Google.
Plus, you have to set the script's // #include statements to catch all of Google's current and future domains (google.com, accounts.google.com, mail.google.com, google-analytics.com, etc.). And if Google serves "Secure cookies" those cannot be touched either.
Best to use a tool built for smartly deleting cookies. I recommend Selective Cookie Delete.
Also, Google, and other sites, track you with far more and worse than cookies. It's a good idea to run CCleaner at least once a week.
BUT, if you still want to do this with Greasemonkey, here is the code that will delete many cookies for the domain that the script is running on:
WARNING: JavaScript and Greasemonkey cannot even see all the cookies on a page, nor can "secure" (server only) cookies be deleted.).
//--- Loop through cookies and delete them.
var cookieList = document.cookie.split (/;\s*/);
for (var J = cookieList.length - 1; J >= 0; --J) {
var cookieName = cookieList[J].replace (/\s*(\w+)=.+$/, "$1");
eraseCookie (cookieName);
}
Where eraseCookie() is:
(Note that this eraseCookie gets many more cookies by attempting all possible paths and the most likely sub-domains.)
function eraseCookie (cookieName) {
//--- ONE-TIME INITS:
//--- Set possible domains. Omits some rare edge cases.?.
var domain = document.domain;
var domain2 = document.domain.replace (/^www\./, "");
var domain3 = document.domain.replace (/^(\w+\.)+?(\w+\.\w+)$/, "$2");;
//--- Get possible paths for the current page:
var pathNodes = location.pathname.split ("/").map ( function (pathWord) {
return '/' + pathWord;
} );
var cookPaths = [""].concat (pathNodes.map ( function (pathNode) {
if (this.pathStr) {
this.pathStr += pathNode;
}
else {
this.pathStr = "; path=";
return (this.pathStr + pathNode);
}
return (this.pathStr);
} ) );
( eraseCookie = function (cookieName) {
//--- For each path, attempt to delete the cookie.
cookPaths.forEach ( function (pathStr) {
//--- To delete a cookie, set its expiration date to a past value.
var diagStr = cookieName + "=" + pathStr + "; expires=Thu, 01-Jan-1970 00:00:01 GMT;";
document.cookie = diagStr;
document.cookie = cookieName + "=" + pathStr + "; domain=" + domain + "; expires=Thu, 01-Jan-1970 00:00:01 GMT;";
document.cookie = cookieName + "=" + pathStr + "; domain=" + domain2 + "; expires=Thu, 01-Jan-1970 00:00:01 GMT;";
document.cookie = cookieName + "=" + pathStr + "; domain=" + domain3 + "; expires=Thu, 01-Jan-1970 00:00:01 GMT;";
} );
} ) (cookieName);
}

Related

How to set/change a cookie via php?

I use this code to change the cookie value in php, or set it if it does not exist yet:
setcookie('maintenance_site_contact_failed', '1', time()+3600, '/', 'entwicklung');
And I am using this code to set a cookie in javascript:
setCookie('maintenance_site_contact_failed', '0', 1);
function setCookie(name,value,days)
{
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
But as you can see in the following screenshot the cookie is created a second time instead of changing the existing one, but the duplicate has a dot in the domain name.
Why is there a dot and why is the cookie created twice?
What I try:
If a user sends my form and the validation fails on serverside, then
I am setting the cookie maintenance_site_contact_failed in the PHP script to 1 so that the client knows that an error happened.
The javascript then checks the value of the cookie, and if the cookie value is 1 then it shows an error and resets the cookie value to 0.
Looks like your cookie is only available to different subdomains because in the php equivalent you're assigning a value for the domain parameter.
Try to do the same inside the Javascript setCookie function:
document.cookie = name + "=" + (value || "") + expires + "; path=/; domain=entwicklung";

HTML cookie don't create

I'm having problem of creating a cookie, I have this code:
window.onload = function() {
var value = readCookie('username');
if(value == null){
alert("null");
document.cookie = "username=Bob; expires=Thu, 18 Dec 2016 12:00:00 UTC";
}
else
alert(value);
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
When the page load I check if the cookie exists, if it doesn't, I pop up an alert saying that is null, then I create the cookie, but every time I load the page it always says its null. Now the problem is in the creation of the cookie or in the readCookie function. I can't find why this doesn't work.
UPDATE
So in google Chrome it won't work, but in internet explorer works perfectly, someone knows why? I would like to work in all browsers.
When I create an HTML page using that code and run it in a normal test environment, the problem you describe does not occur. It alerts null on the first load and Bob on the second load.
If, however, I tell the browser to load the page from file:///Users/david/tmp/jgfklgjkl/index.html instead of my test server at http://localhost:7007/, then it always alerts null.
Cookies are associated with a hostname, and you have to load the page over HTTP in order to use them.
You are presumably loading them from a local file, and that is where the problem lies, not with your code.
Try this function for reading cookie. I've been using it for quite too long and it's working fine so far
function getCookieValue(cookie_name) {
var cookie, cookies = document.cookie.split(';');
for (var i = cookies.length - 1; i >= 0; --i) {
cookie = cookies[i].split('=');
if (cookie[0].trim() === cookie_name)
return cookie[1];
}
return "";
}
Also if you are interested you could use this function for adding cookies for 10 years.
function addCookie(name, value) {
var expire = new Date();
expire.setFullYear(expire.getFullYear() + 10);
d.cookie = name + "=" + value + "; expires=" + expire.toGMTString() + "; path=/";
}

Read a javascript cookie from php

I have a task which is compare up to five products from the product list. For that
I have followed following steps:
step 1:
set onclick event when we click add to compare button of each product. In this event I have set cookie by javascript using this code.
// cookie is set by array because of we have to store 1 to 5 products
var comparearray = [productid];
document.cookie = "compareitem" + "=" + comparearray;
It is successfully set the cookie value which holds product id of those are selected to compare.
Step 2: In my PHP file I have tried to retrieve this cookie value.BY,
$cookie_val = $_COOKIE['compareitem '];
But it is not worked. I don't know this kind of concept is worth. If know, give me the instructions how to solve my problem. Thanks in advance.
Since it may be problem with path I suggest to set/get cookie with extended way:
document.cookie="foo=bar; path=/;"
or you can use this function:
function setCookie (name, value, expires, path, domain, secure) {
document.cookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
Since some people are writing that you can't use cookies set with JS in PHP, I'm going to answer that question now.
Please try to use a cookieSet and cookieGet function, you can use the one from this answer:
How do I create and read a value from cookie?
var createCookie = function(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
else {
expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
}
Note the last parameter which is written into the cookie.. the path-param is set to '/', so the root location! Use a php script in the root location, so that both use the root location and not some other location.
Next please try to stringify the array with JSON.
var cookiedata = JSON.stringify(comparearray);
Then you should be able to get the cookie with PHP and parse the JSON to get the array back.

How to delete __utma cookie from chrome?

I am trying to delete the Google analytic cookies from website. I am using this code to delete the cookies, But the Google analytic cookies is not remove fro the Google Chrome. How to delete the Google analytic cookies from Google Chrome ?.
function clearCookie(name, domain, path){
try {
function Get_Cookie( check_name ) {
// first we'll split this cookie up into name/value pairs
// note: document.cookie only returns name=value, not the other components
var a_all_cookies = document.cookie.split(';'),
a_temp_cookie = '',
cookie_name = '',
cookie_value = '',
b_cookie_found = false;
for ( i = 0; i < a_all_cookies.length; i++ ) {
// now we'll split apart each name=value pair
a_temp_cookie = a_all_cookies[i].split( '=' );
// and trim left/right whitespace while we're at it
cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
// if the extracted name matches passed check_name
if ( cookie_name == check_name ) {
b_cookie_found = true;
// we need to handle case where cookie has no value but exists (no = sign, that is):
if ( a_temp_cookie.length > 1 ) {
cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
}
// note that in cases where cookie is initialized but no value, null is returned
return cookie_value;
break;
}
a_temp_cookie = null;
cookie_name = '';
}
if ( !b_cookie_found ) {
return null;
}
}
if (Get_Cookie(name)) {
var domain = domain || document.domain;
var path = path || "/";
document.cookie = name + "=; expires=" + new Date + "; domain=" + domain + "; path=" + path;
}
}
catch(err) {}
};
clearCookie('__utmz','.domain','/');
clearCookie('__utmb','.domain','/');
clearCookie('__utmc','.domain','/');
clearCookie('__utma','.domain','/');
Following statement -
document.cookie = name + "=; expires=" + new Date + "; domain=" + domain + "; path=" + path;
it will make the cookie as session cookie. For deleting cookie, you should set past time. check this -
Delete cookie by name?
However, there would be one problem. Since these are google analytics cookies, so if google analytics is still enabled and working, it will set the cookies again. If you do not want to set the cookies again by google analytics, then you need to disable google analytics firstly

Cookie is being set.But document.cookie is null

I have set a cookie with document.cookie. The content settings in Google Chrome is showing the cookie. However, document.cookie is displaying as a blank string when printed. Why is this happening?
Here is my code
function setCookie(name,value,lifeTime,path,domain,secure){//lifetime in hours
{
var c_ = name +'='+escape(value)+'; ';
var life = new Date();
lifeTime<1&&lifeTime>0?life.setMinutes(life.getMinutes()+lifeTime*60):life.setHours(life.getHours()+lifeTime);
life = life.toUTCString();
c_+='expires='+life+"; ";
c_+= 'domain='+domain+'; ';
c_ += 'secure=secure; ';//secure
c_ += 'path='+path;
document.cookie = c_;
alert(document.cookie);
/*Just splitted the code instead of c = 'name='+value+'expires ='+life etc*/
}
A possible problem with this function is, it always sets a secure cookie. So, if you've requested/opened the page with HTTP not HTTPS protocol, the secure cookie won't be exposed.

Categories