如何在Google Apps脚本中动态加载外部AJAX网页?

Mow*_*zer 5 javascript ajax web-scraping google-apps-script puppeteer

我想加载由 JS(例如 AngularJS 或类似)生成的网页,然后(仅)使用 Google Apps 脚本抓取它。我怎样才能做到这一点?

我正在寻找类似的东西:

const response = UrlFetchApp.fetch( urlToExternalJsPage );
const content = response.getContentText();
// scrape content
Run Code Online (Sandbox Code Playgroud)

只是,也许,UrlFetchApp用来图书馆或其他什么的来代替?也许是 GAS 的 Puppeteer 库、GAS 的 Cheerio 库或其他东西?

如何加载外部加载的 JS 页面并在生成后从该页面读取 HTML 以抓取它?

想法1

我看到这篇文章:The Best Way to Load Javascript,它提供了以下代码。

function loadScript(url, callback){
  var script = document.createElement("script")
  script.type = "text/javascript";
  if (script.readyState){  //IE
    script.onreadystatechange = function(){
      if (script.readyState == "loaded" || script.readyState == "complete"){
        script.onreadystatechange = null;
        callback();
      }
    };
  } else {  //Others
    script.onload = function(){
      callback();
    };
  }
  script.src = url;
  document.getElementsByTagName("head")[0].appendChild(script);
}
Run Code Online (Sandbox Code Playgroud)

页面上的实际代码最终如下所示:

<script type="text/javascript" src="http://your.cdn.com/first.js"></script>
<script type="text/javascript">
  loadScript("http://your.cdn.com/second.js", function(){
    //initialization code
  });
</script>
Run Code Online (Sandbox Code Playgroud)

这种方法的问题是我试图严格保留在服务器端。我不想发布任何 HTML 页面和/或为它们提供服务。

想法2

我发现这篇文章似乎描述了一些 GAS 的 Puppeteer Libary。我用谷歌翻译把它从日语翻译过来。问题是它需要使用 Google Cloud Platform,我想避免这种情况。我还想避免设置任何结算并严格保留在 Google Apps 脚本内。

想法3

也许有办法使用浏览器自带的UI服务。具体来说,侧边栏?

在此页面上,我找到了以下使用 .html 文件将网页导入 HTML 服务页面的示例IFRAME

代码.gs
function doGet() {
  var template = HtmlService.createTemplateFromFile('top');
  return template.evaluate();
}
Run Code Online (Sandbox Code Playgroud) 顶部.html
<!DOCTYPE html>
<html>
 <body>
   <div>
     <a href="http://google.com" target="_top">Click Me!</a>
   </div>
 </body>
</html>
Run Code Online (Sandbox Code Playgroud)