Created
April 6, 2017 02:29
-
-
Save nowk/f396a2f2ba1b9b43133798feccaeb438 to your computer and use it in GitHub Desktop.
Pomodor Inject Script for Everhour + E.ggtimer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Testined using https://chrome.google.com/webstore/detail/user-javascript-and-css/nbhcbdghjpllgmfilhnhkllmkecfmpld | |
var runningTimer = false; | |
var longBreakCount = 4; | |
var pomodoroTime = "25minutes"; | |
var shortBreakTime = "5minutes"; | |
var longBreakTime = "15minutes"; | |
var countPomodoro = function() { | |
var pomodoroCount = localStorage.getItem('pomodoroCount') || 0; | |
// reset the count if we exceed the longBreakCount | |
if (pomodoroCount > longBreakCount) { | |
pomodoroCount = 0; | |
} | |
pomodoroCount++; | |
localStorage.setItem("pomodoroCount", pomodoroCount); | |
return pomodoroCount; | |
} | |
var takeBreak = function(longBreak) { | |
var msg = longBreak ? "Take a long break?" : "Take a short break?"; | |
var breakTime = longBreak ? longBreakTime : shortBreakTime; | |
if (confirm(msg)) { | |
console.log("take a break..."); | |
window.open("http://e.ggtimer.com/" + breakTime, "e-ggtimer-com"); | |
} | |
} | |
var everhourFn = function() { | |
var startint, stopint; | |
var watchStop = function() { | |
if (!runningTimer) { | |
return; | |
} | |
if (!!!$("stop-timer").length) { | |
console.log("pomodor stop..."); | |
clearInterval(stopint); | |
runningTimer = false; | |
takeBreak(countPomodoro() >= longBreakCount); | |
} | |
}; | |
var watchStart = function() { | |
if (runningTimer) { | |
return; | |
} | |
if (!!$("stop-timer").length) { | |
console.log("start pomodoro..."); | |
runningTimer = true; | |
setInterval(watchStop, 1000); | |
// start the timer | |
window.open("http://e.ggtimer.com/" + pomodoroTime, "e-ggtimer-com"); | |
} | |
}; | |
// begin watching for a <stop-timer> element | |
startint = setInterval(watchStart, 1000); | |
var isRunningTimer = function() { | |
return runningTimer; | |
} | |
// catch any close/reload, if timmer is running | |
window.onbeforeunload = function(evt) { | |
if (isRunningTimer()) { | |
return true; | |
} | |
} | |
}; | |
var hostname = window.location.hostname; | |
// init on everhour | |
if (hostname === "app.everhour.com") { | |
console.log("[pomodoro][ OK ] on", hostname); | |
// set the window name | |
window.name = "app-everhour-com"; | |
// begin | |
$(everhourFn); | |
} | |
// init on e.ggtimer | |
if (hostname === "e.ggtimer.com") { | |
console.log("[pomodoro][ OK ] on", hostname); | |
var finished = false; | |
var focusEverhour = function() { | |
// FIXME and... none of this works... | |
var win = window.open("", "app-everhour-com", ""); | |
win.focus(); | |
// closing helps if the previous window was everhour, else it | |
// chooses window right of e.ggtimer.com | |
window.close(); | |
}; | |
Egg.onTimeComplete = function(evt) { | |
finished = true; | |
if (confirm("Pomodoro set done.")) { | |
focusEverhour(); | |
} else { | |
focusEverhour(); | |
} | |
Egg.updateParts = function(){}; | |
}; | |
var isFinished = function() { | |
return finished; | |
}; | |
// catch any close/reload if not finished | |
window.onbeforeunload = function(evt) { | |
if (!isFinished()) { | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment