Starting to get the hang out of it. I can detect clicks on save now. Next step is to try to save data.

This commit is contained in:
julien lengrand-lambert
2014-02-27 16:57:39 +01:00
parent 7d598a2665
commit ab89d970e5
3 changed files with 161 additions and 8 deletions

View File

@@ -0,0 +1,39 @@
////////////////////////////////
/**
* OverTime calculation related code
*/
////////////////////////////////
/*
* Checks whether our browser supports LocalStorage
*/
function supports_html5_storage() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
}
//Main action
// We want to operate only on the right page, and if localStorage can be used
if (document.title.indexOf("Fortnox") != -1 && supports_html5_storage()) {
//Creating Elements
var btn = document.createElement("BUTTON")
var t = document.createTextNode("CLICK ME");
btn.appendChild(t);
//Appending to DOMo
document.body.appendChild(btn);
document.getElementById("button_save_and_group").onclick= function(event) {
alert("caca");
// Compensate for IE<9's non-standard event model
//
if (event===undefined) event= window.event;
var target= 'target' in event? event.target : event.srcElement;
alert('clicked on '+target.tagName);
};
}

115
fortNoxOvertime/getHours.js Normal file
View File

@@ -0,0 +1,115 @@
var weeklyHours = 40; // Number of hours to be worked per week
/*
* Checks whether our browser supports LocalStorage
*/
function supports_html5_storage() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
}
/*
* The method that is run instead of deliver.
* Contains all the statistics calculation
*/
function saveStatistics(){
var workingTime = calculateWorkingTime();
var overtime = calculateOvertime(workingTime);
console.log("Overtime this week : " + overtime);
// Need to store the information somewhere now. Using localStorage
saveWorkingTime(workingTime);
saveWeekOvertime(overtime);
saveOvertime(overtime);
//Giving original behaviour back
SaveWeekRows(true);
}
/*
* Calculate the amount of hours worked this week
*/
function calculateWorkingTime(){
var hours = parseInt(document.getElementById("info_hour_sum").innerHTML);
return hours;
}
/*
* Calculates the number of extra hours I worked this week
*/
function calculateOvertime(workingTime){
var weeklyOvertime = workingTime - weeklyHours;
if(weeklyOvertime > 0){
return weeklyOvertime;
}
else{
return 0;
}
}
function saveOvertime(overtime){
key = "overtime";
var current = localStorage[key];
// Checking for possible problems
if(isNaN(current)){
current = 0;
}
localStorage[key] = current + overtime;
}
/*
* Saves the total time worked for a given week
* The key used is the fortnox code for this week :
* year + weeknumber + month : 1409-02 for 9th week of 2014 in february + over
*/
function saveWorkingTime(workingTime){
var weekKey = getWeekKey();
localStorage[weekKey] = workingTime;
}
/*
* Saves the overtime for a given week
* The key used is the fortnox code for this week :
* year + weeknumber + month : 1409-02 for 9th week of 2014 in february
*/
function saveWeekOvertime(overtime){
var weekOvertimeKey = getWeekOvertimeKey();
localStorage[weekOvertimeKey] = overtime;
}
/*
* creates a hash key for the weekly working time
*/
function getWeekKey(){
return document.getElementById("time_week").value;
}
/*
* creates a hash key for the weekly overtime
*/
function getWeekOvertimeKey(){
return getWeekKey() + "_overtime";
}
/*
Happens no matter what
*/
var ok = supports_html5_storage();
if(ok){
alert("Local Storage allowed!");
}
else{
alert("Your browser does not support LocalStorage!");
}
document.getElementById("button_save_and_group").onclick = saveStatistics;

View File

@@ -4,12 +4,11 @@
"name": "FortNox Overtime",
"description": "This extension keeps track of my days off and overtime in FortNox",
"version": "1.0",
"permissions": [
"https://secure.flickr.com/"
],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
}
"content_scripts": [
{
"matches": ["https://apps2.fortnox.se/time/time_time/*"],
"js": ["fortnox.js"]
}
]
// "web_accessible_resources": ["getHours.js"]
}