Skip to content

Commit

Permalink
proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
strukturart committed Dec 16, 2024
1 parent 650009e commit 910a86b
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 52 deletions.
115 changes: 81 additions & 34 deletions application/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import "regenerator-runtime/runtime";

// Extend dayjs with the duration plugin
dayjs.extend(duration);

let articles = [];

const sw_channel = new BroadcastChannel("sw-messages");

const parser = new fxparser.XMLParser({
Expand Down Expand Up @@ -88,14 +91,14 @@ if (userAgent && userAgent.includes("KAIOS")) {
}

let current_article = "";
const proxy = "https://corsproxy.io/?";
const proxy = "https://api.cors.lol/?url=";

let default_settings = {
"opml_url":
"https://raw.githubusercontent.com/strukturart/feedolin/master/example.opml",
"opml_local": "",
"proxy_url": "https://corsproxy.io/?",
"cache_time": 1000,
"proxy_url": "https://api.cors.lol/?url=",
"cache_time": 3600000,
};
//store all articles id to compare
let articlesID = [];
Expand All @@ -120,14 +123,9 @@ localforage
});

let reload_data = () => {
articles = [];
localforage.setItem("last_channel_filter", channel_filter).then(() => {
side_toaster("load data", 3000);

start_loading();
});

setTimeout(() => {}, 3000);
};

function add_read_article(id) {
Expand Down Expand Up @@ -170,8 +168,6 @@ if (!status.notKaiOS) {
});
}

let articles = [];

if (status.debug) {
window.onerror = function (msg, url, linenumber) {
alert(
Expand Down Expand Up @@ -347,32 +343,55 @@ let raw = (i) => {

const fetchOPML = (url) => {
let t = url;
if (status.notKaiOS) t = settings.proxy_url + url;

const xhr = new XMLHttpRequest({ "mozSystem": true });
let xhr = null;
if (status.notKaiOS) {
t = settings.proxy_url + url;
xhr = new XMLHttpRequest();
} else {
xhr = new XMLHttpRequest({ "mozSystem": true });
}

xhr.open("GET", t, true);
xhr.setRequestHeader("Accept", "application/xml");

xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 300) {
// Success
side_toaster("Data loaded successfully", 3000);
load_feeds(xhr.responseText);
} else {
// HTTP error handling
console.log(`HTTP error! Status: ${xhr.status}`);
handleHttpError(xhr.status);
}
};

xhr.onerror = function (error) {
m.route.set("/start");

side_toaster("Error fetching the OPML file" + error, 4000);
xhr.onerror = function () {
handleRequestError();
};

xhr.send();
};

const handleHttpError = (status) => {
console.error(`HTTP Error: Status ${status}`);
side_toaster("OPML file not reachable", 4000);

// Route back to start if on intro
let r = m.route.get();
if (r.startsWith("/intro")) {
m.route.set("/start");
}
};

const handleRequestError = () => {
console.error("Network error occurred during the request.");
side_toaster("OPML file not reachable", 3000);

// Route back to start if on intro
let r = m.route.get();
if (r.startsWith("/intro")) {
m.route.set("/start");
}
};

const load_feeds = async (data) => {
if (data) {
let downloadList;
Expand All @@ -392,7 +411,7 @@ const load_feeds = async (data) => {
settings.last_update = new Date();
localforage.setItem("settings", settings);
} catch (error) {
alert(error);
console.log(error);
}
} else {
alert("Generated download list is empty.");
Expand Down Expand Up @@ -447,6 +466,8 @@ const generateDownloadList = (data) => {
};

const fetchContent = async (feed_download_list) => {
articles = [];

let completedFeeds = 0; // Counter to track how many feeds have finished loading
const totalFeeds = feed_download_list.length; // Total number of feeds to be fetched

Expand All @@ -456,8 +477,6 @@ const fetchContent = async (feed_download_list) => {
const checkIfAllFeedsLoaded = () => {
channel_filter = localStorage.getItem("last_channel_filter");
if (completedFeeds === totalFeeds) {
m.route.set("/start");

console.log("All feeds are loaded");
// All feeds are done loading, you can proceed with further actions
//cache data
Expand All @@ -484,6 +503,9 @@ const fetchContent = async (feed_download_list) => {
.catch((err) => {
console.error("Feeds cached", err);
});

let r = m.route.get();
if (r.startsWith("/intro")) m.route.set("/start");
}
};

Expand Down Expand Up @@ -561,7 +583,7 @@ const fetchContent = async (feed_download_list) => {
});
} else {
let xhr = new XMLHttpRequest({ "mozSystem": true });
xhr.timeout = 5000;
xhr.timeout = 2000;

let url = e.url;
if (status.notKaiOS) {
Expand Down Expand Up @@ -818,13 +840,12 @@ localforage
.setItem("settings", settings)
.then(function (value) {})
.catch(function (err) {
// This code runs if there were any errors
console.log(err);
});
}
settings = value;
//todo set value in settings view, default is 1sec
settings.cache_time = settings.cache_time || 1000;
//todo set value in settings view, default is 1h
settings.cache_time = settings.cache_time || 3600000;

if (settings.last_update) {
status.last_update_duration =
Expand Down Expand Up @@ -1534,6 +1555,9 @@ if ("b2g" in navigator) {
console.log(e);
}
}
if (navigator.mozAlarms) {
globalAudioElement.mozAudioChannelType = "content";
}

let hasPlayedAudio = [];

Expand Down Expand Up @@ -1642,7 +1666,6 @@ const AudioPlayerView = {
document
.querySelector("div.button-left")
.addEventListener("click", function () {
alert("j");
status.sleepTimer
? stopTimer()
: startTimer(settings.sleepTimer * 60 * 1000);
Expand Down Expand Up @@ -2209,6 +2232,8 @@ var settingsView = {
},
},
[
m("option", { value: "1" }, "1"),
m("option", { value: "5" }, "5"),
m("option", { value: "10" }, "10"),
m("option", { value: "20" }, "20"),
m("option", { value: "30" }, "30"),
Expand Down Expand Up @@ -2786,8 +2811,6 @@ document.addEventListener("DOMContentLoaded", function (e) {
status.visibility = true;
let dif = new Date() / 1000 - settings.last_update / 1000;
if (dif > settings.cache_time) {
articles = [];

side_toaster("load new content", 4000);
start_loading();
}
Expand All @@ -2805,6 +2828,7 @@ window.addEventListener("offline", () => {
});

window.addEventListener("beforeunload", (event) => {
localforage.setItem("last_channel_filter", channel_filter).then(() => {});
const entries = window.performance.getEntriesByType("navigation");

// For older browsers (fallback)
Expand All @@ -2821,7 +2845,6 @@ window.addEventListener("beforeunload", (event) => {
// Prevent the reload or display a confirmation dialog
event.preventDefault();

articles = [];
side_toaster("load new content", 4000);
start_loading();

Expand Down Expand Up @@ -2874,6 +2897,12 @@ try {
} catch (e) {}

//worker sleep mode
if (navigator.mozAlarms) {
navigator.mozSetMessageHandler("alarm", function (mozAlarm) {
globalAudioElement.pause();
status.sleepTimer = false;
});
}

let worker;

Expand All @@ -2884,14 +2913,32 @@ try {
}

function startTimer(timerDuration) {
worker.postMessage({ action: "start", duration: timerDuration });
side_toaster("sleep mode on", 3000);
//KaiOS2
if (navigator.mozAlarms) {
let sleepDuration = settings.sleepTimer * 60 * 1000; // Convert minutes to milliseconds
let targetTime = new Date(Date.now() + sleepDuration); // Add duration to current time

var request = navigator.mozAlarms.add(targetTime, "honorTimezone");

request.onsuccess = function () {
status.alarmId = this.result;
};
}

status.sleepTimer = true;
side_toaster("sleep mode on", 3000);

worker.postMessage({ action: "start", duration: timerDuration });
}

function stopTimer() {
worker.postMessage({ action: "stop" });
if (navigator.mozAlarms) {
navigator.mozAlarms.remove(status.alarmId);
}
status.sleepTimer = false;

side_toaster("sleep mode off", 3000);
worker.postMessage({ action: "stop" });
}

worker.onmessage = function (event) {
Expand Down
2 changes: 1 addition & 1 deletion application/manifest.webapp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.0.15",
"version": "2.0.17",
"name": "feedolin",
"description": "Feedolin is an RSS / Atom / Mastodon reader and podcast player. It is intended for users who already use an rss reader client and want to read their feeds on a kaios device. the list of subscribed websites / podcasts is managed locally or online in an opml file.",
"launch_path": "/index.html",
Expand Down
4 changes: 1 addition & 3 deletions application/manifest.webmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@
],

"b2g_features": {
"version": "1.8.128",
"version": "1.8.132",
"id": "feedolin",
"subtitle": "RSS Reader and Mastodon Reader",
"core": true,
"type": "privileged",
"display": "fullscreen",
"origin": "http://feedolin.localhost",

"developer": {
"name": "strukturart",
"url": "https://github.com/strukturart/feedolin"
Expand Down
2 changes: 1 addition & 1 deletion application/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ self.addEventListener("systemmessage", async (evt) => {
const userAgent = navigator.userAgent || "";

if (userAgent && !userAgent.includes("KAIOS")) {
const CACHE_NAME = "pwa-cache-v0.1181";
const CACHE_NAME = "pwa-cache-v0.1198";
const FILE_LIST_URL = "/file-list.json"; // URL of the JSON file containing the array of files

self.addEventListener("install", (event) => {
Expand Down
4 changes: 2 additions & 2 deletions docs/index.339e55c4.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/index.f0337e49.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/manifest.webapp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.0.15",
"version": "2.0.17",
"name": "feedolin",
"description": "Feedolin is an RSS / Atom / Mastodon reader and podcast player. It is intended for users who already use an rss reader client and want to read their feeds on a kaios device. the list of subscribed websites / podcasts is managed locally or online in an opml file.",
"launch_path": "/index.html",
Expand Down
8 changes: 2 additions & 6 deletions docs/manifest.webmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@
],

"b2g_features": {
"version": "1.8.125",
"version": "1.8.132",
"id": "feedolin",
"subtitle": "RSS Reader and Mastodon Reader",
"core": true,
"type": "privileged",
"display": "fullscreen",
"origin": "http://feedolin.localhost",

"developer": {
"name": "strukturart",
"url": "https://github.com/strukturart/feedolin"
Expand All @@ -58,9 +56,7 @@
"desktop-notification": {
"description": "Needed to fire system notifications"
},
"alarms": {
"description": "Required to schedule alarms"
},

"feature-detection": {
"description": "query which keys are available"
},
Expand Down
2 changes: 1 addition & 1 deletion docs/sw.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion example.opml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@


<outline text="Forum" title="Forum">
<outline type="rss" text="KaiOs Reddit" title="KaiOs Reddit" xmlUrl="https://www.reddit.com/r/KaiOS/.rss" htmlUrl=""/>
<outline type="rss" text="getKirby Forum" title="getKirby Forum" xmlUrl="https://forum.getkirby.com/latest.rss" htmlUrl=""/>
</outline>


<outline text="Redit" title="Redit">
<outline type="rss" text="KaiOs Reddit" title="KaiOs Reddit" xmlUrl="https://www.reddit.com/r/KaiOS/.rss" htmlUrl=""/>
<outline type="rss" text="dumbphone Reddit" title="dumbphone Reddit" xmlUrl="https://www.reddit.com/r/dumbphones/.rss" htmlUrl=""/>
</outline>

<outline text="Blog" title="Blog">
<outline type="rss" text="KaiOs Blog" title="KaiOs Reddit" xmlUrl="https://www.kaiostech.com/feed/" htmlUrl=""/>
</outline>
Expand Down
Loading

0 comments on commit 910a86b

Please sign in to comment.