Skip to content

Load remote files using cors proxy #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,37 @@ function initialize() {
};
const loadUrlDB = $.urlParam("url");
if (loadUrlDB != null) {
setIsLoading(true);
const xhr = new XMLHttpRequest();
xhr.open("GET", 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);
}
}
}
}

Expand Down