From 53646320da1f0085246f0e8f0ea191a8a5cde9b9 Mon Sep 17 00:00:00 2001 From: vwh Date: Sun, 21 Jul 2024 20:00:57 +0300 Subject: [PATCH 1/2] Load remote files using cors proxy --- js/main.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/js/main.js b/js/main.js index 01e4794..acd65df 100644 --- a/js/main.js +++ b/js/main.js @@ -83,7 +83,11 @@ function initialize() { if (loadUrlDB != null) { setIsLoading(true); const xhr = new XMLHttpRequest(); - xhr.open("GET", decodeURIComponent(loadUrlDB), true); + xhr.open( + "GET", + `https://corsproxy.io/?url=${decodeURIComponent(loadUrlDB)}`, + true + ); xhr.responseType = "arraybuffer"; xhr.onload = function (e) { loadDB(this.response); From 50592a346f3b9d92b70aad024770615117140254 Mon Sep 17 00:00:00 2001 From: vwh Date: Mon, 22 Jul 2024 11:57:36 +0300 Subject: [PATCH 2/2] Making using proxy cors optional --- js/main.js | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/js/main.js b/js/main.js index acd65df..b369029 100644 --- a/js/main.js +++ b/js/main.js @@ -81,21 +81,37 @@ function initialize() { }; const loadUrlDB = $.urlParam("url"); if (loadUrlDB != null) { - setIsLoading(true); - const xhr = new XMLHttpRequest(); - xhr.open( - "GET", - `https://corsproxy.io/?url=${decodeURIComponent(loadUrlDB)}`, - true - ); - xhr.responseType = "arraybuffer"; - xhr.onload = function (e) { - loadDB(this.response); - }; - xhr.onerror = function (e) { - setIsLoading(false); - }; - xhr.send(); + const sendRequest = (useProxy = false) => { + setIsLoading(true); + const url = useProxy ? "https://corsproxy.io/?" + loadUrlDB : loadUrlDB; + const xhr = new XMLHttpRequest(); + xhr.open( + "GET", + decodeURIComponent(url), + true + ); + xhr.responseType = "arraybuffer"; + xhr.onload = function (e) { + loadDB(this.response); + return true; + }; + xhr.onerror = function (e) { + setIsLoading(false); + return false; + }; + xhr.send(); + } + const noCORSRequest = sendRequest(); + if (!noCORSRequest) { + const proxyConfirm = window.confirm( + "Unable to load the database from the provided URL due to possible CORS restrictions.\n" + + "Would you like to retry using a proxy?\n\n" + + "Note: Using the proxy may expose your database to corsproxy.io services." + ); + if (proxyConfirm) { + sendRequest(true); + } + } } }