html – JavaScript `localStorage` with` document.cookie` Fallback for static websites

The code

So far, I've been putting things into operation as desired, and I hope new eyes can point to bugs or trifles that can be improved. A live demo is hosted and updated on GitHub Pages, and the source code is just one fork or clone Path….

BrowserStorage.js

Class BrowserStorage {
/ **
* @This BrowserStorage
* /
Constructor() {
this.supports_local_storage = this.supportsLocalStorage ();
this.supports_cookies = this.supportsCookies ();
this.storage_available = (this.supports_cookies || this.supports_local_storage)? true wrong;
}

/ **
* Please use `this.supports_local_storage` within tests instead.
* @returns {boolean}
* /
meetsLocalStorage () {
To attempt {
let key = & test;
leave value = true;
localStorage.setItem (key, value);
localStorage.getItem (key);
localStorage.removeItem (key);
} catch (e) {
return it incorrectly;
}
return true;
}

/ **
* Please use `this.supports_cookies` instead within tests.
* @returns {boolean}
* /
supportsCookies () {
if (navigator.cookieEnabled) returns true;

To attempt {
document.cookie = & # 39; testcookie & # 39 ;;
} catch (e) {
return it incorrectly;
} Finally {
if (document.cookie.indexOf (& # 39; testcookie & # 39;)! = -1) {
this.remove (& # 39; testcookie & # 39;);
return true;
}
return it incorrectly;
}
}

/ **
* Copy of the constructor method that should not contain a type error
* /
constructorRefresh () {
this.supports_local_storage = this.supportsLocalStorage ();
this.supports_cookies = this.supportsCookies ();
this.storage_available = (this.supports_cookies || this.supports_local_storage)? true wrong;
}

/ **
* Receives the specified value from browser memory and URI component encoding on return
* @returns {? boolean |? number |? string}
* @This BrowserStorage
* /
get (key) {
let value = zero;
if (this.supports_local_storage) {
value = localStorage.getItem (key);
} else if (this.supports_cookies) {
let cookie_data = document.cookie.match (& # 39; (^ |;)? & # 39; + key + & # 39; = ([^;]*) (; $) & # 39;);
value = cookie_data? cookie_data[2] : Zero;
}
return encodeURIComponent (value);
}

/ **
* Removes the selected value by key from the browser memory. Note for cookies: The next time you load the page, a full whipe will appear
* @returns {boolean}
* @This BrowserStorage
* /
remove (key) {
if (this.supports_local_storage) {
localStorage.removeItem (key)
return true;
} else if (this.supports_cookies) {
// Note that disabling and expiring in the past is like this
// to remove a cookie the next time the page loads.
this.set (key, & # 39 ;, -7);
return true;
}
return it incorrectly;
}

/ **
* Saves client settings in the browser, _serverless_ in the truest sense of the word
* @peram {string | number} key - _variable name_ to save the value below
* @peram {boolean | number | string} value - is stored either under localStorage or as a cookie
* @peram {number} days_to_live - how long a browser should save cookies
* @returns {boolean}
* @This BrowserStorage
* /
set (key, value, days_to_live = false) {
if (this.supports_local_storage) {
localStorage.setItem (key, value);
return true;
} else if (this.supports_cookies) {
let expires = & # 39; & # 39 ;;

if (days_to_live) {
let date = new Date ();
if (days_to_live == 0) {
date.setTime (date.getTime ());
} else {
date.setTime (date.getTime () + (days_to_live * 24 * 60 * 60 * 1000));
}
expires = & # 39 ;; expires = & # 39; + date.toGMTString ();
}

document.cookie = name + & # 39; = & # 39; + value + expires + & # 39 ;; Path = / & # 39 ;;
return true;
}
return it incorrectly;
}

/ **
* Deletes ** all ** client settings from localStorage or cookies
* @returns {boolean}
* @This BrowserStorage
* /
clear() {
if (this.supports_local_storage) {
localStorage.clear ();
return true;
} else if (this.supports_cookies) {
let cookies = document.cookie.split (& # 39 ;; & # 39;);
for (if i = 0; i <cookies.length; i ++) {
let cookie = cookies[i];
let key = encodeURIComponent (cookie.split (& # 39; = & # 39;)[0].replace (/ (^  s + |  s + $) / g, & # 39; & # 39;));
if (typeof (key) == 'string' = typeof (key) == # number & # 39;
this.remove (key);
} else {
return it incorrectly;
}
}
return true;
}
return it incorrectly;
}

}

main.css

/ **
* 1. Center the element
* 2. Reorient text
* /
Body {
Text orientation: center; / * 1 * /
}

.container__main_content {
maximum width: 80%;
maximum width: 80vw;
Display: Inline block; / * 1 * /
Align text: left; / * 2 * /
}

/ **
* Privacy Policy Footer Stuff
* /
.container__privacy_notice {
Position: fixed;
bottom: 5px;
maximum width: 80%;
maximum width: 80vw;
Background color: light gray;
Align text: left; / * 2 * /
}

.privacy_notice__label {
Background color: black;
Color white;
Font size: 1.5rem;
Upholstery: 5px 10px;
float to the right;
}

.privacy_notice__label: hover {
Color: light gray;
}

/ * Status of the label, if the check box is deactivated * /
.privacy_notice__label :: after {
Content: attr (value);
}


/ **
* Checkbox hacks
* /
.hidden__input {
Display: none;
Visibility: hidden;
Opacity: 0;
Filter: alpha (opacity = 0);
}


/ * Status updates for ticking the checkbox * /
.privacy_notice__checkbox: checked ~ .container__privacy_notice {
Box shadow: not set;
max-width: 1.5rem;
max height: 1.5rem;
}

.privacy_notice__checkbox: checked ~ .container__privacy_notice * {
Visibility: hidden;
Opacity: 0;
Filter: alpha (opacity = 0);
}

.privacy_notice__checkbox: checked ~ .container__privacy_notice .privacy_notice__label :: after {
Content: attr (checked);
}

/ * Status of the label, if the check box is activated * /
.privacy_notice__checkbox: checked ~ .container__privacy_notice :: after {
Content: & # 39;  1F56D & # 39 ;;
Position: absolutely;
bottom: 5px;
left: 5px;
}

/ * Combination of checked and suspended state updates * /
.privacy_notice__checkbox: checked ~ .container__privacy_notice: hover {
maximum width: 80%;
maximum width: 80vw;
maximum height: not set;
}

.privacy_notice__checkbox: checked ~ .container__privacy_notice: hover * {
Visibility: visible;
Opacity: 1;
Filter: Alpha (opacity = 100);
}

index.html



  
    BrowserStorage example
    
    

    
    

    
  

  
    

This is a static site that gives the illusion of remembering a client. Test by accepting the Data protection and then refreshing or Close and reopen a tab that points to the current URL.

Ctrl R or Ctrl shift R updates most desktop browsers.

Then you decline the privacy policy by clicking the Acceptable button again to restore the behavior of this site.

Use Ctrl shift I Debugger on one open Desktop Browser, then navigate to the Storage tab to watch it JavaScript performs the above actions.

This site uses JavaScript and localStorage or cookies of your browser. However, this site is currently doing so to improve your experience here Not Save all data about your activity on the other side.

Check the privacy and security hints about BrowserStorage.js Learn more about using client-side storage and scripts.

Note that I tried to use the webcode option when writing this question, but this resulted in various errors.

ask

My goal is to achieve nearly 100% coverage for browsers that have JavaScript local memory or document.cookie Storage options;

  • in this Day and age Does it make sense to use cookies for a local storage fallback?

  • Is there any reason to use or use the old syntax to get JavaScript OO behavior? class and function Keywords are sufficient?

if there are good arguments for not using class, please include some examples of feature detection to load an alternate version.

  • I keep walking over npm But that seems to be more focused on Node-related JavaScript, so I should consider doing so npm init. or would that pollute your package management system?

If projects like this are a good candidate for npm, then a few tips on OS-independent output methods Git Commands would be excellent.

  • Currently I use encodeURIComponent However, returning values ​​stored in the browser results in unpredictable behavior on Boolean and numeric values ​​(sometimes it's a string, sometimes not) better Way to retrieve values?

At this point, it feels like I need to do a few preliminary checks before returning to achieve consistent behavior.

  • Where did I make mistakes and / or how can code execution be improved without affecting readability?

  • One thing I find annoying about the use Checkbox hacks Is accessibility, is there any? Well Ways to get tab and space/Enter Support?


Thank you, and I hope that, even if that is above, I am one bad influence that we can all learn together why.

A programming language that extends schema with static typing

I've developed a programming language called Theme-D that extends the schema with static typing. See Theme D homepage.

Some features of Theme-D include

* Static type system
* A simple object system
* Multi-methods dispatched runtime (and also compilation time)
* Parameterized (type parameters) classes, types and procedures
* Signature types that are similar to Java interfaces but are sent multiple times
* A modular system
* Two types of variables: constants and variable variables

I've also written a wrapper library for Guile gnomes so it's possible to create GUIs with Theme-D.

Any comments?

How can I permanently assign a static IP address to Wi-Fi or USB tether clients?

I use my phone as an extended display with x11vnc that works as intended.
The only problem is that I have to change the IP address on my phone's VNC client very often. Because of dynamic DHCP I suppose.
I use a Motorola g Turbo Lineage OS 7.1.2, rooted.

Can Android reserve IP?
Basically, IP address and host name are assigned depending on the Mac address.
Or something that does the same.

Generate static pages with Drupal 7 and Gatsby

I am currently in the process of creating a static and up-to-date website using Gatsby and Drupal 7. The content is published every Monday for Gatsby.
Gatsby works fine when I hook up GarphQL.
However, in Drupal 7, GraphQL is not stable and does not work in all scenarios.
Therefore, I plan to use the REST API to generate an API that will be used through Gatsby.
In this case, the site is fully dynamic, but we prefer to create a static but up-to-date site.

There are two scenarios:

1) Managing content from the backend (Drupal 7) and whenever content is updated, updates must be managed in a specific state, but in this case every time APIs are called.

2) Can I use nodejs to create middleware to use Drupal 7 REST APIs and convert them to GraphQL? Then I can use Gatsby in the front-end and connect to that middleware to generate a precompiled static site.

Give me your suggestion for the approach and new suggestions are welcome.

Many Thanks

Static nginx proxy static file locations for multiple Web applications where static files have the same relative path

I've set up Nginx as a reverse proxy for various web apps running on an Apache server:

/ var / www / app_abc
| -static
| | -app.js
| | -app.css
| -index.html

/ var / www / app_xyz
| -static
| | -app.js
| | -app.css
| -index.html

app_abc runs on the Apache virtual host port 8081
The app_xyz runs on the Apache virtual host port 8082

They work properly in Apache when you access them:

lure http: // localhost: 8081
lure http: // localhost: 8082

Now I only have one domain: real-domain.com
I can not create a subdomain like abc.domain.com or xyz.domain.com

I have the nginx configuration as follows:

By default, app_abc is called when the request is "http://www.real-domain.com":

Location / {
proxy_pass http: // localhost: 8081
}

If you are requesting http://www.real-domain.com/xyz, call app_xyz

location ~ /xyz(.*) {
rewrite ^ / xyz /?(.*) / $ 1 break;
proxy_pass http: // localhost: 8082;
}

for static files

Location ~ ^ / static {
try_files $ uri $ uri / / var / www / app_abc / $ uri / var / www / app_xyz / $ uri;
}

The only problem here is that the requests:

static / app.js
static / app.cs

for "http://www.real-domain.com/xyz" 404 is returned

works for "http://www.real-domain.com/"

my question is :

How are static files provided for different apps where static files in different apps share the same relative paths?

ipv6 – Forward traffic from a device with a static IP

I'd like to set up a VPN on my home network, but I've just learned that my ISP (Comcast) only provides public IP addresses to business customers, and I can not get them without canceling and paying my 1-year contract a fine, and to rent their modem.

I'm wondering if I could set up a tiny cloud server with a static IP and easily forward all traffic. The whole thing seems a bit crazy, because I guess my local server would have to do a query to prevent data from being lost when my IP is reassigned. This is all rather unfamiliar territory for me and I have no idea if there is a reasonable solution if I do not have a public IP. Every advice is appreciated!

magento2.3 – Query string variables are displayed on a static page

How do I get information about the query and view it on a landing page?

This is a link that was created on another website and contains variables that can be passed to the magento2.3.1 website.
Www.mymagentowebsite.com/staticpage.html?w=150&h=220&c=2&shape=LZ0005

Now I want to get the variables w, h, c and shape
and display them on a static page.

c ++ – Can a lib file (foo.lib) be used for both dynamic and static links?

As I understand it, a lib file is used either for a dynamic link (as a DLL export library) or a static link (the link is the implementation). It can not be used for both.

I use MKS Toolkit (third-party software) to create a project. Found this in their header file doing lib selection. However, both options use the same library. The library is an export library for a DLL.

Sorry, the code is rejected due to a wrong format.

The MT option results in an expected connection error. I just want to know why code is written that way or what's wrong with my understanding or action.