Skip to content

Commit

Permalink
Use offline sites.json if API is down
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitrrine committed Mar 8, 2025
1 parent d58d111 commit 36c44f8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/fetch-sites.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Fetch JSON from API

on:
schedule:
- cron: "0 * * * *" # Runs every 24 hours (adjust UTC if needed)
workflow_dispatch: # Allows manual triggering

jobs:
update-json:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Fetch JSON
run: |
curl -sS https://smc-api.lodine.xyz/sites -o data/sites.json || echo "API is down, using last known copy"
- name: Check for changes
id: check_changes
run: |
git diff --quiet || echo "CHANGED=true" >> $GITHUB_ENV
- name: Commit and push if changed
if: env.CHANGED == 'true'
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git add data/sites.json
git commit -m "Update sites.json (Automated)"
git push
20 changes: 18 additions & 2 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// StopMalwareContent's Source Code for Firefox extension
// Inspired from https://github.com/StopModReposts/Extension
const API_URL = "https://smc-api.lodine.xyz/sites";
const OFFLINE_COPY_URL = "data/sites.json";

let cachedSites = [];
let ignoreList = [];
let lastBlockedSite = {
Expand All @@ -15,9 +17,23 @@ refreshCache();

function refreshCache() {
fetch(API_URL)
.then((response) => response.json())
.then((response) => {
cachedSites = response;
if (!response.ok) throw new Error("API unreachable");
return response.json();
})
.then((data) => {
cachedSites = data;
})
.catch(() => {
console.warn("Using offline copy due to API failure");
return fetch(OFFLINE_COPY_URL)
.then((response) => response.json())
.then((data) => {
cachedSites = data;
})
.catch(() => {
console.error("Failed to load offline copy");
});
});
}

Expand Down
1 change: 1 addition & 0 deletions data/sites.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "smc-extension",
"version": "1.4.1",
"version": "1.4.2",
"description": "Restricts access to harmful or unwanted web resources.",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down

0 comments on commit 36c44f8

Please sign in to comment.