How to set that google translate cookie never expires? - cookies

Im using the google translate code on my page and it works good, but if I look at the cookie it says that it only have expiration during the session!? So I want to set it so it does´t expire, so that it is the same language when the user comes back as he choosed the first time.
Im using this now.
SOLVED! OK so with this the user can select a language and the next time he visit the page it is translated to the language he picked before!
var ckDomain;
function googleTranslateElementInit() {
function getCookie(name)
{
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value != null) ? unescape(value[1]) : null;
}
var kakan;
var googkakan;
kakan=getCookie("googtrans22");
$$(document).on('change', '#google_translate_element', function (e) {
setTimeout(function(){
function getCookie(name)
{
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value != null) ? unescape(value[1]) : null;
}
googkakan=getCookie("googtrans");
document.cookie = "googtrans22="+googkakan+"; expires=Thu, 07-Mar-2047 20:22:40 GMT; path=/" + ckDomain;
document.cookie = "googtrans22="+googkakan+"; expires=Thu, 07-Mar-2047 20:22:40 GMT; path=/";
},1000);
});
for (var ckDomain = window.location.hostname.split("."); 2 < ckDomain.length;){
ckDomain.shift();
}
ckDomain = ";domain=" + ckDomain.join(".");
// domain cookie
document.cookie = "googtrans="+kakan+"; expires=Thu, 07-Mar-2047 20:22:40 GMT; path=/" + ckDomain;
// host-only cookie (with no domain name definition)
document.cookie = "googtrans="+kakan+"; expires=Thu, 07-Mar-2047 20:22:40 GMT; path=/";
new google.translate.TranslateElement({
pageLanguage: 'sv',
autoDisplay: false,
layout: google.translate.TranslateElement
}, 'google_translate_element');
}
(function() {
var googleTranslateScript = document.createElement('script');
googleTranslateScript.type = 'text/javascript';
googleTranslateScript.async = true;
googleTranslateScript.src = 'https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit';
( document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0] ).appendChild( googleTranslateScript );
})();

Apparently the library is forced to write over the cookie to make it expire at end of the session.
The good news is that before doing that it reads the existing cookie, so you can feed it before each initialization call.
To force the library to translate a Swedish page into English:
function googleTranslateElementInit() {
var ckDomain;
for (var ckDomain = window.location.hostname.split("."); 2 < ckDomain.length;){
ckDomain.shift();
}
ckDomain = ";domain=" + ckDomain.join(".");
// domain cookie
document.cookie = "googtrans=/sv/en; expires=Thu, 07-Mar-2047 20:22:40 GMT; path=/" + ckDomain;
// host-only cookie (with no domain name definition)
document.cookie = "googtrans=/sv/en; expires=Thu, 07-Mar-2047 20:22:40 GMT; path=/";
new google.translate.TranslateElement({
pageLanguage: 'sv',
autoDisplay: false,
layout: google.translate.TranslateElement
}, 'google_translate_element');
}

Related

Set cookie in Xamarin Android WebView

In Xamarin Android SDK 30, when trying to set webview cookie does not work.
string cookieString = string.Format("{0}={1}; path=/;domain={2}; secure; ", Strings.SessionCookie, encryptedCookie, "value");
if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
CookieManager.Instance.SetAcceptThirdPartyCookies(webView, true);
}
else
{
CookieManager.Instance.SetAcceptCookie(true); //this function doesn't work from lollipop(API21) and above
}
CookieManager.Instance.SetCookie(RootCookieDomain, cookieString);
webView.Settings.JavaScriptEnabled = true;
webView.Settings.DomStorageEnabled = true;
webView.LoadUrl(Link);
The above code works perfectly in SDK 29 but as soon as its switched to 30 it stops working and cookie is not being set.
I set the cookie with the code below. It works on API30.
Xaml:
<WebView x:Name="MyWebview" WidthRequest="400" HeightRequest="500"></WebView>
<Button Text="click to load" Clicked="Button_Clicked"></Button>
Code:
private void Button_Clicked(object sender, EventArgs e)
{
CookieContainer cookieContainer = new CookieContainer();
Uri uri = new Uri("https://dotnet.microsoft.com/apps/xamarin", UriKind.RelativeOrAbsolute);
Cookie cookie = new Cookie
{
Name = "XamarinCookie",
Expires = DateTime.Now.AddDays(1),
Value = "My cookie",
Domain = uri.Host,
Path = "/"
};
cookieContainer.Add(uri, cookie);
MyWebview.Cookies = cookieContainer;
MyWebview.Source = new UrlWebViewSource { Url = uri.ToString() };
}

universal_cookie__WEBPACK_IMPORTED_MODULE_3__.default.remove is not a function

whenever am i trying to remove cookie its show me this error.
TypeError: universal_cookie__WEBPACK_IMPORTED_MODULE_3__.default.remove is not a function
this is my code
Logout = () =>{
var user = Cookies.get('shailuKiCookie');
// console.log(user);
if(user){
Cookies.remove("shailuKiCookie");
// alert("logout successfully");
window.location.reload(false);
}else{
window.location.reload(false);
}
}
enter image description here
function set_cookie(name, value) {
document.cookie = name +'='+ value +'; Path=/;';
}
function delete_cookie(name) {
document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
You don't have to specify a cookie value when you delete a cookie.
Just set the expires parameter to a past date:
note : HttpOnly cookies cannot be deleted with JavaScript on the client side.
you should create a Cookies object.
const cookies = new Cookies()
cookies.remove('cookie name')

Delete all cookies in page onUnload event

I try to use following code to delete all cookies in page unload event. However it doesn't seem to work in Chrome. Is there a better way to do it?
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var chip = cookies[i],
entry = chip.split("="),
name = entry[0];
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT; domain=' + window.location.hostname + '; path=/ ';
}
From another post I got a hint. The cookies I want to delete has Host only flag. Therefore I can not set domain when I write cookie back. Here is a working copy.
ar cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var chip = cookies[i],
entry = chip.split("="),
name = entry[0];
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/ ';
}

Varnish 4 vcl_deliver removes set-cookie from backend_response

I've this behaviour of vcl_deliver, it removes every http.set-cookie from backend.
This is my piece of varnishlog:
- VCL_call DELIVER
- RespUnset Set-Cookie: JSESSIONID=20E1512F59F3BA8A7BAE6AC2C10B0F66; Path=/; HttpOnly
- RespUnset Set-Cookie: OpenCmsOuFqn=/; Expires=Wed, 03-Feb-2016 13:18:41 GMT; Path=/
- RespUnset Set-Cookie: OpenCmsUserName=Admin; Expires=Wed, 03-Feb-2016 13:18:41 GMT; Path=/
- RespHeader Set-Cookie: LB=fep001; path=/;
- RespHeader X-Cache: MISS
- VCL_return deliver
I may not see the configuration mistake I post it too.
This is my default.vcl config file:
vcl 4.0;
import std;
import directors;
backend fep001 {
.host = "fe1";
.port = "82";
.probe = {
.url = "/ping";
.interval = 10s;
.timeout = 1s;
.window = 1;
.threshold = 1;
.expected_response = 200;
}
}
backend fep002 {
.host = "fe2";
.port = "82";
.probe = {
.url = "/ping";
.interval = 10s;
.timeout = 1s;
.window = 1;
.threshold = 1;
.expected_response = 200;
}
}
sub vcl_init {
new cluster = directors.round_robin();
new clusterhash = directors.hash();
cluster.add_backend(fep001);
clusterhash.add_backend(fep001, 1.0);
cluster.add_backend(fep002);
clusterhash.add_backend(fep002, 1.0);
}
sub vcl_recv {
if (req.http.Cookie ~ "LB=fep[0-9]+") {
set req.backend_hint = clusterhash.backend(req.http.Cookie.LB);
} else {
set req.backend_hint = cluster.backend();
}
if (! std.healthy(req.backend_hint)) {
std.log("not healthy");
set req.backend_hint = cluster.backend();
}
if (req.http.Cookie) {
set req.http.Cookie = regsuball(req.http.Cookie, "(^|; ) *LB=[^;]+;? *", "\1");
}
if (req.method != "GET" && req.method != "HEAD") {
return(pass);
}
if (req.url ~ "^/export/.*$") {
return(hash);
}
return(pass);
}
sub vcl_backend_response {
set beresp.http.X-node = beresp.backend.name;
set beresp.http.Vary = "Accept-Encoding";
if (bereq.url ~ "^/export/.*$" && beresp.status < 400) {
set beresp.ttl = 30m;
} else {
set beresp.ttl = 0s;
}
return(deliver);
}
sub vcl_deliver {
if (obj.hits == 0 && req.http.Cookie !~ "LB=fep[0-9]+") {
set resp.http.Set-Cookie = "LB=" + resp.http.X-node + "; path=/;";
}
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT:" + obj.hits;
} else {
set resp.http.X-Cache = "MISS";
}
}
How could I keep those http headers?
Thank you
Davide
I finally found one solution deduced from the article Proper sticky session load balancing in Varnish
It seems that Varnish 4 does not add other Set-Cookie but override it and does not add such Varnish 3 in such ways:
set resp.http.Set-Cookie = "LB=" + req.http.X-node + "; path=/;" + resp.http.Cookie;
It means that you have to use some VMODs.
I added cookie and header imports:
vcl 4.0;
import std;
import directors;
import cookie;
import header;
I changed a bit the backend selection:
cookie.parse(req.http.cookie);
if (cookie.get("LB")) {
set req.backend_hint = clusterhash.backend(cookie.get("LB"));
} else {
set req.backend_hint = cluster.backend();
}
if (! std.healthy(req.backend_hint)) {
std.log("not healthy");
set req.backend_hint = cluster.backend();
}
and in the end this is the main reason to add those VMODs:
if (obj.hits == 0 && req.http.Cookie !~ "LB=fep[0-9]+") {
header.remove(resp.http.Set-Cookie,"^LB=.*$");
header.append(resp.http.Set-Cookie,"LB=" + resp.http.X-node + "; Expires=" + cookie.format_rfc1123(now, 60m) + "; path=/;");
}
I hope this answer will help someone.

how to add cookies when the user login and how to delete cookies when user logout

I am already using this code add and delete in the cookies
deleted code:
String profileScore=null;
Cookie cookiesScore =new Cookie("profileScore","");
cookiesScore.setValue("");
cookiesScore.setMaxAge(0);
response.addCookie(cookiesScore);
but its not deleted properly please help me give any example
Try this if using javascript
create a cookie:
call a this method at login:
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
delete a cookie:
call a this method at logout:
function deleteCookies() {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}