diff cs/cookies.js @ 78:f833a888c548

add cookie-based PIN system, and update laterlinks to use it
author paulo
date Thu, 02 Jun 2016 00:27:50 -0700
parents
children 4d3f845c4ef2
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/cs/cookies.js	Thu Jun 02 00:27:50 2016 -0700
     1.3 @@ -0,0 +1,63 @@
     1.4 +/*\
     1.5 +|*|
     1.6 +|*|  :: cookies.js ::
     1.7 +|*|
     1.8 +|*|  A complete cookies reader/writer framework with full unicode support.
     1.9 +|*|
    1.10 +|*|  Revision #1 - September 4, 2014
    1.11 +|*|
    1.12 +|*|  https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
    1.13 +|*|  https://developer.mozilla.org/User:fusionchess
    1.14 +|*|
    1.15 +|*|  This framework is released under the GNU Public License, version 3 or later.
    1.16 +|*|  http://www.gnu.org/licenses/gpl-3.0-standalone.html
    1.17 +|*|
    1.18 +|*|  Syntaxes:
    1.19 +|*|
    1.20 +|*|  * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
    1.21 +|*|  * docCookies.getItem(name)
    1.22 +|*|  * docCookies.removeItem(name[, path[, domain]])
    1.23 +|*|  * docCookies.hasItem(name)
    1.24 +|*|  * docCookies.keys()
    1.25 +|*|
    1.26 +\*/
    1.27 +
    1.28 +var docCookies = {
    1.29 +  getItem: function (sKey) {
    1.30 +    if (!sKey) { return null; }
    1.31 +    return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
    1.32 +  },
    1.33 +  setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
    1.34 +    if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
    1.35 +    var sExpires = "";
    1.36 +    if (vEnd) {
    1.37 +      switch (vEnd.constructor) {
    1.38 +        case Number:
    1.39 +          sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
    1.40 +          break;
    1.41 +        case String:
    1.42 +          sExpires = "; expires=" + vEnd;
    1.43 +          break;
    1.44 +        case Date:
    1.45 +          sExpires = "; expires=" + vEnd.toUTCString();
    1.46 +          break;
    1.47 +      }
    1.48 +    }
    1.49 +    document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
    1.50 +    return true;
    1.51 +  },
    1.52 +  removeItem: function (sKey, sPath, sDomain) {
    1.53 +    if (!this.hasItem(sKey)) { return false; }
    1.54 +    document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
    1.55 +    return true;
    1.56 +  },
    1.57 +  hasItem: function (sKey) {
    1.58 +    if (!sKey) { return false; }
    1.59 +    return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
    1.60 +  },
    1.61 +  keys: function () {
    1.62 +    var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
    1.63 +    for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); }
    1.64 +    return aKeys;
    1.65 +  }
    1.66 +};