Javascript change variable depending if I'm the user - javascript

I'm programming a game and I currently have the player speed set to a variable. What I'd like to do is use JavaScript to detect if my computer is playing the game and then change the speed. example: The default player speed is a variable that has a value of 5. If the program detects It's me that's playing the game, change the speed to 10. I would like to do this in JavaScript and I can't figure out a way to achieve this. How would I be able to accomplish this?

Save a value using Cookies or Web Storage and just check if it exists. If it exists, set the player speed to 10.
For example using Web Storage:
Set the value using
localStorage.setItem("check", "1");
and check if it exists using
if (localStorage.check == 1) {console.log("true");}
Some documentations:
Cookies: https://www.w3schools.com/js/js_cookies.asp
Web Storage: https://www.w3schools.com/html/html5_webstorage.asp

JavaScript will not be able to determine your Windows user name. unless it's on the current page or you do AJAX calls to a server-side script to get more information.
But you can do it manually: save in session storage or local storage and then check if something exists, it will save on your computer so it will know:
sessionStorage.setItem('status','loggedIn')
and then:
if (sessionStorage.getItem('status') != null))
//redirect to page
}
else{
//show validation message
}

Related

how to find tab close and clear local storage

I am using onbeforeunload function in javascript but problem is that if browser is refresh then also clear local storage . I want to only clear local storage when browser close not refresh browser
window.onbeforeunload = function(e) {
localStorage.setItem('isLoggedIn', 'false');
};
You can use sessionStorage instead. It is automatically cleared when the browser is closed.
Example:
sessionStorage.setItem("isLoggedIn","true"); // Value will be set to null when browser is closed
You can be needed to learn about local storage, session storage, and Cookies. there are 3 options use for store data but it's one of the different another concept.
local storage store locally and it's permanent if you not remove this or clear browser.
session storage use for containing a status or a token or a user some information.
when you close the browser then session storage remove automatically. (note: if you went any running time you can change it like as logout).
Cookies is another powerful way to store data in the browser or client site. it also allows the time schedule or date. when expiring time or date then it's destroyed automatically.
there are 3 storage are the different their concept and work it's also different storage capacity each other.
Session storage operation:
sessionStorage.SessionName = "SessionData" ,
sessionStorage.getItem("SessionName") and
sessionStorage.setItem("SessionName","SessionData");
Cookie example simple operation:
you can also learn about more:
https://www.quora.com/What-is-the-difference-between-sessionstorage-localstorage-and-Cookies
https://developer.mozilla.org/en-US/docs/Mozilla/Gecko/DOM_Storage_implementation_notes
https://www.w3schools.com/js/js_cookies.asp
=== Thanks ===

How to save browser extension data in local storage?

Can anyone tell me how to fix this one line and save this one variable so I can get on with my day?
I'm a total noob to browser extensions. My extension is intended to make changes to any webpage a user visits, and determines what changes to make by AJAX querying a database - that much works fine. To make my extension valuable for multiple users though, each user needs an ID number that persists across all domains.
My system works like this: on log-in at the extension's home site, the user gets a cookie with an encrypted ID number. I need to get that number from the cookie and save it in local storage, but I can't figure out how to save the ID number. The critical code is this:
//If we've just logged in we get our ID number from the cookie:
if(visitedURL.indexOf("log-in site name here") > -1){
userId = getCookie("the cookie");
//Then we make a key/value pair to hold our ID number
userStorage = {
key : userid
}
//And then, we attempt to save it. Uncommenting the following causes my extension to break.
setting = browser.storage.local.set({
userStorage;
});
}
I used this as my guide:
https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/storage/StorageArea/set

How to make my upvote/downvote button store cookies and count votes only 1 time

Take a look at this https://fiddle.jshell.net/tnvv0dds/4/ I made this upvote/downvote button for my website, but it resets every time I refresh the website and it counts votes every time I click on it. I have no idea how to fix it since I am a bit new. Can someone make a JavaScript suggestion for me? I spent 3-4 days in searching it without success.
You can learn HTML5 Web Storage, and use them with the following code.
$(document).ready(function() {
if(localStorage.getItem("count") == null)
$("#count").text(0);
else
$("#count").text(localStorage.getItem("count"));
});
var counter = 0;
$("#plus").click(function(){
counter++;
$("#count").text(counter);
localStorage.setItem("count", counter);
});
$("#minus").click(function(){
counter--;
$("#count").text(counter);
localStorage.setItem("count", counter);
});
If you want people to cast one vote each, you have to save the information of who voted how in a database on the server. This is the only way you can have a persistent count. Showing the current count, of course, involves querying the DB. Saving information into a database requires you using a server-sided programming language like e.g. PHP. This cannot be done with pure client-sided JavaScript.
To prevent people from double-voting, you can save their IP address on first vote and deny the vote whenever you find the current IP address in the database. Since some people have dynamic/changing IP addresses, this isn't bullet-proof. Additionally (really, only as an addition) you could save a flag in local storage or cookie and check that whenever a user tries to vote. Set flag on first vote and deny vote if flag was found.

Storing a 'user' in javascript

I would like to enable a user to save their preferences without having to log in. I was thinking of using the user's IP to save their preferences, but this doesn't work for 'workplaces' where multiple people will be on the same IP-connection.
What would be the best way to do this? Basically, I want to store a per-person session without using a database (and hopefully, without having to use a secondary data-store, such as redis).
Is it possible to do this in javascript?
Cookies are your best bet. Tied to the browser of a user, and available for your server too - https://developer.mozilla.org/en-US/docs/Web/API/document/cookie
You may also look at local storage - https://developer.mozilla.org/en/docs/Web/API/Window/localStorage
Understand the difference between the two and make a choice - Local Storage vs Cookies
From your description, I think that saving these preferences on the user's machine is the best option. If it is small amount of data (less than 100KB), then use cookies. If it is big (but still within allowed limits approx 5MB) then use local storage (indexedDB or webStorage).
You can use localstorage. You can store key-value items.
Set value:
window.localstorage.setItem(key, value);
Get value:
var value = window.localstorage.getItem(key);
Example: http://jsfiddle.net/wasnvvo7/1/
Just give the user a unique key to store as a cookie and then use that key to keep track of that user's data. --WARNING!! if the user moves to a different machine or deletes his/her cookies, the session will not be usable.

Cookie with Multiple Values stored

I'm having trouble with cookies. I have a bunch of links that when clicked on, create a cookie. For each link I need to be able to save that cookie value to the main cookie name.
Here is the click function I'm using to create the cookie:
$j('a.createCookie').click(function(e) {
var cookieName = "InsightsCookie";
var cookieValue = $j(this).attr("id");
$j.cookie(cookieName, cookieValue, {expires: 365, path: '/'});
});
The end result would be "InsightsCookie: cookieValue, cookieValue, cookieValue" - where each link clicked on would add a value to InsightsCookie.
Any help would be much appreciated.
Cookies aren't intended to store structured data.
Typically the cookie has some kind of key value (a random integer, or alphanumerical value, for example) that is unique to that person. The web site uses that cookie to know who is visiting, and then keeps track of all the times/places the person with that cookie goes in some kind of database, thereby building a history.
So, basically, it's typically the web site's job to keep track of that, not the cookie on the user's machine.
If that's not an option for you for some reason, you could simply get the value that's already in the cookie, and then append the new value to it with each visit. If that user visits a lot of pages on your site, the cookie might get too big very quickly. There are restrictions on the maximum size of a cookie, and that's kind of a janky way to do it.

Categories