Chrome extension to reduce the time by 10 minutes - javascript

I am developing a simple extension where I would like to have two buttons (one being "Reduce Time" and other being "Reset Time").
I have created the manifest.json and other necessary files, including the html that contains the two buttons.
How should I code in the .js file so that I can achieve the following functionality:
On clicking the "Reduce Time", the system time should go back 10
mins
On clicking the "Reset Time", the system time should revert
back to original time.
My .js so far is thus:
var globalCount = 0;
function reduceTime(){
var d = new Date();
d.setTime(d.getTime()-600000);
globalCount = globalCount + 1;
}
function resetTime(){
var d = new Date();
if(globalCount > 0)
{
d.setTime(d.getTime() + (600000*globalCount));
}
}
But when I run the extension, and click on the "Reduce Time" button, I believe nothing happens as the date in the taskbar still shows the same value. Am I missing something?

The code is doing what you're asking it to do. If you include a log statement after either of the setTime() calls, you'll find that d is indeed changed, just as setTime() is advertised to do. Read more about setTime().
The code cannot do what you want it to do. JavaScript in a browser can't change the system clock. It would be all sorts of horror if it could.

Related

How to get a value if the present time is within a time window?

I have been trying to copy the value in A9 into the cell in column D, when the current time (now or time.new) corresponds to the time specified in the same row in column C.
I managed to get a script that pulls the value D1 once in an hour into the last cell in column D, which would be perfectly fine if the "time-driven" trigger would not pick a random time in the hour. It needs to be exactly 2 minutes past every full hour (+/- 30 seconds). So I thought to make the script run every minute and paste the value in a cell if the time in colum C is within 1h2m30s and 1h1m30s before the present time... But with my script there seems to be an error.
Please find the test document here, where the function is currently being executed once every hour.
The Apps Script that runs at a random time every hour is as follows (currently running):
function myFunction() {
var ss= SpreadsheetApp.getActiveSpreadsheet();
var sheet= ss.getSheetByName('FACTS & PREDICTIONS');
var Direction=SpreadsheetApp.Direction;
let aLast =sheet.getRange("D"+(sheet.getLastRow()+1)).getNextDataCell(Direction.UP).getRow();
let val= sheet.getRange('A9').getDisplayValue();
sheet.getRange(`D${aLast+1}`).setValue(val);
}
The new script, that I have been trying to make is as follows:
function myFunction() {
var ss= SpreadsheetApp.getActiveSpreadsheet();
var sheet= ss.getSheetByName('Admin Panel');
var Direction=SpreadsheetApp.Direction;
if(e.source.getActiveSheet().getName()=="FACTS & PREDICTIONS") {
if(col == 4 && cell.offset(0,-1).getValue() > new Date()-TIME(1,2,30) && col == 4 && cell.offset(0,-1).getValue() < new Date()-TIME(1,1,30)){
let val= sheet.getRange('A9').getDisplayValue();
e.source.getActiveSheet().getRange(row,4).setValue(val);
}
}
}
The above script is actually patched together because I have no clue of Apps Scrips. I have no idea what is wrong with the code or what I need to change.
Edit: Just fount this, where you can make a Trigger locked to a specific minute, BUT it has +/- 15 minutes deviation. Useless...
Using a time-driven trigger to run every minute might be the right approch but the expressions comparing Date objects are wrong because comparing them always will be false.
As you are new to Google Apps Script, you might find worthy to spend some time learning the basics and common quirks about Google Apps Script and JavaScript, i.e.
Compare two dates with JavaScript
Also you might have to rethink if Google Sheets / Apps Script is the right tool for this task specially when requiring to work with precise times / small time tolerances and requiring high reliability. This because the Google Apps Script triggers are not 100% reliable, i.e. the trigger might fail to run all and every minute, time-driven triggers have a daily total time execution quota.

JavaScript Date does not reflect system time change in Google Chrome

Implementing html+js clock timer (jsfiddle sample) and I found problem when changing operating system time backward and then forward.
Lets have this html:
<div id="time">-</div>
And JS code:
var time = document.getElementById('time');
function iteration() {
time.innerHTML = new Date().toString();
}
setInterval(iteration, 100);
Lets say you started page at 2:22PM. Label display correct time.
Now change operating system local time backward 1 hour to 1:22PM, now JS new Date() correctly returns changed time 1:22PM.
Next change operating system local time forward to actual date - 2:22PM, and now JS new Date() does not return 2:22PM but old 1:22PM. So it seems to me that it does not handle correctly changes to local system time (forward only?).
Seems to be problem only in Google Chrome (37.0.2062.124 m (64-bit)).
In Internet Explorer and Firefox, JS new Date() return correct value.
Update: Does anyone know how to 'fix' this for Google chrome using JS code?

Create a scheduled Greasemonkey script

I need to create a special kind of script.
I want to show a message at certain times of the day. I've tested the code in Firebug Console and it works. The code is:
//Getting the hour minute and seconds of current time
var nowHours = new Date().getHours() + '';
var nowMinutes = new Date().getMinutes() + '';
var nowSeconds = new Date().getSeconds() + '';
var this_event = nowHours + nowMinutes + nowSeconds;
//172735 = 4PM 25 Minutes 30 Seconds. Just checked if now is the time
if (this_event == "162530") {
window.alert("Its Time!");
}
I feel that the Script is not running every second. For this to work effectively, the script has to be able to check the hour minutes and second "Every Second". I'm not worried about the performance, I just have to be accurate about the timing (to the second).
How do I do this?
Of course the script isn't running each second, GM-scripts run once when the document has been loaded.
Calculate the difference between the current time and the target-time and use a timeout based on the difference:
var now=new Date(),
then=new Date(),
diff;
then.setHours(16);
then.setMinutes(15);
then.setSeconds(30);
diff=then.getTime()-now.getTime();
//when time already has been reached
if(diff<=0){
window.alert('you\'re late');
}
//start a timer
else{
window.setTimeout(function(){window.alert('it\'s time');},diff);
}
Javascript doesn't guarantee your timeouts and other such events fire exactly on-time.
You should compare two Date objects using >= and remove the timeout or what ever other method you're using for tracking the time inside the matching if (and then reset it if necessary).
For more details see: https://stackoverflow.com/a/19252674/1470607
Alternatively you can use string comparison (but with caveats): https://stackoverflow.com/a/6212411/1470607

Javascript countdown - not counting down

I have a countdown script that gets the live time and subtracts it from a set time. It all works apart from the fact that it doesn't update unless you refresh your page. The setInterval at the bottom of my function instructs the function to run every one second, but it doesn't seem to be doing that...
Can anybody help?
Here is my jsfiddle: http://jsfiddle.net/4yMZy/
Each time cCountDown runs, its is calculating the time left like so:
nDates = new Date(datetime);
xDay = new Date("Fri, 26 May 2012 16:34:00 +0000");
timeLeft = (xDay - nDates);
The value of datetime there never changes from one run to another. So cCountDown is constantly running, but is always comparing the difference between the same two dates. Since the same two dates are used, the difference is always the same, so you do not see any countdown occur.
You could change nDates = new Date(datetime); to nDates = new Date(); and it will start counting down, but I am not sure why you are getting datetime from some server in the first place.
There are some other issues with your code as well. You should run it through jslint or jshint.
If changed your code to http://jsfiddle.net/4yMZy/7/
So it fetches the time and updates the seconds according to the set interval.
Edit:
jsfiddle actually provides a button for that on the top.

Javascript setTimeout on iOS Safari

I am working on a little script that warns the user that his session is about to time out and his/her changes might not get saved.
On any browser, that works pretty well and I implemented a solution that just uses setTimeout to trigger a dialog box after a certain amount of time (unless the user takes certain actions in between).
On iOS Safari, however, this approach doesn't work, as the setTimeout gets "halted" while the user navigates to another app on his/her phone. Once the user opens Safari again and comes back to the page, the timer continues where it left off, rather than looking at the total time that expired.
Any suggestions on how to approach a session timeout warning that doesn't break on the iPhone?
Set the end time of the session in a variable.
Instead of using a counter, use javascript's date:
// get a date object
var today = new Date();
// ask the object for some information
var hours = today.getHours();
var minutes = today.getMinutes();
var seconds = today.getSeconds();
var theHour = today.getHours();
Compare the end time to the current time every second
Disclaimer: Handle case where user returns and session has ended.

Categories

Resources