0

I have a number of JavaScript functions that I am using in Google Sheets to perform various webpage speed tracking and archiving steps.

Would like to leverage the Internet Wayback Archive to store pages when the page score shifts +/- a particular score range.

The challenge that I'm having is that I'm not sure how (if it's possible) to execute a converted curl command from within the Google Sheets script editor interface.

If I understand things correctly, I would need to convert the following curl code:

curl -X POST -H "Content-Type: application/json" -d '{"url": "google.com"}' https://pragma.archivelab.org

to the following format:

$.ajax({
  url: "https://pragma.archivelab.org",
  type: 'POST',
  dataType: 'json',
  contentType: 'application/json',
  processData: false,
  data: '{"url": "google.com"}',
  success: function (data) {
    alert(JSON.stringify(data));
  },
  error: function(){
    alert("Cannot get data");
  }
});

Am I approaching this the correct way? Will this type of call work in the Google Sheets scripting toolset?

Caelaran
  • 45
  • 2
  • 9
  • Please add the jQuery tag. – evolutionxbox Sep 05 '17 at 13:19
  • That endpoint does not employ CORS, does not send [Access-Control-Allow-Origin header](https://stackoverflow.com/a/10636765/560593), so you cannot directly access it through browser ajax request. You would need the server as a middle man to deliver the data, or find out if they have a JSONP format that can be used – Patrick Evans Sep 05 '17 at 13:21
  • Well poop, will follow up with them, thanks! – Caelaran Sep 05 '17 at 13:35

2 Answers2

0

The biggest thing that differentiates cURL and Ajax is that AJAX is client side code. Browsers have to follow strict behaviours to prevent malicious behaviours. Ajax is bound to the CORS policy which means that if you are requesting a page from another domain, that domain should explicitly state that your domain (or global domains, or even wildcard for) is allowed to do such thing.

The JSONP protocol is not bound to the CORS. It is done so because it cannot cause harm, and it makes building/using APIs easier. Luckily, the web archive seems to be well supported by mementoweb, who provide an API just like that.

Read more about it here

Send your request to http://timetravel.mementoweb.org/api/json/YYYY<MM|DD|HH|MM|SS>/URI and mementoweb will send you the list of Memento closest to the specified date.

I hope it helps you go forward.

And to answer your question, your request is just fine. Note that the data attribute could be a simple object instead of string, jQuery will handle the rest for you.

Salketer
  • 14,263
  • 2
  • 30
  • 58
  • While your comment is helpful what I am trying to do is to force an artifact save at a particular point in time if a threshold cross is detected, instead of finding the most recent artifacts around a timestamp where those artifacts may / may not include the modified webpage. – Caelaran Sep 05 '17 at 17:21
  • Then I'd either use a webserver to handle the request, or dig deeper into the API doc to see if such a thing is supported. – Salketer Sep 06 '17 at 06:18
0

Try sending the requests to your own server and having said server do the curl using something like python on php on the server side, that way you can go around the CORS headers nightmare you are in your way to.

Felipe Valdes
  • 1,998
  • 15
  • 26