Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timer version #3

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified hackbeanpot/.DS_Store
Binary file not shown.
103 changes: 92 additions & 11 deletions hackbeanpot/background.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,93 @@
chrome.tabs.onUpdated.addListener((tabId, tab) => {
if (tab.url && tab.url.includes("youtube.com/watch")) {
const queryParameters = tab.url.split("?")[1];
const urlParameters = new URLSearchParams(queryParameters);

chrome.tabs.sendMessage(tabId, {
type: "NEW",
videoId: urlParameters.get("v"),
});
}
});
function saveURLs(urls) {
chrome.storage.sync.set({ 'userUrls': urls }, function() {
console.log('User URLs saved:', urls);
});
}

function getUserUrls(callback) {
chrome.storage.sync.get('userUrls', function(data) {
const userUrls = data.userUrls || [];
console.log('User URLs retrieved:', userUrls);
if(callback) {
callback(userUrls);
}
})
}

function addUrlToList(url) {
const urlList = document.getElementById('urlList');
const listItem = document.createElement('li');
listItem.textContent = url;
urlList.appendChild(listItem);
}

document.addEventListener('DOMContentLoaded', function() {
const urlForm = document.getElementById('urlForm');
const urlInput = document.getElementById('urlInput');

getUserUrls(function(userUrls) {
userUrls.forEach(function(url) {
addUrlToList(url);
});
});

urlForm.addEventListener('submit', function(event) {
event.preventDefault();
const url = urlInput.value.trim();
if(url) {
addUrlToList(url);
getUserUrls(function(userUrls) {
userUrls.push(url);
saveURLs(userUrls);
});
urlInput.value = '';
}
})
})

saveURLs(userinput);

const userCountdown = 60;
let countdownInterval;

function checkURL() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
const currentUrl = tabs[0].url;

const isMatch = urlList.some(url => currentUrl.includes(url));

if(isMatch) {
console.log("Current URL matches");
startCountdown();
} else {
console.log("Current URL does not match");
pauseCountdown();
}
})

}

function beginCounting() {
let remainingSecs = countdown;
countdownInterval = setInterval(() => {
console.log(secondsLeft + " seconds left");
if(secondsLeft === 0) {
clearInterval(countdownInterval);
console.log("Countdown finished!");
} else {
remainingSecs--;
}
}, 1000)
}

function pauseCountdown() {
clearInterval(countdownInterval);
isCountdownRunning = false;
}

checkURL();

setInterval(checkURL, 1000);



86 changes: 13 additions & 73 deletions hackbeanpot/contentScript.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,13 @@
(() => {
let youtubeLeftControls, youtubePlayer;
let currentVideo = "";
let currentVideoBookmarks = [];

const fetchBookmarks = () => {
return new Promise((resolve) => {
chrome.storage.sync.get([currentVideo], (obj) => {
resolve(obj[currentVideo] ? JSON.parse(obj[currentVideo]) : []);
});
});
};

const addNewBookmarkEventHandler = async () => {
const currentTime = youtubePlayer.currentTime;
const newBookmark = {
time: currentTime,
desc: "Bookmark at " + getTime(currentTime),
};

currentVideoBookmarks = await fetchBookmarks();

chrome.storage.sync.set({
[currentVideo]: JSON.stringify([...currentVideoBookmarks, newBookmark].sort((a, b) => a.time - b.time))
});
};

const newVideoLoaded = async () => {
const bookmarkBtnExists = document.getElementsByClassName("bookmark-btn")[0];

currentVideoBookmarks = await fetchBookmarks();

if (!bookmarkBtnExists) {
const bookmarkBtn = document.createElement("img");

bookmarkBtn.src = chrome.runtime.getURL("assets/bookmark.png");
bookmarkBtn.className = "ytp-button " + "bookmark-btn";
bookmarkBtn.title = "Click to bookmark current timestamp";

youtubeLeftControls = document.getElementsByClassName("ytp-left-controls")[0];
youtubePlayer = document.getElementsByClassName('video-stream')[0];

youtubeLeftControls.appendChild(bookmarkBtn);
bookmarkBtn.addEventListener("click", addNewBookmarkEventHandler);
}
};

chrome.runtime.onMessage.addListener((obj, sender, response) => {
const { type, value, videoId } = obj;

if (type === "NEW") {
currentVideo = videoId;
newVideoLoaded();
} else if (type === "PLAY") {
youtubePlayer.currentTime = value;
} else if ( type === "DELETE") {
currentVideoBookmarks = currentVideoBookmarks.filter((b) => b.time != value);
chrome.storage.sync.set({ [currentVideo]: JSON.stringify(currentVideoBookmarks) });

response(currentVideoBookmarks);
}
});

newVideoLoaded();
})();

const getTime = t => {
var date = new Date(0);
date.setSeconds(t);

return date.toISOString().substr(11, 8);
};

function manipulateDOMBasedOnTime(elapsedTime) {
//CHANGE THIS
//if the elapsedTime is greater than or equal to the user inputted time, add waves
if(elapsedTime >= userCountdown) {
document.body.style.backgroundColor = 'lightblue';
}
}

let elapsedTime = 0;
const timerInterval = setInterval(() => {
elapsedTime++;
manipulateDOMBasedOnTime(elapsedTime);
}, 1000);
Binary file added hackbeanpot/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions hackbeanpot/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
"description": "Extension to facilitate productivity online",
"version": "1.0",
"permissions": ["storage", "tabs", "activeTab"],
"host_permissions": ["https://*.youtube.com/*"],
"background": {
"service_worker": "background.js"
},
"action": {
"default_popup": "popup.html",
"default_icon": "testimage.png"
"default_icon": "logo.png"
},
"content_scripts": [
{
Expand Down
Empty file added hackbeanpot/options.css
Empty file.
Empty file added hackbeanpot/options.html
Empty file.
74 changes: 19 additions & 55 deletions hackbeanpot/popup.css
Original file line number Diff line number Diff line change
@@ -1,56 +1,20 @@
.container {
width: 380px;
height: 220px;
color: #314d3e;
body{
background-color: lightblue;
}

.title {
font-size: 20px;
font-weight: bold;
padding: 8px;
}

.textbox {
width: 100%;
font-size: 12px;
margin: 0;
padding: 0px 2px;
}

.textbox:focus {
outline: 0;
border-color: #66afe9;
}

.bookmarks {
margin: 5px 5px;
padding: 3px;
}

.bookmark {
display: flex;
border-bottom-color: #00254d;
border-bottom-width: 1px;
border-bottom-style: solid;
border-radius: 0.8rem;
padding: 3px;
padding-bottom: 7px;
margin-bottom: 7px;
}

.bookmark-title {
padding-left: 2px;
}

.bookmark-controls img {
margin: 0 4px;
width: 18px;
height: 18px;
cursor: pointer;
}
.bookmark-controls {
flex: auto;
text-align: right;
}

font-size: 25px;
width: 400px;
height: 240px;
}
#timer{
background: #fff;
color: #000;
width: 150px;
margin: 50px auto;
margin-left: center;
margin-top: 20px;
text-align: center;
}
.settings {
background-color: transparent;
max-width: 50%;
max-height: 50%;
}
32 changes: 25 additions & 7 deletions hackbeanpot/popup.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
<link href="popup.css" rel="stylesheet" type="text/css" />
<script type="module" src="popup.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Countdown Timer</title>
<link rel="stylesheet" href="popup.css">
</head>
<body>

<div class="container">
<div>
<div class="title">Your bookmarks for this video</div>
<link href="popup.css" rel="stylesheet" type="text/css" />

<div class="bookmarks" id="bookmarks"></div>
</div>
<div>
<p id="chosenMessage">Example Message</p>
<input type="number" id="countdownInput" min="1" placeholder="Time (minutes)">
<button id="startButton">Start Countdown</button>
<button id="pauseButton" disabled>Pause</button>
<button id="resetButton">Reset</button> <!-- New button for resetting the timer -->
</div>
<div id="timer">00:00:00</div>
<script src="popup.js"></script>
</div>

<a href="options.html" class="settings"><img src = "settings.png" alt="Settings" width = "100" height="100">
</a>

</body>
</html>
Loading