Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Firefox extension, which tracks time spent in Facebook and tells it to you INSTANTLY.
timeSpent = 0;
timeStarts = 0
browser.tabs.onActivated.addListener((activeInfo) => {
browser.tabs.sendMessage(
activeInfo.tabId,
{timeSpent: timeSpent}
).then(response => {
timeStarts = (new Date).getTime();
}).catch(error => {
if (timeStarts > 0) {
timeSpent += (new Date).getTime() - timeStarts;
}
timeStarts = 0;
});
});
{
"manifest_version": 2,
"name": "Higher power",
"version": "1.0",
"description": "Helps you to avoid temptations",
"icons": {
},
"content_scripts": [
{
"matches": ["*://*.facebook.com/*"],
"js": ["power.js"],
"run_at": "document_end"
}
],
"background": {
"scripts": ["background-script.js"]
}
}
var banner = document.getElementById("mybanner");
if (banner == null) {
banner = document.createElement("div");
}
banner.id = "mybanner";
banner.innerHTML = "Banner Content";
banner.style.position = "fixed";
banner.style.zIndex = 99999999;
banner.style.display = "block";
banner.style.backgroundColor = "#CCC";
banner.style.top = 2;
banner.style.left = 2;
banner.style.border = "2px solid green";
document.body.insertBefore(banner, document.body.childNodes[0]);
timeSpent = 0;
seconds = 0;
function tick() {
console.log("tick");
seconds++;
spent = (Math.floor(timeSpent/1000) + seconds);
spentMin = Math.floor(spent / 60);
spentHours = Math.floor(spentMin / 60);
repr = "" + spent + "s";
if (spentMin >= 1) {
repr = "" + spentMin + "min";
} else if (spentHours >= 1) {
repr = "" + spentHours + "h " + spentMin + "min";
}
banner.innerHTML = "Spent here: " + repr;
if (spent > 15*60) {
banner.style.backgroundColor = "yellow";
} else if (spent > 60*60) {
banner.style.backgroundColor = "red";
}
}
intervalId = setInterval(tick, 1000);
browser.runtime.onMessage.addListener(request => {
clearInterval(intervalId);
seconds = 0;
timeSpent = request.timeSpent;
intervalId = setInterval(tick, 1000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.