Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aman34503 committed Nov 27, 2021
0 parents commit 3713213
Show file tree
Hide file tree
Showing 11 changed files with 1,286 additions and 0 deletions.
128 changes: 128 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
var min = 0;
var sec = 0;
var cycle = 1;
var totalRounds, longBreak;
var stopwatchStart = false;
var breakOrWork = false;
var timerStart = false;
var stopwatch, timer;
var isTimerDone = false;
aborted = false;
var finish;
var tmp = [];
chrome.runtime.onMessage.addListener(
function (request) {
if (request.message == "start stopwatch") {
min = 0;
sec = 0;
cycle = 1;
totalRounds = getCookie("roundNum");
longBreak = getCookie("restNum");
stopwatchStart = true;
aborted = false;
stopwatch = setInterval(stopwatchFunction, 1000);
} else if (request.message == "abort") {
clearInterval(stopwatch);
clearInterval(timer);
aborted = true;
} else if (request.message == "completed") clearInterval(finish);
else if (request.message == "update badLinks") {
tmp = request.content;
updateArray(request.content);
}
}
);

function timerFunction() {
if (timerStart) {
breakOrWork = false;
sec = parseInt(sec) - 1;
min = parseInt(min);
if (sec == -1) {
sec = 59;
min -= 1;
}
if (sec < 10) sec = '0' + sec;
if (min < 10) min = '0' + min;
} else clearInterval(timer);
if (min == "0-1") {
timerStart = false;
alert("Back to Work!");
chrome.runtime.sendMessage({message: "text", time: "00:00", subtitle: `Pomodoro Cycle ${cycle}`});
updateArray(tmp);
localStorage.setItem("nyaa",localStorage.getItem("TEMP"));

// start stopwatch
clearInterval(timer);
stopwatchStart = true;
aborted = false;
min = 0;
sec = 0;
stopwatch = setInterval(stopwatchFunction, 1000)
} else {
addCookie("time", `${min}:${sec}`);
addCookie("subtitle", "Take a Break!");
chrome.runtime.sendMessage({message: "text", time: `${min}:${sec}`, subtitle: "Take a Break!"});
}
}

function stopwatchFunction() {
breakOrWork = true;
if (stopwatchStart) {
sec = parseInt(sec) + 1;
min = parseInt(min);

if (sec == 60) {
min += 1;
sec = 0;
}

if (sec < 10) sec = '0' + sec;
if (min < 10) min = '0' + min;
}
if (min == "25" && sec == "01") {
clearInterval(stopwatch);
resetArray();
if (!aborted && cycle == totalRounds) {
alert("You are done!");
breakOrWork = false;
addCookie("timer-on", "done");
}
else if (!aborted && cycle % 4 == 0) {
min = longBreak;
sec = 0;
stopwatchStart = false;
alert(`You have completed Cycle ${cycle} of the Pomodoro!\nPlease take a longer break.`);
cycle += 1;

var t = `${min}:00`;
chrome.runtime.sendMessage({message: "text", time: t, subtitle: "Take a Break!"});

timerStart = true;
aborted = false;
timer = setInterval(timerFunction, 1000);
} else {
stopwatchStart = false;
alert(`You have completed Cycle ${cycle} of the Pomodoro!\nPlease take a five minute break.`);
cycle += 1;
chrome.runtime.sendMessage({message: "text", time: "05:00", subtitle: "Take a Break!"});

timerStart = true;
aborted = false;
min = 5;
sec = 0;
timer = setInterval(timerFunction, 1000);
}
} else {
addCookie("time", `${min}:${sec}`);
addCookie("subtitle", `Pomodoro Cycle ${cycle}`);
chrome.runtime.sendMessage({message: "text", time: `${min}:${sec}`, subtitle: `Pomodoro Cycle ${cycle}`});
}
}

function resetArray() {
updateArray("null");
badArray = ["*://*.thisisnotarealwebsite.com/*","*://*.ijustneedaplaceholderorelsethiswillerror.com/*"];
localStorage.setItem("TEMP",localStorage.getItem("nyaa"));
localStorage.setItem("nyaa",badArray);
}
42 changes: 42 additions & 0 deletions block_sites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//block_sites.js

var newLink;
var found = false;

chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
console.log(tab.url);
var url = new URL(tab.url);
url = String(url.hostname);
url = url.split(".")
if (url.length-1 == 2 && url[0].startsWith("www"))url = url.slice(1).join(".");
else url = url.join(".");
link = "*://*."+url+"/*";
console.log(link);

if(chrome.webRequest.onBeforeRequest.hasListener(blockRequest))
chrome.webRequest.onBeforeRequest.removeListener(blockRequest);
var rawr = localStorage.getItem("nyaa");
rawr = rawr.split(',').slice(1);
console.log(rawr);
for (let i = 0; i < rawr.length; i += 1){
console.log(rawr[i]);
var url = new URL(rawr[i]);
url = String(url.hostname);
url = url.split(".")
if (url.length-1 == 2 && url[0].startsWith("www"))url = url.slice(1).join(".");
else url = url.join(".");
rawr[i] = "*://*."+url+"/*";
}
console.log(rawr);
chrome.webRequest.onBeforeRequest.addListener(blockRequest, {urls: rawr }, ['blocking']);
if (rawr.includes(link) == true && (changeInfo.status == "complete" && tab.status == "complete" && tab.url != undefined)) alert("You are currently in a Pomodoro Session!\nPlease focus.");
})


function updateArray(arr) {
badLinks = arr.split(", ");
}

function blockRequest(details) {
return {cancel: true};
}
20 changes: 20 additions & 0 deletions cookie_functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function getCookie(name) {
var find = name + "=";
var things = document.cookie.split("; ");
for (let i = 0; i < things.length; i += 1) {
var found = true;
for (let j = 0; j < find.length; j += 1) {
if (things[i][j] != find[j]) {
found = false;
break;
}
}
if (found) return things[i].split(find)[1];
}
return "";
}

function addCookie(name, value) {
var expire = "expires=Fri, 3 Jan 3000 23:59:59 GMT";
document.cookie = `${name}=${value}; ${expire};path=/`;
}
84 changes: 84 additions & 0 deletions manage_websites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
var websiteDiv;

document.getElementById("bb4").onclick = () => mainMenu();
document.getElementById("websites-submit").onclick = () => {
addNewSite(document.getElementById("websites-add").value);
document.getElementById("websites-add").value = "";
}
document.getElementById("discard-button").onclick = () => document.getElementById("WebsiteBlocked").children = discardSites(document.getElementById("WebsiteBlocked").children);

function setWebsites() { // creates the set websites GUI
document.getElementById("MainMenu").hidden = true;
document.getElementById("SetupTimer").hidden = true;
document.getElementById("TimerOn").hidden = true;
document.getElementById("abortedScreen").hidden = true;
document.getElementById("websitesScreen").hidden = false;
websiteDiv = document.createElement("div");
websiteDiv.id = "WebsiteBlocked";
document.getElementById("websites-list").appendChild(websiteDiv);
const things = getCookie("banned").split(", ")
for (let i = 0; i < things.length; i += 1) addToBlockedWebsitesList(things[i]); // add website to blocked list
}

function addToBlockedWebsitesList(inner) { // add known blocked website to the blocked website list (in HTML)
let check = document.createElement("INPUT");
check.setAttribute("type", "checkbox");
check.name = inner;
let label = document.createElement("LABEL");
label.innerText = inner + "\n";
label.className = "boxes";
label.name = inner;
websiteDiv.appendChild(check);
websiteDiv.appendChild(label);
}

function addNewSite(link) { // tries (if possible) to add a new cookie and site
if (link == "") return;
else if (link.substring(0, 8) != "https://" && link.substring(0, 7) != "http://") alert('Please enter a valid URL precending with "https://" or "http://".');
else if (link.includes(" ")) alert('Please remove any whitespace.');
else {
try {
var url = new URL(link);
link = String(url.origin)+"/";
foundValid = true;
} catch (_){
foundValid = false;
}
if (!foundValid) {
alert("Please enter a valid URL with a proper domain.");
return;
}
if (getCookie("banned").includes(link)) {
alert("This website is already blocked.")
return;
}
arr = new Set(getCookie("banned").split(", "))
arr.add(link);
addCookie("banned", Array.from(arr).join(", "));
addToBlockedWebsitesList(link, true);
}
}

function discardSites(instances) {
var ind = instances.length - 2;
while (ind >= 0) {
if (instances[ind].checked) {
var arr = getCookie("banned").split(", ");
for (let i = 0; i < arr.length; i += 1) {
var found = true;
for (let j = 0; j < Math.min(instances[ind + 1].innerText.length, arr[i].length); j += 1) {
if (arr[i][j] != instances[ind + 1].innerText[j]) found = false;
}
if (found) {
arr.splice(i, 1);
addCookie("banned", arr.join(", "));
break;
}
}
instances[ind].remove();
instances[ind].remove();
}
ind -= 2;
}
return instances;
}
37 changes: 37 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"manifest_version": 2,
"name": "Potencymate",
"description": "Potencymate blocks distracting sites according to the Pomodoro Technique Set your timer for 25 minutes, and focus on a single task until the timer rings. When your session ends, mark off one pomodoro and record what you completed. Then enjoy a five-minute break. After four pomodoros, take a longer, more restorative 15-30 minute break.",
"version": "1.2",
"icons": {
"16": "./images/16.png",
"32": "./images/32.png",
"48": "./images/48.png",
"64": "./images/64.png",
"128": "./images/128.png"
},

"background": {
"scripts": ["timer.js", "block_sites.js", "cookie_functions.js", "background.js"]
},

"browser_action": {
"default_popup": "popup.html",
"default_title": "Potencymate"
},
"permissions": [
"tabs",
"alarms",
"cookies",
"debugger",
"webRequestBlocking",
"notifications",
"tts",

"webRequest",
"activeTab",
"http://*/*",
"https://*/*"
]
}

Loading

0 comments on commit 3713213

Please sign in to comment.