1

I'm working on an html file in a google apps script project right now and I want it to be able to retrieve the html content of a web page, extract a snippet, and paste the snippet to the page. I've tried using fetch() and a couple of its options (mostly CORS), but I either get nothing back or an error that says "No 'Access-Control-Allow-Origin' header is present on the requested resource." A workaround I found was using google.script.run.withSuccessHandler() to return the html content via UrlFetchApp.fetch(url).getContentText(). The problem with this is that it is time consuming and I want to try and optimize it as much as possible. Does anyone have any suggestions on how to make this work or will I be forced to stick with my workaround?

Ben Craven
  • 37
  • 5
  • 1
    The permission error is because the website disallows posting it's content without permission. You can ask for their permission or see if they have a publicly available api for their data. – TheMaster Aug 16 '22 at 09:57

1 Answers1

1

Using "Vanilla JavaScript" in a Google Apps Script web application might not work to

retrieve the html content of a web page, extract a snippet, and paste the snippet to the page.

The above because the client-side code of Google Apps Script web applications is embedded in an iframe tag that can't be modified from server-side code. In other words, Google Apps Script is not universally suitable web development platform. One alternative is to use another platform to create your web application, i.e. GitHub Pages, Firebase, etc.

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • This is due to [same origin policy](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy) enforced by the browser. The browser disallows getting content from another website and putting it on your own. `UrlFetch` works only because the request is not bound by the browser, because the request is made server side from Google's servers. Even if the request is put in github pages or any client side page using `fetch`, the request will fail in any decent browser, due to same origin policy. – TheMaster Aug 17 '22 at 08:23