Add files via upload

This commit is contained in:
Kenneth Hendricks 2024-10-16 21:05:49 -04:00 committed by GitHub
parent 543198e9b9
commit e1a6e6fd5e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 287 additions and 0 deletions

BIN
firefox_addon_image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

123
js/background.js Normal file
View file

@ -0,0 +1,123 @@
const filterListURL =
"https://raw.githubusercontent.com/fmhy/FMHYFilterlist/main/filterlist-domains.txt";
let unsafeSites = [];
let potentiallyUnsafeSites = [];
// Fetch the filter list
function fetchFilterList() {
console.log("Fetching filter list...");
fetch(filterListURL)
.then((response) => response.text())
.then((text) => {
console.log("Filter list fetched successfully!");
const lines = text.split("\n");
let isPotentiallyUnsafeSection = false; // Track whether we're in the "potentially unsafe" section
lines.forEach((line) => {
// Ignore comments and blank lines
if (line.startsWith("#")) {
if (line.includes("not recommended/potentially unsafe")) {
isPotentiallyUnsafeSection = true;
}
} else if (line.trim()) {
const domain = line.trim(); // Trim whitespace
if (isPotentiallyUnsafeSection) {
potentiallyUnsafeSites.push(domain); // Add to potentially unsafe sites
} else {
unsafeSites.push(domain); // Add to unsafe sites
}
}
});
console.log("Parsed Unsafe Sites:", unsafeSites); // Check if unsafe sites are populated
console.log("Parsed Potentially Unsafe Sites:", potentiallyUnsafeSites); // Check potentially unsafe sites
})
.catch((error) => console.error("Error fetching filter list:", error));
}
// Update the toolbar icon based on the site's status
function updateIcon(status, tabId) {
let iconPath = "res/ext_icon_144.png"; // Default extension icon for unknown sites
if (status === "safe") {
iconPath = "res/icons/safe.png"; // Icon for safe sites
} else if (status === "unsafe") {
iconPath = "res/icons/unsafe.png"; // Icon for unsafe sites
} else if (status === "potentially_unsafe") {
iconPath = "res/icons/potentially_unsafe.png"; // Icon for potentially unsafe sites
}
browser.browserAction.setIcon({
path: iconPath,
tabId: tabId,
});
}
// Check the site status for a given tab and URL
function checkSiteAndUpdateIcon(tabId, url) {
if (!url) return;
const currentUrl = new URL(url).hostname.replace("www.", "");
console.log("Checking site status for toolbar icon:", currentUrl);
// Check if the site is unsafe or potentially unsafe
let isUnsafe = unsafeSites.some((site) => currentUrl.includes(site));
let isPotentiallyUnsafe = potentiallyUnsafeSites.some((site) =>
currentUrl.includes(site)
);
if (isUnsafe) {
console.log("Updating toolbar icon to unsafe for:", currentUrl);
updateIcon("unsafe", tabId); // Update the toolbar icon to unsafe
} else if (isPotentiallyUnsafe) {
console.log("Updating toolbar icon to potentially unsafe for:", currentUrl);
updateIcon("potentially_unsafe", tabId); // Update the toolbar icon to potentially unsafe
} else {
console.log("No data for this site:", currentUrl);
updateIcon("default", tabId); // Use default extension icon for unknown sites
}
}
// Listen for messages from the popup
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "checkSiteStatus") {
const currentUrl = message.url;
console.log("Checking site status for popup:", currentUrl);
// Check if the site is unsafe or potentially unsafe
let isUnsafe = unsafeSites.some((site) => currentUrl.includes(site));
let isPotentiallyUnsafe = potentiallyUnsafeSites.some((site) =>
currentUrl.includes(site)
);
if (isUnsafe) {
sendResponse({ status: "unsafe", url: currentUrl });
} else if (isPotentiallyUnsafe) {
sendResponse({ status: "potentially_unsafe", url: currentUrl });
} else {
console.log("No data for this site:", currentUrl);
sendResponse({ status: "no_data", url: currentUrl }); // Return no data status
}
}
return true; // Indicates we will respond asynchronously
});
// Listen for when a tab is updated (e.g., new URL loaded)
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === "complete" && tab.url) {
checkSiteAndUpdateIcon(tabId, tab.url);
}
});
// Listen for when a tab is activated (e.g., tab switched)
browser.tabs.onActivated.addListener((activeInfo) => {
browser.tabs.get(activeInfo.tabId, (tab) => {
if (tab.url) {
checkSiteAndUpdateIcon(tab.id, tab.url);
}
});
});
// Fetch the filter list when the extension is loaded
fetchFilterList();

26
manifest.json Normal file
View file

@ -0,0 +1,26 @@
{
"manifest_version": 2,
"name": "FMHY SafeGuard",
"version": "1.0.0",
"icons": {
"128": "res/ext_icon_144.png"
},
"browser_action": {
"default_icon": {
"128": "res/ext_icon_144.png"
},
"default_popup": "pub/index.html"
},
"permissions": [
"activeTab",
"<all_urls>",
"webRequest",
"webRequestBlocking",
"storage",
"notifications"
],
"background": {
"scripts": ["js/background.js"],
"persistent": true
}
}

93
pub/index.html Normal file
View file

@ -0,0 +1,93 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FMHY SafeGuard</title>
<style>
body {
text-align: center;
padding-bottom: 10px;
color: #848a94;
background-color: rgb(26, 26, 26);
font-family: "Helvetica Neue", "Helvetica", "Arial", sans-serif;
}
a {
color: #E8E8E8;
text-decoration: none;
}
a:hover {
color: #D04343;
}
.title h1 {
font-size: 36px;
background: -webkit-linear-gradient(120deg, #c4b5fd 30%, #7bc5e4);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
text-fill-color: transparent;
margin-left: 25px;
margin-right: 25px;
}
#status-container {
margin-top: 20px;
}
.status-image {
width: 100px;
height: 100px;
}
#status-message {
font-size: 18px;
margin-top: 10px;
color: #E8E8E8;
}
#error-message {
color: red;
}
#footer {
font-size: 12px;
margin-top: 20px;
color: #848a94;
}
#footer a {
color: #78b3e2;
text-decoration: none;
}
#footer a:hover {
color: #78b3e2;
text-decoration: underline;
}
</style>
</head>
<body>
<div class="title">
<h1>FMHY SafeGuard</h1>
</div>
<div id="status-container">
<!-- The status icon will be dynamically updated by the JS -->
<img id="status-icon" class="status-image" src="safe.png" alt="Status Icon">
<!-- The status message will also be updated -->
<p id="status-message">Checking site status...</p>
</div>
<!-- An area for error messages, if necessary -->
<p id="error-message"></p>
<div id="footer">
<p>Created using <a href="https://github.com/fmhy/FMHYFilterlist" target="_blank">FMHY Filterlists</a></p>
</div>
<script src="index.js"></script>
</body>
</html>

45
pub/index.js Normal file
View file

@ -0,0 +1,45 @@
document.addEventListener('DOMContentLoaded', function() {
console.log("Popup loaded, attempting to get site status...");
const statusIcon = document.getElementById('status-icon');
const statusMessage = document.getElementById('status-message');
const errorMessage = document.getElementById('error-message');
// Query the active tab to get its URL
browser.tabs.query({active: true, currentWindow: true}, function(tabs) {
if (tabs.length === 0) {
console.error("No active tab found.");
statusMessage.textContent = "Error: No active tab found.";
return;
}
const currentUrl = new URL(tabs[0].url).hostname.replace('www.', '');
console.log("Sending message to background to check site status for:", currentUrl);
// Send a message to the background script to check the site's status
browser.runtime.sendMessage({action: "checkSiteStatus", url: tabs[0].url}, function(response) {
if (!response) {
console.error("No response from background script.");
statusMessage.textContent = "Error: Could not retrieve site status.";
return;
}
if (response.status === "unsafe") {
console.log("Popup: Site is unsafe:", currentUrl);
// Update popup for unsafe site
statusIcon.src = '../res/icons/unsafe.png'; // Update icon to unsafe
statusMessage.textContent = `${currentUrl} is unsafe. Be cautious!`; // Update message
} else if (response.status === "potentially_unsafe") {
console.log("Popup: Site is potentially unsafe:", currentUrl);
// Update popup for potentially unsafe site
statusIcon.src = '../res/icons/potentially_unsafe.png'; // Update icon to potentially unsafe
statusMessage.textContent = `${currentUrl} is potentially unsafe. Be cautious!`; // Update message
} else if (response.status === "no_data") {
console.log("Popup: No data for this site:", currentUrl);
// Update popup for no data
statusIcon.src = '../res/ext_icon_144.png'; // Default extension icon
statusMessage.textContent = "There is no data for this site yet."; // Update message for no data
}
});
});
});

BIN
res/ext_icon_144.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1 KiB

BIN
res/icons/safe.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

BIN
res/icons/unsafe.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB