问题描述
目前在我们的应用程序(Google 编辑器插件和 Google Workspace 插件)中,我们使用 showModalDialog 在 iFrame 中加载不同的 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, 这就是我正在尝试的 在编辑器插件中,
- 在 HTML 页面上有一个按钮,当点击事件被触发时,它会与 .gs 文件通信,在该文件中将以下代码写入函数中
- 在其他 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 (将#修改为@)