2

I am trying to get a screenshot of a website using Google Apps Script from the URL. I was following this guide

I currently have the following code

function snapScreenshot() {
  var siteUrl = 'https://praveen.science/';
  var url = "https://www.googleapis.com/pagespeedonline/v4/runPagespeed?screenshot=true&url=" + encodeURIComponent(siteUrl) + "&key=API_KEY";
  var res = UrlFetchApp.fetch(url).getContentText()
  var obj = JSON.parse(res);
  var blob = Utilities.newBlob(Utilities.base64DecodeWebSafe(obj.screenshot.data), "image/png", "sample.png");
  DriveApp.createFile(blob);
}

I currently have this error on var res

Exception: Request failed for https://www.googleapis.com returned code 404. Truncated server response: <!DOCTYPE html>

And if I use var url = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url= I am able to get the JSON. I changed my var blob to

var blob = Utilities.newBlob(Utilities.base64DecodeWebSafe(obj.lighthouseResult.audits['final-screenshot'].details.data), "image/jpeg", "sample.jpeg"); 

but am getting the error

Exception: Could not decode string.

on var blob

Thanks a lot for your help.

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • The screenshot is provided as a data URI. You'll need to strip out the prefix `data:image/jpeg;base64,` to get the raw base64 encoded string. Also that encoded string is not 'web safe', you'll have to use the non-web-safe function instead ie. `Utilities.base64Decode()` once you remove the prefix. – TheAddonDepot Dec 02 '21 at 22:58

1 Answers1

0

The PageSpeed API is now at version 5 (an earlier version of the API was used in the guide you referenced). Apparently, the latest version of the API no longer supports screenshots (and all earlier versions of the API seem to be disabled/sunset).

Check the PageSpeed API reference. The screenshot query parameter is noticeably absent in the reference documentation.


In the V5 version of the API, screenshots are now directly accessible as a data URI in the JSON response to the runpagespeed endpoint.

JSON response is formatted as follows:

{
    ...
    ...
    "lighthouseResult": {
        ...
        ...
        "audits" : {
            ...
            ...
            "full-page-screenshot" : {
                ...
                ...
                "details": {
                    ...
                    ...
                    "screenshot": {
                        "data": "data:image/jpeg;base64,..."
                    }
                }
            }
        }
    }
}
TheAddonDepot
  • 8,408
  • 2
  • 20
  • 30
  • 1
    I know that the screenshot query parameter is absent for v5, but instead is in the parsed JSON when I console log `obj.lighthouseResult.audits`. Anyway, I found out the answer and it's currently working, but thanks. – Wing Hang Khoo Dec 02 '21 at 22:02
  • @WingHangKhoo That's awesome! I'll update my answer accordingly. – TheAddonDepot Dec 02 '21 at 22:18
  • I used v5 and what I did was `replace(/^data:image\/[a-z]+;base64,/, "")`. That fixed my problem. – Wing Hang Khoo Dec 03 '21 at 03:51