cookies: Refactor `pad_cookie.js`

* Use the cookie functions from `pad_utils.js`.
  * Delete unused methods, variables, and parameters.
  * Simplify the logic.
  * Use an ES6 class instead of a weird literal thingy.
  * Use `const` instead of `var`.
pull/4392/head
Richard Hansen 2020-10-02 23:46:41 -04:00 committed by John McLear
parent 891d2600fa
commit d55edebddd
1 changed files with 35 additions and 120 deletions

View File

@ -1,9 +1,3 @@
/**
* This code is mostly from the old Etherpad. Please help us to comment this code.
* This helps other people to understand this code better and helps them to improve it.
* TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
*/
/**
* Copyright 2009 Google Inc.
*
@ -20,130 +14,51 @@
* limitations under the License.
*/
const createCookie = require('./pad_utils').createCookie;
const readCookie = require('./pad_utils').readCookie;
var padcookie = (function()
{
var cookieName = isHttpsScheme() ? "prefs" : "prefsHttp";
function getRawCookie()
{
// returns null if can't get cookie text
if (!document.cookie)
{
return null;
}
// look for (start of string OR semicolon) followed by whitespace followed by prefs=(something);
var regexResult = document.cookie.match(new RegExp("(?:^|;)\\s*" + cookieName + "=([^;]*)(?:;|$)"));
if ((!regexResult) || (!regexResult[1]))
{
return null;
}
return regexResult[1];
exports.padcookie = new class {
constructor() {
this.cookieName_ = window.location.protocol === 'https:' ? 'prefs' : 'prefsHttp';
const prefs = this.readPrefs_() || {};
delete prefs.userId;
delete prefs.name;
delete prefs.colorId;
this.prefs_ = prefs;
this.savePrefs_();
}
function setRawCookie(safeText)
{
var expiresDate = new Date();
expiresDate.setFullYear(3000);
var secure = isHttpsScheme() ? ";secure" : "";
var sameSite = isHttpsScheme() ? ";sameSite=Strict": ";sameSite=Lax";
document.cookie = (cookieName + "=" + safeText + ";expires=" + expiresDate.toGMTString() + secure + sameSite);
}
function parseCookie(text)
{
// returns null if can't parse cookie.
try
{
var cookieData = JSON.parse(unescape(text));
return cookieData;
}
catch (e)
{
return null;
}
}
function stringifyCookie(data)
{
return escape(JSON.stringify(data));
}
function saveCookie()
{
if (!inited)
{
return;
}
setRawCookie(stringifyCookie(cookieData));
if ((!getRawCookie()) && (!alreadyWarnedAboutNoCookies))
{
init() {
if (this.readPrefs_() == null) {
$.gritter.add({
title: "Error",
text: html10n.get("pad.noCookie"),
title: 'Error',
text: html10n.get('pad.noCookie'),
sticky: true,
class_name: "error"
class_name: 'error',
});
alreadyWarnedAboutNoCookies = true;
}
}
function isHttpsScheme() {
return window.location.protocol == "https:";
readPrefs_() {
const jsonEsc = readCookie(this.cookieName_);
if (jsonEsc == null) return null;
try {
return JSON.parse(unescape(jsonEsc));
} catch (e) {
return null;
}
}
var wasNoCookie = true;
var cookieData = {};
var alreadyWarnedAboutNoCookies = false;
var inited = false;
savePrefs_() {
createCookie(this.cookieName_, escape(JSON.stringify(this.prefs_)), 365 * 100);
}
var pad = undefined;
var self = {
init: function(prefsToSet, _pad)
{
pad = _pad;
getPref(prefName) {
return this.prefs_[prefName];
}
var rawCookie = getRawCookie();
if (rawCookie)
{
var cookieObj = parseCookie(rawCookie);
if (cookieObj)
{
wasNoCookie = false; // there was a cookie
delete cookieObj.userId;
delete cookieObj.name;
delete cookieObj.colorId;
cookieData = cookieObj;
}
}
for (var k in prefsToSet)
{
cookieData[k] = prefsToSet[k];
}
inited = true;
saveCookie();
},
wasNoCookie: function()
{
return wasNoCookie;
},
isCookiesEnabled: function() {
return !!getRawCookie();
},
getPref: function(prefName)
{
return cookieData[prefName];
},
setPref: function(prefName, value)
{
cookieData[prefName] = value;
saveCookie();
}
};
return self;
}());
exports.padcookie = padcookie;
setPref(prefName, value) {
this.prefs_[prefName] = value;
this.savePrefs_();
}
}();