Skip to content

Commit

Permalink
Code Split: Weather, UserText, browser-utils (#494)
Browse files Browse the repository at this point in the history
* Code Split

* ID changed

* fix browserUtils

* minor changes

* Update browser-utils.js

Typo fix and isSafari more robust

* Update ai-tools.js

Reverted to avoid conflicts with #495

* Update index.html

* Update script.js

fix

* Fix browser-utils.js

* Update bookmarks.js

* Update script.js

* Update script.js

* Update browser-utils.js

final

* Create background.js

* Update manifest(firefox).json
  • Loading branch information
prem-k-r authored Jan 26, 2025
1 parent e5705d1 commit aaa992a
Show file tree
Hide file tree
Showing 17 changed files with 448 additions and 401 deletions.
File renamed without changes
17 changes: 10 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
<script defer src="scripts/save-load-states.js"></script>
<script defer src="scripts/wallpaper.js"></script>
<script defer src="scripts/clock.js"></script>
<script defer src="scripts/weather.js"></script>
<script defer src="scripts/custom-text.js"></script>
<script defer src="scripts/browser-utils.js"></script>
<script defer src="scripts/script.js"></script>
<script defer src="scripts/ai-tools.js"></script>
<script defer src="scripts/bookmarks.js"></script>
<script defer src="scripts/google-app-menu.js"></script>
<script defer src="scripts/todo-list.js"></script>
<script defer src="scripts/google-apps.js"></script>
<script defer src="scripts/voice-search.js"></script>
<script defer src="scripts/backup-restore.js"></script>

Expand Down Expand Up @@ -59,7 +62,7 @@

<body>
<!----------------------- Loading Screen Starting ----------------------->
<img src="./docs/Loading.png" id="LoadingScreen" fetchpriority="high" />
<img src="./images/Loading.png" id="LoadingScreen" fetchpriority="high" />

<!----------------------- ToDo List Setup Starting ----------------------->
<!-- ToDo Icon -->
Expand Down Expand Up @@ -601,7 +604,7 @@ <h3 id="bookmarkViewAs">View as</h3>
<div class="humidityBar">
<div class="thinLine"></div>
<div class="slider" id="slider">
<div id="humidityLevel">Humidity</div>
<div id="humidityLevel"></div>
</div>
</div>

Expand All @@ -616,7 +619,7 @@ <h3 id="bookmarkViewAs">View as</h3>
</svg>

</div>
<span id="feelsLike" style="padding-inline-end: 12px;">Seems</span>
<span id="feelsLike" style="padding-inline-end: 12px;"></span>
</div>
<div class="tiles location">
<div class="icon">
Expand All @@ -628,7 +631,7 @@ <h3 id="bookmarkViewAs">View as</h3>
</svg>

</div>
<span class="location_spn" id="location">Earth</span>
<span class="location_spn" id="location"></span>
</div>
</div>
<!-- end of tilesContainer -->
Expand Down Expand Up @@ -845,10 +848,10 @@ <h3 id="bookmarkViewAs">View as</h3>
<!-- -----------AI-Tools------------------ -->
<div class="aiToolsCont" id="aiToolsCont">

<div class="hangno" id="0NIHK">
<div class="hangno" id="aiToolsIcon">
<div class="icon">
<svg fill="none" height="100%" viewBox="0 0 280 280" width="100%" xmlns="http://www.w3.org/2000/svg">
<path id="HangNoAlive" class="accentColor" clip-rule="evenodd"
<path class="accentColor" clip-rule="evenodd"
d="M152 178C152 163.641 163.641 152 178 152H254C268.359 152 280.275 163.76 277.393 177.827C267.155 227.797 227.797 267.155 177.827 277.393C163.76 280.275 152 268.359 152 254V178ZM128 183.26C128 206.423 99.9943 218.024 83.6152 201.645L8.78205 126.811C3.43206 121.461 0.529946 114.017 1.78833 106.556C10.5659 54.5162 50.7487 13.1442 102.17 2.60743C116.237 -0.275091 128 11.6406 128 26V183.26ZM0 214.74C0 191.577 28.0057 179.976 44.3847 196.355L127.994 279.964C127.998 279.968 128 279.974 128 279.979V279.979C128 279.991 127.991 280 127.979 280H26C11.6406 280 0 268.359 0 254V214.74ZM152 26C152 11.6406 163.641 0 178 0H254C268.359 0 280 11.6406 280 26V102C280 116.359 268.359 128 254 128H178C163.641 128 152 116.359 152 102V26Z"
fill="white" fill-rule="evenodd" />
</svg>
Expand Down
2 changes: 1 addition & 1 deletion manifest(firefox).json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"https://search.brave.com/api/suggest*"
],
"background": {
"scripts": ["background.js"],
"scripts": ["scripts/background.js"],
"persistent": true
},
"icons": {
Expand Down
8 changes: 3 additions & 5 deletions scripts/ai-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* If not, see <https://www.gnu.org/licenses/>.
*/

// when User click on "AI-Tools"
// When User click on "AI-Tools"
const element = document.getElementById("toolsCont");
const shortcuts = document.getElementById("shortcutsContainer");

Expand Down Expand Up @@ -68,11 +68,9 @@ document.addEventListener("click", (event) => {
}
});

document.getElementById("0NIHK").onclick = toggleShortcuts;

//

document.getElementById("aiToolsIcon").onclick = toggleShortcuts;

// Save and load checkbox state
document.addEventListener("DOMContentLoaded", function () {
const aiToolsCont = document.getElementById("aiToolsCont");
const aiToolsCheckbox = document.getElementById("aiToolsCheckbox");
Expand Down
1 change: 1 addition & 0 deletions scripts/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 1 addition & 1 deletion scripts/backup-restore.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,4 @@ resetbtn.addEventListener("click", () => {
localStorage.clear();
location.reload();
}
});
});
16 changes: 9 additions & 7 deletions scripts/bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,19 +279,19 @@ document.addEventListener("DOMContentLoaded", function () {
// Open in a new tab
event.preventDefault();
if (isFirefox) {
browser.tabs.create({url: node.url, active: false});
} else if (isChrome) {
chrome.tabs.create({url: node.url, active: false});
browser.tabs.create({ url: node.url, active: false });
} else if (isChromiumBased) {
chrome.tabs.create({ url: node.url, active: false });
} else {
window.open(node.url, "_blank");
}
} else {
// Open in the current tab
event.preventDefault();
if (isFirefox) {
browser.tabs.update({url: node.url});
} else if (isChrome) {
chrome.tabs.update({url: node.url}, function () {
browser.tabs.update({ url: node.url });
} else if (isChromiumBased) {
chrome.tabs.update({ url: node.url }, function () {
});
} else {
window.location.href = node.url;
Expand All @@ -312,6 +312,7 @@ document.addEventListener("DOMContentLoaded", function () {

// ------------------------ End of Bookmark System -----------------------------------

// Save and load the state of the bookmarks toggle
document.addEventListener("DOMContentLoaded", function () {
const bookmarksCheckbox = document.getElementById("bookmarksCheckbox");
const bookmarkGridCheckbox = document.getElementById("bookmarkGridCheckbox");
Expand All @@ -320,7 +321,7 @@ document.addEventListener("DOMContentLoaded", function () {
let bookmarksPermission;
if (isFirefox && browser.permissions) {
bookmarksPermission = browser.permissions;
} else if (isChrome || isEdge || isBrave && chrome.permissions) {
} else if (isChromiumBased && chrome.permissions) {
bookmarksPermission = chrome.permissions;
} else {
alert(translations[currentLanguage]?.UnsupportedBrowser || translations["en"].UnsupportedBrowser);
Expand Down Expand Up @@ -380,6 +381,7 @@ document.addEventListener("DOMContentLoaded", function () {
loadCheckboxState("bookmarkGridCheckboxState", bookmarkGridCheckbox);
})

// Keyboard shortcut for bookmarks
document.addEventListener("keydown", function (event) {
if (event.key === "ArrowRight" && event.target.tagName !== "INPUT" && event.target.tagName !== "TEXTAREA" && event.target.isContentEditable !== true) {
if (bookmarksCheckbox.checked) {
Expand Down
36 changes: 36 additions & 0 deletions scripts/browser-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Material You NewTab
* Copyright (c) 2023-2025 XengShi
* Licensed under the GNU General Public License v3.0 (GPL-3.0)
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/

// Constants to detect the browser and platform details

// Check if the browser is Firefox
const isFirefox = typeof browser !== "undefined" || navigator.userAgent.toLowerCase().includes("firefox");

// Check if the browser is Chromium-based
const isChromiumBased = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime) && !isFirefox;

// Check if the browser is Edge
const isEdge = /Edg/.test(navigator.userAgent);

// Check if the browser is Brave
const isBrave = !!(navigator.brave && navigator.brave.isBrave());

// Check if the browser is Opera
const isOpera = /OPR/.test(navigator.userAgent);

// Check if the browser is Chrome
const isChrome = (/Chrome|CriOS/.test(navigator.userAgent)) && /Google Inc/.test(navigator.vendor) && !isEdge && !isBrave && !isOpera;

// Check if the browser is Safari
const isSafari = /Safari/.test(navigator.userAgent) && !isChromiumBased && /Apple Computer/.test(navigator.vendor);

// Check if the operating system is macOS
const isMac = /Macintosh|MacIntel|MacPPC|Mac68K/.test(navigator.platform);

// Check if the device is a desktop (not mobile)
const isDesktop = !/Android|iPhone|iPad|iPod/.test(navigator.userAgent);
3 changes: 2 additions & 1 deletion scripts/clock.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ document.addEventListener("DOMContentLoaded", function () {

// ----------------------- End of clock display -------------------------

// Save and load toggle state
document.addEventListener("DOMContentLoaded", function () {
const timeformatField = document.getElementById("timeformatField");
const hourcheckbox = document.getElementById("12hourcheckbox");
Expand Down Expand Up @@ -376,4 +377,4 @@ document.addEventListener("DOMContentLoaded", function () {
loadCheckboxState("hourcheckboxState", hourcheckbox);
loadActiveStatus("timeformatField", timeformatField);
loadActiveStatus("greetingField", greetingField);
});
});
58 changes: 58 additions & 0 deletions scripts/custom-text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Material You NewTab
* Copyright (c) 2023-2025 XengShi
* Licensed under the GNU General Public License v3.0 (GPL-3.0)
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/

// Custom text
document.addEventListener("DOMContentLoaded", () => {
const userTextDiv = document.getElementById("userText");
const userTextCheckbox = document.getElementById("userTextCheckbox");

// Load and apply the checkbox state
const isUserTextVisible = localStorage.getItem("userTextVisible") !== "false";
userTextCheckbox.checked = isUserTextVisible;
userTextDiv.style.display = isUserTextVisible ? "block" : "none";

// Toggle userText display based on checkbox state
userTextCheckbox.addEventListener("change", () => {
const isVisible = userTextCheckbox.checked;
userTextDiv.style.display = isVisible ? "block" : "none";
localStorage.setItem("userTextVisible", isVisible);
});

// Set the default language to English if no language is saved
const savedLang = localStorage.getItem("selectedLanguage") || "en";
applyLanguage(savedLang);

// Load the stored text if it exists
const storedValue = localStorage.getItem("userText");
if (storedValue) {
userTextDiv.textContent = storedValue;
} else {
// Fallback to the placeholder based on the selected language
const placeholder = userTextDiv.dataset.placeholder || translations["en"].userText; // Fallback to English
userTextDiv.textContent = placeholder;
}

// Handle input event
userTextDiv.addEventListener("input", function () {
localStorage.setItem("userText", userTextDiv.textContent);
});

// Remove placeholder text when the user starts editing
userTextDiv.addEventListener("focus", function () {
if (userTextDiv.textContent === userTextDiv.dataset.placeholder) {
userTextDiv.textContent = ""; // Clear the placeholder when focused
}
});

// Restore placeholder if the user leaves the div empty after editing
userTextDiv.addEventListener("blur", function () {
if (userTextDiv.textContent === "") {
userTextDiv.textContent = userTextDiv.dataset.placeholder; // Show the placeholder again if empty
}
});
});
5 changes: 2 additions & 3 deletions scripts/google-app-menu.js → scripts/google-apps.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ document.addEventListener("click", function (event) {
});
// ------------------------End of Google App Menu Setup-----------------------------------

// Toggle

// Save and load toggle state
document.addEventListener("DOMContentLoaded", function () {
const googleAppsCont = document.getElementById("googleAppsCont");

Expand All @@ -56,4 +55,4 @@ document.addEventListener("DOMContentLoaded", function () {
});
loadCheckboxState("googleAppsCheckboxState", googleAppsCheckbox);
loadDisplayStatus("googleAppsDisplayStatus", googleAppsCont);
});
});
2 changes: 1 addition & 1 deletion scripts/languages.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Translation data
const translations = {
en: en, // English
pt: pt, // Portuguese-BR
pt: pt, // Portuguese-BR (Brazil)
zh: zh, // Chinese (Simplified)
hi: hi, // Hindi
hu: hu, // Hungarian
Expand Down
10 changes: 9 additions & 1 deletion scripts/preload.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
/*
* Material You NewTab
* Copyright (c) 2023-2025 XengShi
* Licensed under the GNU General Public License v3.0 (GPL-3.0)
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/

// Set Loading Screen Color before Everything Loads
document.documentElement.style.setProperty('--Loading-Screen-Color',localStorage.getItem('LoadingScreenColor') || "#bbd6fd");
document.documentElement.style.setProperty('--Loading-Screen-Color', localStorage.getItem('LoadingScreenColor') || "#bbd6fd");
Loading

0 comments on commit aaa992a

Please sign in to comment.