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
parent
891d2600fa
commit
d55edebddd
|
@ -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.
|
* Copyright 2009 Google Inc.
|
||||||
*
|
*
|
||||||
|
@ -20,130 +14,51 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const createCookie = require('./pad_utils').createCookie;
|
||||||
|
const readCookie = require('./pad_utils').readCookie;
|
||||||
|
|
||||||
var padcookie = (function()
|
exports.padcookie = new class {
|
||||||
{
|
constructor() {
|
||||||
var cookieName = isHttpsScheme() ? "prefs" : "prefsHttp";
|
this.cookieName_ = window.location.protocol === 'https:' ? 'prefs' : 'prefsHttp';
|
||||||
|
const prefs = this.readPrefs_() || {};
|
||||||
function getRawCookie()
|
delete prefs.userId;
|
||||||
{
|
delete prefs.name;
|
||||||
// returns null if can't get cookie text
|
delete prefs.colorId;
|
||||||
if (!document.cookie)
|
this.prefs_ = prefs;
|
||||||
{
|
this.savePrefs_();
|
||||||
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];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function setRawCookie(safeText)
|
init() {
|
||||||
{
|
if (this.readPrefs_() == null) {
|
||||||
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))
|
|
||||||
{
|
|
||||||
$.gritter.add({
|
$.gritter.add({
|
||||||
title: "Error",
|
title: 'Error',
|
||||||
text: html10n.get("pad.noCookie"),
|
text: html10n.get('pad.noCookie'),
|
||||||
sticky: true,
|
sticky: true,
|
||||||
class_name: "error"
|
class_name: 'error',
|
||||||
});
|
});
|
||||||
alreadyWarnedAboutNoCookies = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function isHttpsScheme() {
|
readPrefs_() {
|
||||||
return window.location.protocol == "https:";
|
const jsonEsc = readCookie(this.cookieName_);
|
||||||
|
if (jsonEsc == null) return null;
|
||||||
|
try {
|
||||||
|
return JSON.parse(unescape(jsonEsc));
|
||||||
|
} catch (e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var wasNoCookie = true;
|
savePrefs_() {
|
||||||
var cookieData = {};
|
createCookie(this.cookieName_, escape(JSON.stringify(this.prefs_)), 365 * 100);
|
||||||
var alreadyWarnedAboutNoCookies = false;
|
}
|
||||||
var inited = false;
|
|
||||||
|
|
||||||
var pad = undefined;
|
getPref(prefName) {
|
||||||
var self = {
|
return this.prefs_[prefName];
|
||||||
init: function(prefsToSet, _pad)
|
}
|
||||||
{
|
|
||||||
pad = _pad;
|
|
||||||
|
|
||||||
var rawCookie = getRawCookie();
|
setPref(prefName, value) {
|
||||||
if (rawCookie)
|
this.prefs_[prefName] = value;
|
||||||
{
|
this.savePrefs_();
|
||||||
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;
|
|
||||||
|
|
Loading…
Reference in New Issue