2

I'm using the IP Geolocation API from Abstract

$.getJSON("https://ipgeolocation.abstractapi.com/v1/?api_key=YOUR_UNIQUE_API_KEY", function(data) {
       console.log(data.ip_address);
       console.log(data.country);
   })

EDIT: Basically I want to create a textfile that will save all the visitor IP addresses only onto my computer since I have the source files.

How can I save data.ip_address and data.country of each user onto only my local computer and not have the user click and download anything? Save the JSON into a texfile? That JSON request is on the one and only HTML file for my Github page. I just need the simplest solution for this quick, static project. I've tried using require(fs) but I keep getting an error and read that I can't really use nodeJS for what I'm doing.

xeno
  • 39
  • 2
  • Where is this code being run such that you don't have access to NodeJS or a similar framework, but a user would be visiting this page? JavaScript doesn't have a native way to interact with your computer filesystem; 'fs' is something that NodeJS provides. – JType Nov 30 '20 at 00:47
  • Does this answer your question? [How can JavaScript save to a local file?](https://stackoverflow.com/questions/11071473/how-can-javascript-save-to-a-local-file) – tevemadar Nov 30 '20 at 00:52
  • I'm running it on GitHub pages, it can host it for me and my username is the URL. @JType – xeno Nov 30 '20 at 01:02
  • Maybe I misworded my question. I basically want a texfile of all the visitor IP addresses saved onto my computer. I don't want the user to be able to access/download the JSON. @tevemadar – xeno Nov 30 '20 at 01:08
  • Well GitHub pages only allows you to run client side code if I'm not mistaken. So you can't have it automatically saving data to your pc. You need a server side language for that. For everyone's safety, client side languages do not have access to file systems. – icecub Nov 30 '20 at 01:09
  • That makes sense, thank you. So it seems there is no way around to what I want to do with GitHub pages being static. @icecub – xeno Nov 30 '20 at 01:17
  • There is a way. But it's just plain stupid to do. You can setup a mail server or Gmail and use SMTP. Then you can have the data automatically emailed to you through Javascript. But this requires you to expose your email password in the javascript source code. Which obviously is very, very stupid to do. One of those "just because you can, doesn't mean you should" situations. Other than that, no, it's not possible. – icecub Nov 30 '20 at 01:24

3 Answers3

2

Javascript doesn't have access to the local filesystem. You need a webserver and a server side language to be able to save something to the server's filesystem.

The only solution I can think of would be to force the browser to create a file and present it as a download. Something like:

var text = data.ip_address;
    text += '\n';
    text += data.country;

var filename = 'myData.txt';

var blob = new Blob([text], {type: 'text/csv'});
if(window.navigator.msSaveOrOpenBlob) {
    window.navigator.msSaveBlob(blob, filename);
} else {
    var elem = window.document.createElement('a');
    elem.href = window.URL.createObjectURL(blob);
    elem.download = filename;        
    document.body.appendChild(elem);
    elem.click();        
    document.body.removeChild(elem);
}
icecub
  • 7,964
  • 5
  • 34
  • 63
1

This is a nasty hack but you can run this (either on your site or in your console):

const win = window.open("", "Title", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=780,height=200,top="+(screen.height-400)+",left="+(screen.width-840));
win.document.body.innerHTML = JSON.stringify(data);

// So in your case
$.getJSON("https://ipgeolocation.abstractapi.com/v1/?api_key=YOUR_UNIQUE_API_KEY", function(data) {
  const win = window.open("", "Title", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=780,height=200,top="+(screen.height-400)+",left="+(screen.width-840));
  win.document.body.innerHTML = JSON.stringify(data);
})

and then save that opened window to your local machine as a json file.

thelastshadow
  • 3,020
  • 3
  • 31
  • 33
0

GitHub Pages does not support server-side languages such as PHP, Ruby, or Python.

-- from docs, so it's not possible to write files. GitHub pages serve static content, without any backend.
And even if it would be, collecting your users' location and IP into a GitHub repo might not be the best idea ever from privacy point of view.

tevemadar
  • 9,697
  • 2
  • 15
  • 36