Skip to content

Commit

Permalink
Update index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
kenhendricks00 authored Nov 3, 2024
1 parent aba7d2c commit e43f4b0
Showing 1 changed file with 80 additions and 41 deletions.
121 changes: 80 additions & 41 deletions platform/chromium/pub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,78 @@ document.addEventListener("DOMContentLoaded", () => {
return `${urlObj.protocol}//${urlObj.hostname}`;
}

try {
// Get the active tab's URL
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const activeTab = tabs[0];

if (!activeTab || !activeTab.url) {
throw new Error("No active tab found or URL is unavailable.");
// Function to apply theme based on settings
function applyTheme() {
chrome.storage.sync.get("theme", ({ theme }) => {
if (theme === "dark") {
document.body.setAttribute("data-theme", "dark");
} else if (theme === "light") {
document.body.setAttribute("data-theme", "light");
} else {
const prefersDark = window.matchMedia(
"(prefers-color-scheme: dark)"
).matches;
document.body.setAttribute(
"data-theme",
prefersDark ? "dark" : "light"
);
}
});
}

const currentUrl = normalizeUrl(activeTab.url);
const rootDomain = extractRootDomain(currentUrl);
console.log(`Active tab URL: ${currentUrl}, Root domain: ${rootDomain}`);

// Send a message to the background script to check the site's status
chrome.runtime.sendMessage(
{ action: "checkSiteStatus", url: currentUrl },
(response) => {
if (!response || !response.status) {
throw new Error(
"Failed to retrieve site status from the background script."
);
}
// Apply theme on load
applyTheme();

// Determine if the root domain or the full URL is marked as safe/starred
chrome.runtime.sendMessage(
{ action: "checkSiteStatus", url: rootDomain },
(isRootDomainMarked) => {
// Handle different site statuses and update the UI accordingly
if (
isRootDomainMarked.status === response.status &&
response.status !== "no_data"
) {
// If both the root domain and the current URL share the same status, show the root domain in the message
handleStatusUpdate(response.status, rootDomain);
} else {
// Otherwise, show the current URL in the message
handleStatusUpdate(response.status, currentUrl);
}
}
// Get the active tab's URL
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (chrome.runtime.lastError || !tabs[0] || !tabs[0].url) {
handleError("No active tab found or URL is unavailable.");
return;
}

const currentUrl = normalizeUrl(tabs[0].url);
const rootDomain = extractRootDomain(currentUrl);
console.log(`Active tab URL: ${currentUrl}, Root domain: ${rootDomain}`);

// Send a message to the background script to check the site's status
chrome.runtime.sendMessage(
{ action: "checkSiteStatus", url: currentUrl },
(response) => {
if (chrome.runtime.lastError || !response || !response.status) {
handleError(
"Failed to retrieve site status from the background script."
);
return;
}
);
});
} catch (error) {
console.error("Error while checking site status:", error);
errorMessage.textContent = `Error: ${error.message}`;

// Determine if the root domain or the full URL is marked as safe/starred
chrome.runtime.sendMessage(
{ action: "checkSiteStatus", url: rootDomain },
(isRootDomainMarked) => {
if (chrome.runtime.lastError || !isRootDomainMarked) {
handleError("Failed to check root domain status.");
return;
}

if (
isRootDomainMarked.status === response.status &&
response.status !== "no_data"
) {
// If both the root domain and the current URL share the same status, show the root domain in the message
handleStatusUpdate(response.status, rootDomain);
} else {
// Otherwise, show the current URL in the message
handleStatusUpdate(response.status, currentUrl);
}
}
);
}
);
});

function handleError(message) {
console.error(message);
errorMessage.textContent = `Error: ${message}`;
updateUI("error", "An error occurred while retrieving the site status.");
}

Expand All @@ -87,6 +113,12 @@ document.addEventListener("DOMContentLoaded", () => {
`${displayUrl} is <strong>potentially unsafe</strong>. Proceed with caution.`
);
break;
case "fmhy":
updateUI(
"fmhy",
`${displayUrl} is an <strong>FMHY</strong> related site. Proceed confidently.`
);
break;
case "safe":
updateUI("safe", `${displayUrl} is <strong>safe</strong> to browse.`);
break;
Expand Down Expand Up @@ -119,6 +151,7 @@ document.addEventListener("DOMContentLoaded", () => {
const icons = {
unsafe: "../res/icons/unsafe.png",
potentially_unsafe: "../res/icons/potentially_unsafe.png",
fmhy: "../res/icons/fmhy.png",
safe: "../res/icons/safe.png",
starred: "../res/icons/starred.png",
no_data: "../res/ext_icon_144.png",
Expand All @@ -136,4 +169,10 @@ document.addEventListener("DOMContentLoaded", () => {

console.log(`UI updated: ${message}`);
}

// Add settings button functionality
document.getElementById("settingsButton").addEventListener("click", () => {
// Open the settings page in a new tab
chrome.runtime.openOptionsPage();
});
});

0 comments on commit e43f4b0

Please sign in to comment.