Merge pull request #71 from simple-login/localstorage-fallback
Localstorage fallback
This commit is contained in:
commit
c9d40bd507
|
@ -273,12 +273,12 @@
|
|||
<script>
|
||||
var clipboard = new ClipboardJS('.clipboard');
|
||||
|
||||
var introShown = localStorage.getItem("introShown");
|
||||
var introShown = store.get("introShown");
|
||||
if ("yes" !== introShown) {
|
||||
// only show intro when screen is big enough to show "developer" tab
|
||||
if (window.innerWidth >= 1024) {
|
||||
introJs().start();
|
||||
localStorage.setItem("introShown", "yes")
|
||||
store.set("introShown", "yes")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -412,10 +412,12 @@ def setup_do_not_track(app):
|
|||
@app.route("/dnt")
|
||||
def do_not_track():
|
||||
return """
|
||||
<script src="/static/local-storage-polyfill.js"></script>
|
||||
|
||||
<script>
|
||||
// Disable GoatCounter if this script is called
|
||||
|
||||
window.localStorage.setItem('goatcounter-ignore', 't');
|
||||
store.set('goatcounter-ignore', 't');
|
||||
|
||||
alert("GoatCounter disabled");
|
||||
|
||||
|
|
76
static/local-storage-polyfill.js
Normal file
76
static/local-storage-polyfill.js
Normal file
|
@ -0,0 +1,76 @@
|
|||
// From https://stackoverflow.com/a/12302790/1428034
|
||||
window.store = {
|
||||
localStoreSupport: function () {
|
||||
try {
|
||||
return 'localStorage' in window && window['localStorage'] !== null;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
set: function (name, value, days) {
|
||||
if (days) {
|
||||
var date = new Date();
|
||||
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
|
||||
var expires = "; expires=" + date.toGMTString();
|
||||
}
|
||||
else {
|
||||
var expires = "";
|
||||
}
|
||||
if (this.localStoreSupport()) {
|
||||
localStorage.setItem(name, value);
|
||||
}
|
||||
else {
|
||||
document.cookie = name + "=" + value + expires + "; path=/";
|
||||
}
|
||||
},
|
||||
get: function (name) {
|
||||
if (this.localStoreSupport()) {
|
||||
var ret = localStorage.getItem(name);
|
||||
//console.log(typeof ret);
|
||||
switch (ret) {
|
||||
case 'true':
|
||||
return true;
|
||||
case 'false':
|
||||
return false;
|
||||
default:
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// cookie fallback
|
||||
/*
|
||||
* after adding a cookie like
|
||||
* >> document.cookie = "bar=test; expires=Thu, 14 Jun 2018 13:05:38 GMT; path=/"
|
||||
* the value of document.cookie may look like
|
||||
* >> "foo=value; bar=test"
|
||||
*/
|
||||
var nameEQ = name + "="; // what we are looking for
|
||||
var ca = document.cookie.split(';'); // split into separate cookies
|
||||
for (var i = 0; i < ca.length; i++) {
|
||||
var c = ca[i]; // the current cookie
|
||||
while (c.charAt(0) == ' ') c = c.substring(1, c.length); // remove leading spaces
|
||||
if (c.indexOf(nameEQ) == 0) { // if it is the searched cookie
|
||||
var ret = c.substring(nameEQ.length, c.length);
|
||||
// making "true" and "false" a boolean again.
|
||||
switch (ret) {
|
||||
case 'true':
|
||||
return true;
|
||||
case 'false':
|
||||
return false;
|
||||
default:
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null; // no cookie found
|
||||
}
|
||||
},
|
||||
del: function (name) {
|
||||
if (this.localStoreSupport()) {
|
||||
localStorage.removeItem(name);
|
||||
}
|
||||
else {
|
||||
this.set(name, "", -1);
|
||||
}
|
||||
},
|
||||
}
|
|
@ -170,6 +170,8 @@
|
|||
|
||||
</script>
|
||||
|
||||
<script src="/static/local-storage-polyfill.js"></script>
|
||||
|
||||
<!-- For additional script -->
|
||||
{% block script %}
|
||||
{% endblock %}
|
||||
|
@ -182,7 +184,7 @@
|
|||
return;
|
||||
}
|
||||
|
||||
if (localStorage.getItem('goatcounter-ignore') === 't') {
|
||||
if (store.get('goatcounter-ignore') === 't') {
|
||||
console.log("GoatCounter is disabled");
|
||||
return;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue