Skip to content

Commit

Permalink
CORS
Browse files Browse the repository at this point in the history
  • Loading branch information
strukturart committed Mar 25, 2022
1 parent 1ac6561 commit 1e6196f
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 75 deletions.
2 changes: 1 addition & 1 deletion application/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<head>
<title>o.map</title>
<meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline' 'unsafe-eval'; script-src * 'unsafe-inline' 'unsafe-eval'; connect-src * 'unsafe-inline'; img-src * data: blob: 'unsafe-inline'; frame-src *; style-src * 'unsafe-inline';" />
<meta>
<meta charset="utf-8" />
<link rel="manifest" href="manifest.webapp" type="application/x-web-app-manifest+json" />
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1" />
Expand Down
40 changes: 26 additions & 14 deletions application/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ document.addEventListener("DOMContentLoaded", function () {

let osm_server_list_gpx = function () {
let n = "Bearer " + localStorage.getItem("openstreetmap_token");
//let token = "C_6istcvv43Lu6mQqfA0HqVQJ4jC6b_4L7mL6JSsXZs";
//n = "Bearer " + token;

const myHeaders = new Headers({
Authorization: n,
Expand Down Expand Up @@ -391,32 +393,42 @@ document.addEventListener("DOMContentLoaded", function () {

let osm_server_load_gpx = function (id) {
let token = localStorage.getItem("openstreetmap_token");
let n = "Bearer " + token;
console.log(token);
//token = "C_6istcvv43Lu6mQqfA0HqVQJ4jC6b_4L7mL6JSsXZs ";
let nn = "Bearer " + token;
let url = "https://api.openstreetmap.org/api/0.6/gpx/" + id + "/data";
let xmlhttp = new XMLHttpRequest();

helper.toaster(id, 1000);

xmlhttp.onreadystatechange = function () {
helper.toaster(xmlhttp.status, 1000);
helper.toaster(xmlhttp.response, 1000);
console.log(xmlhttp.getResponseHeader("Location"));
};

xmlhttp.onloadend = function () {
console.log(xmlhttp.getResponseHeader("Location"));
};

xmlhttp.onload = function () {
if (xhr.status != 200) {
// HTTP error?
// handle error
alert("Error: " + xhr.status);
return;
}
console.log(xmlhttp.getResponseHeader("Location"));
};

// get the response from xhr.response
xmlhttp.onerror = function () {
console.log(xmlhttp.getResponseHeader("Location"));
};

console.log(xmlhttp.status);

xmlhttp.open("GET", url, true);
xmlhttp.setRequestHeader("Authorization", n);
xmlhttp.withCredentials = true;
xmlhttp.timeout = 2000;

xmlhttp.setRequestHeader("Authorization", nn);
xmlhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xmlhttp.setRequestHeader("Access-Control-Request-Method", "GET");
xmlhttp.setRequestHeader("Access-Control-Allow-Methods", "GET");

xmlhttp.send(null);
};;
helper.toaster("done", 1000);
};

let OAuth_osm = function () {
let n = window.location.href;
Expand Down
1 change: 1 addition & 0 deletions application/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function getToken() {

getToken().then((result) => {
localStorage.setItem("openstreetmap_token", result.access_token);
alert(result.access_token);

document.getElementById("success").innerText =
"you are now successfully connected to the osm service";
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<head>
<title>o.map</title>
<meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline' 'unsafe-eval'; script-src * 'unsafe-inline' 'unsafe-eval'; connect-src * 'unsafe-inline'; img-src * data: blob: 'unsafe-inline'; frame-src *; style-src * 'unsafe-inline';" />
<meta>
<meta charset="utf-8" />
<link rel="manifest" href="manifest.webapp" type="application/x-web-app-manifest+json" />
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1" />
Expand Down
83 changes: 43 additions & 40 deletions docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,9 @@ document.addEventListener("DOMContentLoaded", function () {
/////////////

let osm_server_list_gpx = function () {
//alert(localStorage.getItem("openstreetmap_token"));
let n = "Bearer " + localStorage.getItem("openstreetmap_token");
let token = "pVOCkTG-lfDjps6HMXEYDE_tBsnW76fT-Xnl8WFuX70";
n = "Bearer " + token;

const myHeaders = new Headers({
Authorization: n,
Expand Down Expand Up @@ -391,34 +392,36 @@ document.addEventListener("DOMContentLoaded", function () {
}

let osm_server_load_gpx = function (id) {
let n = "Bearer " + localStorage.getItem("openstreetmap_token");
let token = localStorage.getItem("openstreetmap_token");
console.log(token);
token = "pVOCkTG-lfDjps6HMXEYDE_tBsnW76fT-Xnl8WFuX70";
let nn = "Bearer " + token;
let url = "https://api.openstreetmap.org/api/0.6/gpx/" + id + "/data";
let xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function () {
helper.toaster(xmlhttp.statusText, 1000);
//alert(xmlhttp.requestURL);
console.log(xmlhttp.getResponseHeader("Location"));
};

const myHeaders = new Headers({
Authorization: n,
});
xmlhttp.onloadend = function () {
console.log(xmlhttp.getResponseHeader("Location"));
};

fetch("https://api.openstreetmap.org/api/0.6/gpx/" + id + "/data", {
method: "GET",
headers: myHeaders,
redirect: "follow",
})
.then(function (response) {
alert(response.status);
if (response.ok) {
return response.blob();
} else {
throw new Error(Error);
}
})
.then(function (data) {
alert(data);
xmlhttp.onload = function () {
console.log(xmlhttp.getResponseHeader("Location"));
};

var file = window.URL.createObjectURL(data);
window.location.assign(file);
})
.catch(function (error) {
alert(error);
});
xmlhttp.open("GET", url, true);

xmlhttp.setRequestHeader("Authorization", nn);
xmlhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xmlhttp.setRequestHeader("Access-Control-Request-Method", "GET");
xmlhttp.setRequestHeader("Access-Control-Allow-Methods", "GET");

xmlhttp.send(null);
helper.toaster("done", 1000);
};

let OAuth_osm = function () {
Expand All @@ -431,7 +434,7 @@ document.addEventListener("DOMContentLoaded", function () {
);
url.searchParams.append(
"redirect_uri",
"https://strukturart.github.io/o.map/oauth.html"
"https://strukturart.github.io/o.map/"
);
url.searchParams.append("scope", "read_gpx");
const windowRef = window.open(url.toString());
Expand Down Expand Up @@ -963,40 +966,40 @@ document.addEventListener("DOMContentLoaded", function () {
map.on("zoomend", function (ev) {
let zoom_level = map.getZoom();
if (zoom_level < 2) {
general.step = 10;
general.step = 20;
}
if (zoom_level > 2) {
general.step = 7.5;
general.step = 8;
}
if (zoom_level > 3) {
general.step = 5;
general.step = 4.5;
}
if (zoom_level > 4) {
general.step = 1;
general.step = 2.75;
}
if (zoom_level > 5) {
general.step = 0.5;
general.step = 1.2;
}
if (zoom_level > 6) {
general.step = 0.25;
general.step = 0.5;
}
if (zoom_level > 7) {
general.step = 0.1;
general.step = 0.3;
}
if (zoom_level > 8) {
general.step = 0.075;
general.step = 0.15;
}
if (zoom_level > 9) {
general.step = 0.05;
general.step = 0.075;
}
if (zoom_level > 10) {
general.step = 0.025;
general.step = 0.04;
}
if (zoom_level > 11) {
general.step = 0.01;
general.step = 0.02;
}
if (zoom_level > 12) {
general.step = 0.0075;
general.step = 0.01;
}
if (zoom_level > 13) {
general.step = 0.005;
Expand All @@ -1008,7 +1011,7 @@ document.addEventListener("DOMContentLoaded", function () {
general.step = 0.001;
}
if (zoom_level > 16) {
general.step = 0.0001;
general.step = 0.0005;
}
});

Expand Down
3 changes: 2 additions & 1 deletion docs/manifest.webapp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

"redirects": [
{
"from": "https://strukturart.github.io/o.map/oauth.html",

"from": "https://strukturart.github.io/o.map/",
"to": "/oauth.html"
}
],
Expand Down
3 changes: 2 additions & 1 deletion docs/oauth.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
</head>

<body>
you are now successfully connected to the osm service
<div id="success"></div>

</body>

</html>
28 changes: 11 additions & 17 deletions docs/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ function getToken() {
var urlencoded = new URLSearchParams();
urlencoded.append("code", code);
urlencoded.append("grant_type", "authorization_code");
urlencoded.append(
"redirect_uri",
"https://strukturart.github.io/o.map/oauth.html"
);
urlencoded.append("redirect_uri", "https://strukturart.github.io/o.map/");
urlencoded.append("client_id", "KEcqDV16BjfRr-kYuOyRGmiQcx6YCyRz8T21UjtQWy4");

var requestOptions = {
Expand All @@ -20,25 +17,22 @@ function getToken() {
redirect: "follow",
};

fetch("https://www.openstreetmap.org/oauth2/token", requestOptions)
.then((response) => response.json())
.then(function (p) {
alert(urlencoded);
alert(JSON.stringify(p));
localStorage.setItem("openstreetmap_token", p.access_token);
});
return fetch(
"https://www.openstreetmap.org/oauth2/token",
requestOptions
).then((response) => response.json());
}
getToken();
/*

getToken().then((result) => {
alert(result);
localStorage.setItem("openstreetmap_token", result.access_token);

document.getElementById("success").innerText =
"you are now successfully connected to the osm service";

window.dispatchEvent(
new CustomEvent("tokens", {
detail: result,
})
);
// window.close();
window.close();
});
*/

0 comments on commit 1e6196f

Please sign in to comment.