I'm looking for a way to change the value returned from javascript's new Date() function.
In general - I'm looking for a way to write an extension that will give the user the ability to set his timezone (or diff from the time of his system's clock) without changing time/timezone on his computer.
I checked the chrome extension api but found nothing there. Will appreciate it if someone can point me at the right direction.
I have seen some solutions involving overriding javascript's getTime() method:
Date.prototype.getTime = function() { return [DATE + OFFSET] };
usage:
(new Date).getTime();
(source)
Apparently the time does not come from the browser but rather the operating system. So my suggestion would be to inject this javascript using a content script, and in your overriding function, you can offset the date/time by however much you would like.
Related
I am trying to get the current hour of the time which i try in this approach.
var currentTimeHour = dates.ZonedDateTime.now().getHour()
It did return value but it is zero while my time is 3pm. And i notice when the time turn to 4pm, the value return become one.
I did tried getMinute and getDay and it seems fine. Did i did anything wrong and is there anyway to get the hour of the current time?
Thanks.
dates.ZonedDateTime.now().getHour() is correct one to use.
Or you may want to use just dates.ZonedDateTime.now().time.hour
var myNow = dates.ZonedDateTime.now()
console.log('myNow', myNow)
var myHour = dates.ZonedDateTime.now().getHour()
console.log('myNow.getHour()', myHour)
Let's try the above code in JS file, and can see clearly the structure of now() in debug console. As the following screenshot.
First confirm the timezone is correct. IDE may change to a different timezone. This is the screenshot if I change IDE to Indiana timezone.
To confirm/change IDE timezone, use simulator -> User -> GPS/Clock override feature.
I have a single Sammy route that recognizes an arbitrary number of parameters. The route looks like this:
get(/^\/(?:\?[^#]*)?#page\/?((?:[^\:\/]+\:[^\:\/]+\/?)*)$/g, function() {
var params = {};
var splat = this.params.splat[0];
var re = /([^\:\/]+)\:([^\:\/]+)/g;
match = true
while(match = re.exec(splat)) {
params[match[1]] = match[2];
}
self.loadData(params);
});
This code works. What it does is it recognizes routes of the pattern #page/param1:value1/param2:value2/ for an arbitrary number of parameters. My loadData function has default values for many of these parameters. I'm confident there isn't a problem with the actual loading of the pages, since it works 100% on many computers in many browsers. However, it has weird behavior on my Android's browser and on my friend's Mac's Safari and Chrome (works on my PC's Chrome). I've noticed that these are Webkit browsers.
The behavior is that the route runs correctly for the first URL change, then won't for the next URL change (although the URL in the browser bar does indeed always change), then it'll work again for the third one, and won't for the fourth. That is, it works every other time. This seems like very strange behavior to me, and I'm at a loss as to how to debug this. For certain links, I was able to run a hack such that on click I set the window location to the URL and forcefully run the sammy code with runRoute('get', url);. It's impractical to have to add this for every click event on the page, and that doesn't really account for all URL changes anyway. Is there something I can do to debug why my route isn't being run every time the URL is changing?
For those of you who encounter similar behavior, on every other click in the above-mentioned browsers, this.params.splat was undefined. It's supposed to be set to the matched part of the URL (e.g. /#page/param1:value1/).
The hack I came up with to deal with this is to add this to the top of the get route:
if(this.params.splat === undefined) {
app.unload().run();
return;
}
This doesn't get to the root of the problem, it's just a hack that allows it to re-run the routes so that params.splat isn't undefined the next time through. If anyone has more information on what is going on, I'd be interested.
am a newbie, trying to write some basics extension. For my extension to work i need to initialize some data, so what I did is inside my background.js i declared something like this.
localStorage["frequency"] = 1; //I want one as Default value. This line is not inside any method, its just the first line of the file background.js
Users can goto Options page and change this above variable to any value using the GUI. As soon as the user changes it in UI am updating that value.
Now the problem is to my understanding background.js reloads everytime the machine is restarted. So every time I restart my machine and open Chrome the frequency value is changed back to 1. In order to avoid this where I need to initialize this value?
You could just use a specific default key. So if frequency is not set you would try default-frequency. The default keys are then still set or defined in the background.js.
I like to do that in one step, in a function like this
function storageGet(key,defaultValue){
var item = localstorage.getItem(key);
if(item === null)return defaultValue;
else return item;
}
(According to the specification localstorage must return null if no value has been set.)
So for your case it would look something like
var f = storageGet("frequency",1);
Furthermore you might be interested in checking out the chrome.storage API. It's used similar to localstorage but provides additional functionalities which might be useful for your extension. In particular it supports to synchronize the user data across different chrome browsers.
edit I changed the if statement in regard to apsillers objection. But since the specification says it's ought to be null, I think it makes sense to check for that instead of undefined.
This is another solution:
// background.js
initializeDefaultValues();
function initializeDefaultValues() {
if (localStorage.getItem('default_values_initialized')) {
return;
}
// set default values for your variable here
localStorage.setItem('frequency', 1);
localStorage.setItem('default_values_initialized', true);
}
I think the problem lies with your syntax. To get and set your localStorage values try using this:
// to set
localStorage.setItem("frequency", 1);
// to get
localStorage.getItem("frequency");
In my chrome extension I would like to make any changes in user preferences in-memory while the extension is running, and only save the preferences (as a JSON obj) to localStorage when the extension is about to terminate (I use a background html page in my extension).
I am currently writing to localStorage every time a preference is changed but am looking to see if this can be made more efficient to avoid writing to disk on each preference change and instead just do it once before termination.
Is there any way to know when the extension is being terminated or does anyone know of a better approach to handle this scenario?
Does this help?
function saveSettings(){
window.clearTimeout(window.savingSettings);
return window.savingSettings = window.setTimeout(function(){
... // write to local storage here.
},2000)
};
This will makes sure:
there's at least two seconds between save operations
the whole thing runs somewhat parallelized to the main thread
You should see a performance boost to some degree but there's a catch. If a user closes the browser without waiting those two seconds after changing a pref, prefs won't be saved.
How is real time autocomplete with prefix matching implemented in Quora ?
Since Solr and Sphinx doesn't support real-time updating so what changes were made to support real time updating?
Looks like it's done using javascript and jquery. I grabbed a few key lines from the minified script on the Quora homepage that I think support this theory:
Here's an ajax call to a resource providing JSON data:
$.ajax({type:"GET",url:this.resultsQueryPath,dataType:"json",data:a,success:this.fnbind(ƒ(a){this.ajaxCallback(a)}),error:this.fnbind(ƒ(a,b,c){console.log(b,c),this.requestOutstanding=!1,this.$("##results_shell").html("Could not retrieve results: "+b)})})}
note that the successful result gets put into the "a" variable. Then later here's the autocompletion based on the keydown of the "question_box" element which is completing from the parent of "a"
this.$ ("##item input.question_box").keydown (ƒ (b) {
if (b.keyCode==9&&!b.shiftKey)for (var c=e.getLiveDomId (a.cid),d=a.parent ().orderedVisibleChildren (),f\^M=0;f<d.length-1;++f)if (c==d [f]) {
$ (this).blur (),$ ("#"+d [f+1]+" input.question_box").focus ();return!1}
})
I think this is pretty incontrovertible, but it would still be nice to have the un-minified script to compare. For instance I can't see where resultsQueryPath comes from (I can't locate it's source, may be intentionally obfuscated).