24
loading...
This website collects cookies to deliver better user experience
manifest.json
. In the file, add the manifest version, name of the extension, a description and the version of the extension to begin with. The file should now look similar to this.{
"manifest_version": 3,
"name": "Count Down Days",
"version": "1.0",
"description": "Takes a date input and displays the number of days left until the given date"
}
"background": {
"service_worker": "background.js"
},
"action": {
"default_popup": "content/popup.html",
"default_icon": {
"16": "images/icon16.png", // optional
"24": "images/icon24.png", // optional
"32": "images/icon32.png" // optional
}
}
{
"manifest_version": 3,
"name": "Count Down Days",
"version": "0.1",
"description": "Takes a date input and displays the day count left to the given date ",
"background": {
"service_worker": "background.js"
},
"action": {
"default_popup": "content/popup.html",
"default_icon": {
"16": "/images/timer.png",
"128": "/images/timer.png",
"48": "/images/timer.png",
"256": "/images/timer.png"
}
}
}
chrome://extensions
"permissions": ["activeTab" ,"storage", "scripting"]
let date = "08 15 2021";
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.sync.set({ date });
console.log("Default Date set to Aug 15, 2021");
});
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="popup.css" />
</head>
<body>
<div class="buttons">
<button id="showDays">
<img class="img-icon" src="../images/timer.png" alt="Sand Clock" />
</button>
<button id="changeDate">
<img class="img-icon" src="../images/change-date.png" alt="Change Date Icon" />
</button>
</div>
<script src="popup.js"></script>
</body>
</html>
button {
height: 30px;
width: 30px;
outline: none;
margin: 10px;
border: none;
border-radius: 2px;
}
button img {
width: 100%;
height: auto;
}
Note: The Chrome extension CANNOT be opened on chrome:// URL.
// Content script follows
function showDaysLeft() {
// get the date string from Chrome storage
chrome.storage.sync.get("date", ({ date }) => {
// create a new div that will be appended to the body
let daysElement = document.createElement("div");
// adding styles to the new div
daysElement.style.cssText = "position: absolute; color: black; top: 30px; left: 50%; transform: translateX(-50%); background-color: pink; z-index: 99999; padding: 1rem; border-radius: 10px; box-shadow: 3px 3px 6px #00000060";
// Date.parse converts Date string to milliseconds
// To get the number of days left we get the difference in milliseconds and divide by 86400000 (milliseconds in a day)
noOfDaysLeft = parseInt((Date.parse(new Date(date)) - Date.parse(new Date())) / (86400000));
let content = '';
if (noOfDaysLeft < 0) {
content = document.createTextNode("Deadline has already passed.Please set a new one. :D");
alert(daysElement);
} else {
content = document.createTextNode(noOfDaysLeft + " days until go time! B)");
}
// Append the text node to the div
daysElement.appendChild(content);
// Append the div to the body tag
document.body.appendChild(daysElement);
setTimeout(() => {
document.body.removeChild(daysElement)
}, 3000);
});
}
function resetDate() {
let newDate = " ";
let daysElement = document.createElement("div");
daysElement.style.cssText = "position: absolute; color: black; top: 30px; left: 50%; transform: translateX(-50%); background-color: pink; z-index: 99999; padding: 1rem; border-radius: 10px; box-shadow: 3px 3px 6px #00000060";
// Get the date string input through window.prompt
newDate = window.prompt("Enter date in the dd/mm/yyyy format");
dateArray = newDate.split("/");
dateString = dateArray[1] + " " + dateArray[0] + " " + dateArray[2];
newDate = Date.parse(new Date(dateString));
let content = '';
// Check if the format is right
if (newDate) {
noOfDaysLeft = parseInt((Date.parse(new Date(newDate)) - Date.parse(new Date())) / (86400000));
if (noOfDaysLeft < 0) {
content = document.createTextNode("Are you time travelling to the past? I am not ready for you yet :D");
} else {
content = document.createTextNode("New date saved! \n" + noOfDaysLeft + " days until go time! B)");
// save the new date
chrome.storage.sync.set({ "date": newDate });
}
} else {
content = document.createTextNode("Enter a valid date - date/month/full-year");
}
daysElement.appendChild(content);
document.body.appendChild(daysElement);
setTimeout(() => {
document.body.removeChild(daysElement)
}, 3000);
}
// Initialize buttons
let showDays = document.getElementById("showDays");
let changeDate = document.getElementById("changeDate");
// When the button is clicked, inject showDaysLeft and resetDate into current page
showDays.addEventListener("click", async () => {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: showDaysLeft,
});
});
changeDate.addEventListener("click", async () => {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: resetDate,
});
});
Note: Apart from the injected listeners other functions/variables cannot be directly run.