diff --git a/materialious/android/app/capacitor.build.gradle b/materialious/android/app/capacitor.build.gradle index e2cef015..76030400 100644 --- a/materialious/android/app/capacitor.build.gradle +++ b/materialious/android/app/capacitor.build.gradle @@ -12,6 +12,7 @@ dependencies { implementation project(':capacitor-app') implementation project(':capacitor-browser') implementation project(':capgo-inappbrowser') + implementation project(':capacitor-nodejs') } diff --git a/materialious/android/app/src/main/AndroidManifest.xml b/materialious/android/app/src/main/AndroidManifest.xml index 6615e008..7cbefaa0 100644 --- a/materialious/android/app/src/main/AndroidManifest.xml +++ b/materialious/android/app/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" + android:usesCleartextTraffic="true" android:theme="@style/AppTheme"> { instanceStore.set(ensureNoTrailingSlash(invidiousInstance)); authStore.set(null); - goto('/'); + goto('/', { replaceState: true }); ui('#dialog-settings'); }} > diff --git a/materialious/src/lib/patches/androidRequests.ts b/materialious/src/lib/patches/androidRequests.ts new file mode 100644 index 00000000..2c6ae055 --- /dev/null +++ b/materialious/src/lib/patches/androidRequests.ts @@ -0,0 +1,41 @@ +import { goto } from "$app/navigation"; +import { Capacitor } from "@capacitor/core"; + +if (Capacitor.getPlatform() === 'android') { + const originalFetch = window.fetch; + + // CORS-Anywhere proxy URL + const corsAnywhereProxyUrl: string = 'http://localhost:3000/'; + + // Overwrite fetch to use CORS-Anywhere proxy + window.fetch = async (requestInput: RequestInfo | URL, requestOptions: RequestInit = {}): Promise => { + const requestUrl: string = + typeof requestInput === 'string' ? requestInput : requestInput.toString(); + + // Check if the URL is already proxied, to avoid double proxying + if (!requestUrl.startsWith(corsAnywhereProxyUrl)) { + requestInput = corsAnywhereProxyUrl + requestUrl; + } + + // Ensure options.method is set to a valid value + requestOptions.method = requestOptions.method ? requestOptions.method.toUpperCase() : 'GET'; + + // Use the original fetch with the proxied URL and options + return originalFetch(requestInput, requestOptions); + }; + + const currentOrigin: string = window.location.protocol + '//' + window.location.host; + const originalXhrOpen = XMLHttpRequest.prototype.open; + + XMLHttpRequest.prototype.open = function (...args: any[]): void { + const targetOriginMatch = /^https?:\/\/([^\/]+)/i.exec(args[1]); + if (targetOriginMatch && targetOriginMatch[0].toLowerCase() !== currentOrigin) { + args[1] = corsAnywhereProxyUrl + args[1]; + } + /* @ts-ignore */ + return originalXhrOpen.apply(this, args); + }; + + // Must reload page after patches + goto('/', { replaceState: true }); +} \ No newline at end of file diff --git a/materialious/src/routes/(app)/+layout.svelte b/materialious/src/routes/(app)/+layout.svelte index 72dc2ab2..331cc5a7 100644 --- a/materialious/src/routes/(app)/+layout.svelte +++ b/materialious/src/routes/(app)/+layout.svelte @@ -11,6 +11,7 @@ import SyncParty from '$lib/SyncParty.svelte'; import Thumbnail from '$lib/Thumbnail.svelte'; import { bookmarkletLoadFromUrl, loadSettingsFromEnv } from '$lib/externalSettings'; + import '$lib/patches/androidRequests'; import { activePageStore, authStore, diff --git a/materialious/static/nodejs-android/index.js b/materialious/static/nodejs-android/index.js new file mode 100644 index 00000000..db5bc1cc --- /dev/null +++ b/materialious/static/nodejs-android/index.js @@ -0,0 +1,5 @@ +const corsAnywhere = require('cors-anywhere'); + +corsAnywhere.createServer({ + originWhitelist: [], +}).listen(3000, 'localhost'); \ No newline at end of file diff --git a/materialious/static/nodejs-android/package.json b/materialious/static/nodejs-android/package.json new file mode 100644 index 00000000..7741a1dd --- /dev/null +++ b/materialious/static/nodejs-android/package.json @@ -0,0 +1,8 @@ +{ + "name": "cors-patch", + "version": "0.0.1", + "main": "./index.js", + "dependencies": { + "cors-anywhere": "^0.4.4" + } +} \ No newline at end of file