Google Apps Script does not load when embedded into iFrame

前端 未结 1 1921
醉酒成梦
醉酒成梦 2020-12-22 11:05

I am trying to embed my Google Apps Script WebApp into an iFrame on another domain but the webapp is not loaded and I only see a white screen. There is also no error in the

相关标签:
1条回答
  • 2020-12-22 11:54

    When publishing web-apps with access: "anyone", it is needed for the end user to be logged in. Otherwise, the user is redirected to Google login page. Google's login page does not allow itself to be iframed. If you're logged in, the web app isn't redirected to Google's login page and this error doesn't appear, else it appears.

    Possible solutions:

    • Warn users beforehand that they must login
    • Publish using access: "Anyone even anonymous"
    • If that's not possible, It is possible to detect whether the iframed page has window loaded or not using window.length(one of the few properties that is accessible cross origin), which will be 0 if page refused to connect and more than 0 in a web-app, because your page is iframed. Then it is possible to redirect the user to the login page.
    <!DOCTYPE html>
    <html>
      <body>
        body
        <iframe
          src="https://script.google.com/macros/s/[SCRIPT_ID]/exec"
          >Loading
        </iframe>
        <script>
          const main = (() => {
            console.log("loading page");
            const f = document.querySelector("iframe");
            f.onload = (e) => {
              console.log("iframe onload");
              const fw = f.contentWindow;
              if (fw.length > 0) console.info("logged in");
              else {
                alert("Please Login!");
                window.location = "https://accounts.google.com";
              }
            };
          })();
        </script>
      </body>
    </html>
    

    References:

    • Same origin policy
    0 讨论(0)
提交回复
热议问题