2011-12-04 16:33:56 +01:00
|
|
|
/**
|
2019-04-16 00:34:29 +02:00
|
|
|
* This code is mostly from the old Etherpad. Please help us to comment this code.
|
2011-12-04 16:33:56 +01:00
|
|
|
* This helps other people to understand this code better and helps them to improve it.
|
|
|
|
* TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
|
|
|
|
*/
|
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2009 Google Inc.
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
var padcookie = (function()
|
|
|
|
{
|
2017-05-05 09:28:44 +02:00
|
|
|
var cookieName = isHttpsScheme() ? "prefs" : "prefsHttp";
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function getRawCookie()
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
// returns null if can't get cookie text
|
2011-07-07 19:59:34 +02:00
|
|
|
if (!document.cookie)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// look for (start of string OR semicolon) followed by whitespace followed by prefs=(something);
|
2017-05-05 09:54:26 +02:00
|
|
|
var regexResult = document.cookie.match(new RegExp("(?:^|;)\\s*" + cookieName + "=([^;]*)(?:;|$)"));
|
2011-07-07 19:59:34 +02:00
|
|
|
if ((!regexResult) || (!regexResult[1]))
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return regexResult[1];
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
|
|
|
|
function setRawCookie(safeText)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var expiresDate = new Date();
|
|
|
|
expiresDate.setFullYear(3000);
|
2016-06-08 21:14:10 +02:00
|
|
|
var secure = isHttpsScheme() ? ";secure" : "";
|
2020-07-10 09:43:20 +02:00
|
|
|
var sameSite = isHttpsScheme() ? ";sameSite=Strict": ";sameSite=Lax";
|
|
|
|
document.cookie = (cookieName + "=" + safeText + ";expires=" + expiresDate.toGMTString() + secure + sameSite);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
function parseCookie(text)
|
|
|
|
{
|
|
|
|
// returns null if can't parse cookie.
|
|
|
|
try
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var cookieData = JSON.parse(unescape(text));
|
|
|
|
return cookieData;
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
catch (e)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
|
|
|
|
function stringifyCookie(data)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
return escape(JSON.stringify(data));
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
|
|
|
|
function saveCookie()
|
|
|
|
{
|
|
|
|
if (!inited)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
setRawCookie(stringifyCookie(cookieData));
|
|
|
|
|
2013-02-07 14:30:09 +01:00
|
|
|
if ((!getRawCookie()) && (!alreadyWarnedAboutNoCookies))
|
2011-07-07 19:59:34 +02:00
|
|
|
{
|
2020-06-01 21:17:48 +02:00
|
|
|
$.gritter.add({
|
|
|
|
title: "Error",
|
|
|
|
text: html10n.get("pad.noCookie"),
|
|
|
|
sticky: true,
|
|
|
|
class_name: "error"
|
|
|
|
});
|
2011-03-26 14:10:41 +01:00
|
|
|
alreadyWarnedAboutNoCookies = true;
|
|
|
|
}
|
|
|
|
}
|
2019-04-16 00:34:29 +02:00
|
|
|
|
2016-06-08 21:14:10 +02:00
|
|
|
function isHttpsScheme() {
|
|
|
|
return window.location.protocol == "https:";
|
|
|
|
}
|
2011-03-26 14:10:41 +01:00
|
|
|
|
|
|
|
var wasNoCookie = true;
|
|
|
|
var cookieData = {};
|
|
|
|
var alreadyWarnedAboutNoCookies = false;
|
|
|
|
var inited = false;
|
|
|
|
|
2012-01-16 06:37:47 +01:00
|
|
|
var pad = undefined;
|
2011-03-26 14:10:41 +01:00
|
|
|
var self = {
|
2012-01-27 06:02:58 +01:00
|
|
|
init: function(prefsToSet, _pad)
|
2011-07-07 19:59:34 +02:00
|
|
|
{
|
2012-01-27 06:02:58 +01:00
|
|
|
pad = _pad;
|
2012-01-16 06:37:47 +01:00
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
var rawCookie = getRawCookie();
|
2011-07-07 19:59:34 +02:00
|
|
|
if (rawCookie)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
var cookieObj = parseCookie(rawCookie);
|
2011-07-07 19:59:34 +02:00
|
|
|
if (cookieObj)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
wasNoCookie = false; // there was a cookie
|
|
|
|
delete cookieObj.userId;
|
|
|
|
delete cookieObj.name;
|
|
|
|
delete cookieObj.colorId;
|
|
|
|
cookieData = cookieObj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
for (var k in prefsToSet)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
cookieData[k] = prefsToSet[k];
|
|
|
|
}
|
|
|
|
|
|
|
|
inited = true;
|
|
|
|
saveCookie();
|
|
|
|
},
|
2011-07-07 19:59:34 +02:00
|
|
|
wasNoCookie: function()
|
|
|
|
{
|
|
|
|
return wasNoCookie;
|
|
|
|
},
|
2017-05-05 10:17:07 +02:00
|
|
|
isCookiesEnabled: function() {
|
|
|
|
return !!getRawCookie();
|
|
|
|
},
|
2011-07-07 19:59:34 +02:00
|
|
|
getPref: function(prefName)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
return cookieData[prefName];
|
|
|
|
},
|
2011-07-07 19:59:34 +02:00
|
|
|
setPref: function(prefName, value)
|
|
|
|
{
|
2011-03-26 14:10:41 +01:00
|
|
|
cookieData[prefName] = value;
|
|
|
|
saveCookie();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return self;
|
2011-07-07 19:59:34 +02:00
|
|
|
}());
|
2012-01-16 02:23:48 +01:00
|
|
|
|
|
|
|
exports.padcookie = padcookie;
|