微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当在 Chrome 浏览器中启用“阻止第三方 cookie”时,类 ui.showModalDialog 会引发 LocalStorage Security 异常

如何解决当在 Chrome 浏览器中启用“阻止第三方 cookie”时,类 ui.showModalDialog 会引发 LocalStorage Security 异常

目前在我们的应用程序(Google 编辑器插件和 Google Workspace 插件)中,我们使用 showModalDialog 在 iF​​rame 中加载不同的 URL。 Link) 这是相同的代码片段:

var html = HtmlService.createHtmlOutputFromFile(‘top’).setWidth(700)
           .setHeight(620).setSandBoxMode(HtmlService.SandBoxMode.IFRAME)
           .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
DocumentApp.getUi().showModalDialog(html,“My Page”);

‘Top’ 是一个带有 iframe 的 html 页面,用于承载 URL。当用户单击加载项侧边栏上托管的 HTML 页面/卡片界面页面中的按钮时,此模式对话框会正确显示。这在启用“允许所有 cookie”时工作正常。

仅当用户在 Chrome 浏览器 cookie 设置中设置“阻止第三方 cookie”时才会发生异常。禁用第三方 cookie 后,当用户单击侧边栏中的按钮时,会出现对话框并且无法呈现页面并引发安全异常。 我在浏览器的控制台日志中看到以下本地存储安全异常。 Error on browser

未捕获的安全错误:无法从“窗口”读取“localStorage”属性:此文档的访问被拒绝。

我该如何解决这个问题?无论如何,是否可以通过谷歌应用程序脚本中的存储 API 设置 cookie?

这里更新了代码和问题的详细描述

我们正在尝试展示我们现有的文件选择器实现,以保持应用程序之间的一致性。 iframe 和 google App 脚本之间的通信方式是通过 Post Message 进行的。要触发 iframe, 这就是我正在尝试的 在编辑器插件中,

  1. 在 HTML 页面上有一个按钮,当点击事件被触发时,它会与 .gs 文件通信,在该文件中将以下代码写入函数
  2. 在其他 Google Workspace 插件中,我们通过卡片界面添加一个按钮,该按钮上的回调函数执行以下功能
    function() {
       var driveService = getDriveService();
        if(driveService.hasAccess()) {
            var driveService = getDriveService();
    var template = HtmlService.createHtmlOutputFromFile('top').setWidth(700)
        .setHeight(620).setSandBoxMode(HtmlService.SandBoxMode.IFRAME)
        .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
         DocumentApp.getUi().showModalDialog(template,“Modal Title");
    } else {
      showSidebar()
    }
}

在 top.html 内

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <style>
      
    </style>
  </head>
  <body onload="getTargetURL()">
  <iframe id="iframe" style="width: 700px; height: 610px;" src='' frameborder='0'> </iframe>
   <script>
   var dialogToken = '';
   var src = '';
   function getTargetURL() {
   google.script.run.withSuccessHandler((success) => {
        dialogToken = encodeURIComponent(success['dialogToken']);
        src = url + dialog token; // which is almost similar to access token      
        frame = document.getElementById('iframe');
        frame.src = src;
      }).withFailureHandler(() => {
      }).fetchDialogToken()
   }
   window.addEventListener('message',handleMessage,false);
   function handleMessage(e) {
   if(e['data'] != undefined) {
     google.script.run.withSuccessHandler((success)=> {
       console.log('successfully saved the document' + JSON.stringify(success))
     }).withFailureHandler((error)=> {
       alert("unable to save the document" + JSON.stringify(error))
     }).saveDocument(e)
   } 
   }
   </script>
   <script src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>
 </body>
</html>

appscript.json 文件


{
  "timeZone": "America/Los_Angeles","dependencies": {
    "enabledAdvancedServices": [{
      "userSymbol": "Drive","serviceId": "drive","version": "v2"
    }],"libraries": [{
      "userSymbol": "OAuth2","libraryId": "1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF","version": "35"
    }]
  },"oauthScopes": [
"https://www.googleapis.com/auth/script.external_request","https://www.googleapis.com/auth/script.container.ui","https://www.googleapis.com/auth/drive","https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive.Metadata","https://www.googleapis.com/auth/drive.addons.Metadata.readonly","https://www.googleapis.com/auth/documents"],"runtimeVersion": "V8","addOns": {
    "common": {
      "name": "GWA for add-on","logoUrl": " ","layoutProperties": {
        "primaryColor": "#3b69f2","secondaryColor": "#3b69f2"
      }
    },"docs": {
      "homepageTrigger": {
        "runFunction": "onLoadFunction"
      },"onFileScopeGrantedTrigger": {
        "runFunction": "onFileScopeGranted"
      }
    },"sheets": {
      "homepageTrigger": {
        "runFunction": "onLoadFunction"
      },"slides": {
      "homepageTrigger": {
        "runFunction": "onLoadFunction"
      },"onFileScopeGrantedTrigger": {
        "runFunction": "onFileScopeGranted"
      }
    }
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。